1

I am using a MacOS osascript command in a shell script and I try to run the following command:

APPNAME=$@

if pgrep -x "$APPNAME" > /dev/null # checking if app is open
then
  echo "Closing..."
  osascript -e 'quit app $APPNAME'

else
  echo "*** The app is not open"
fi

Ideally, the command would be osascript -e 'quit app "Calendar"' as an example of a working solution. However, I can't seem to insert a variable in-between ' ' quotation marks.

What would be a workaround?

6
  • See Process Management for how to do this sort of thing properly. Commented May 28, 2020 at 20:38
  • @DaniilGannota: Why are you using single quotes when you don't want interpolation to occur? Aside from the obvious solution osascript -e "quit app $APPNAME", an easy-to-read alternative, which might come handy if you want to do one day a mixture of interpolated and non-interpolated strings, is osascript -e 'quit app '"$APPNAME" . Commented May 29, 2020 at 7:59
  • 1
    @user1934428 I tried both these but they throw me an error! I get errors -2740, -2741 and -2743. That's why I am asking here :) Apparently, this method worked for me: osascript -e 'on run argv' -e 'quit app (item 1 of argv)' -e 'end run' "$appname" Commented May 29, 2020 at 10:12
  • Closers: not a duplicate -- see chepner's comment and OP's follow up. This is a miscategorized Apple specific question, not a *nix question.. Commented May 29, 2020 at 10:42
  • @DaniilGannota: If you want to pass double quotes literally to osascript (which would be an odd requirement, but not inconceivable), you have of course to include them in the command: osascript -e 'quit app "'"$APPNAME"'"'. I just show the general principle of how to handle quoting. You need to apply this in the way it fits for your concrete usage case. Commented May 29, 2020 at 10:49

4 Answers 4

4

Trying to build a script dynamically using string interpolation is always fragile. You should pass the app name as an argument to the AppleScript, so that you don't have to worry about escaping any characters through two levels of interpreters.

APPNAME=$1  # The name should be passed as a single argument.

if pgrep -x "$APPNAME" > /dev/null # checking if app is open
then
  echo "Closing..."
  osascript -e 'on run argv' -e 'quit app (item 1 of argv)' -e 'end run' "$APPNAME"

else
  echo "*** The app is not open"
fi

No matter what the value of APPNAME is, you are executing the exact same script; the only difference is the argument that script receives.

Sign up to request clarification or add additional context in comments.

Comments

1

The point (one of the points, anyway) of single quotes is to prevent variable interpolation. It sounds like what you really want is a way to get double quotes into a string. There are lots of ways to do that. A common one is:

osascript -e "quit app \"$appname\""

4 Comments

Cross your fingers that appname doesn't contain a double quote :) The safe version is something like osascript -e 'on run argv' -e 'quit app (item 1 of argv)' -e 'end run' "$appname" (there may be a slightly shorter way to specify a multiline script; I don't know if there's an AppleScript equivalent for ; like bash uses.)
Apparently not, though since the question is tagged bash and zsh, something like osascript -e $'on run argv\nquit app (item 1 of argv)\nend run' textedit is an option. (Moral of the story: don't use parameterized "one-liners" in AppleScript.)
I get the following error when I try to run it syntax error: A identifier can’t go after this number. (-2740). Do you know why?
@chepner as I am very inexperienced with shell commands, I am not entirely sure what you did there, but it worked! Thanks a lot 😊
0

This implementation worked for me:

osascript -e 'quit app "'"$APPNAME"'"'

Trying this using:

osascript -e 'quit app "\"$APPNAME\""'

did not work resulting in error showing: "\"$APPNAME\""... as the value it is seeing.

See https://discussions.apple.com/thread/251633896 for more

Comments

-1

Try:

osascript -e 'quit app '"$APPNAME"

Or, if osascript requires additional double quotes, try:

osascript -e 'quit app "'"$APPNAME"'"'

6 Comments

For some reason the latter example is easier to keep straight in my head than the backslash \" equivalent -- while writing it anyway. Reading it later on it looks equally obscure.
This is still fragile: your second one assumes that $APPNAME itself doesn't contain double quotes.
@chepner, Re "fragile": Thanks, I'm not up on MacOS quoting, so I'll probably delete this answer soon.
This isn't unique to macOS; you have the same problem on any platform. The bigger problem here is that you are nesting one script inside another script, which just multiplies the quoting concerns.
@chepner, In that case it's unclear what "your second one assumes ... double quotes" is warning against. This works f='"dquote"' ; echo "$f", so if used carefully what is fragile there? (Agreed however, that minimizing quoting is preferable.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.