Pass in variable from shell script to applescript Pass in variable from shell script to applescript shell shell

Pass in variable from shell script to applescript


Shell variables doesn't expanding inside single quotes. When you to want pass a shell variable to osascript you need to use double "" quotes. The problem is, than you must escape double quotes needed inside the osascript, like:

the script

say "Hello" using "Alex"

you need escape quotes

text="Hello"osascript -e "say \"$text\" using \"Alex\""

This not very readable, therefore it much better to use the bash's heredoc feature, like

text="Hello world"osascript <<EOFsay "$text" using "Alex"EOF

And you can write multiline script inside for a free, it is much better than using multiple -e args...


You can also use a run handler or export:

osascript -e 'on run argv    item 1 of argvend run' aaosascript -e 'on run argv    item 1 of argvend run' -- -aaosascript - -aa <<'END' 2> /dev/nullon run {a}    aend runENDexport v=1osascript -e 'system attribute "v"'

I don't know any way to get STDIN. on run {input, arguments} only works in Automator.