Both shell- and AppleScripts can be used just about anywhere either is appropriate.
AppleScripts work better when talking to apps and user-level system facilities. (For instance, you can say tell app "iTunes" to play
and it will, or tell app "Finder" to open the first file of the second window
.)
Shell scripts (you referred to bash) work better when talking to low-level system objects and Unixy stuff.
They're both interoperable: you can call a shell command from AppleScript with do shell script "command here"
and you can call AppleScript code from the shell with osascript -e "AppleScript"
. Further, there's quite a bit of overlap between the two (especially when it comes to file system operations).
There are certainly more resources for learning shell scripting online than AppleScript and it doesn't help that AppleScript has a reputation for being a "read-only" language (i.e., harder to write than read).
In any case, the most modern way to do scripting is with Automator, where you can use either shell- or AppleScripting (or both) along with pre-built modules from various apps you have installed.
The argument for osascript -e
can contain newlines:
osascript -e 'set x to "a"
say x'
You can also specify multiple -e
arguments:
osascript -e 'set x to "a"' -e 'say x'
Or if you use a heredoc, bash
interprets three characters (\
, $
, and `
) between <<END
and END
but no characters between <<'END'
and END
.
osascript <<'END'
set x to "a"
say x
END
Edit:
Since osascript can operate with a heredoc (ie take input from /dev/stdin) then one can just write the script as a whole file and prepend with the correct shebang line:
#!/usr/bin/env osascript
set x to "a"
say x
This also allows you to save your apple script as a actual program in ~/Applications/.app using the following procedure (changing for your script's name):
mkdir -p ~/Applications/<APP_NAME>.app/Contents/MacOS
touch ~/Applications/<APP_NAME>.app/Contents/MacOS/<APP_NAME>
open -A TextEdit ~/Applications/<APP_NAME>.app/Contents/MacOS/<APP_NAME>
Ensure that both the script file in .../MacOS/ and the matches
Best Answer
Credit:Control haptic engine and force touch from terminal @bmike