10

This is based on the answer to:

https://apple.stackexchange.com/questions/103565/bash-script-that-will-start-up-second-terminal-process

Doing this applescript command works fine in terminal (it opens a new window and tells me the uptime):

osascript -e 'tell app "Terminal" to do script "uptime"'

However, trying to pass a variable as a string literal does not work:

cmd="'tell app \"Terminal\" to do script \"uptime\"'"
osascript -e ${cmd}

"0:1: syntax error: A unknown token can’t go here. (-2740)"

What's going on?

2 Answers 2

9

I can't really explain why the below works, but it definetively has something to do with parsing text in the shell. The quotes around $cmd, sees to that the space is preserved. Osa script in itself, isn't too happy about apostrophes, (singleticks), so I guess that is why the your version didn't work.

You can do like this:

 cmd="tell application \"Terminal\" to do script \"uptime\""
 osascript -e "$cmd"

At least this worked for me. :)

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

5 Comments

This is why shell ranks third in "this language has an annoying syntax", see hammerprinciple.com/therighttool/statements/…
I really wrote why it works above, expanding the variable, within the quotes, preserves the space. whereas osascript really can't cope with apostrophes, (single ticks). Well, the shell in itself, isn't that bad, but say you want a do shell script, that performs an osascript, then you are up to some serious quoting. :) I don't see forth mentioned on that list, forth must be the worst of all languages at all times.
For safetly, I recommend you use single quotes on the first line of your shell script (i.e. cmd='tell application "Terminal" to do script "uptime"'). Bash uses single quotes to denote string literals and double quotes to denote interpolated strings. Fortunately it isn't a problem on this occasion as your AppleScript code doesn't contain any of Bash's "special" characters. However, it's easy to forget exactly which characters Bash automatically substitutes, and things can go all sorts of blooey should Bash start rewriting bits of your AS code for you.
That is correct, I started out with an applescript, and there I had to remove the apostrophes to make that work, later on, they were off. Apostrophes retains whitespace in shell script, and nothing gets to be substituted within them, whereas anything containing a dollar sign, and backticks will get to be substituted/expanded when the "text" is surrounded by quotes. (And . . . wildcards (globs))!
I also pondered whether I should do like foo said, and rewrite it, but I declined, since it for some reason fails.
1

Similar issue with stopping teamviewer via osascript solved for me by double-escaping in order to get the internal double-quotes. I did not use single quotes to allow bash expansion.

# TeamViewer
alias tvstop="sudo osascript -e \"quit app \\\"TeamViewer.app\\\"\""
alias tvstart="open -g -a /Applications/TeamViewer.app"

Comments

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.