AppleScript Tips for Shell Scripting
3 October 2015
OS X is surprisingly scriptable, including OS X applications. (Especially Apple’s applications). This is the good news. The bad news is that said scripts are difficult to write and debug, and they don’t play very well with Unix-style shell scripts. (Manipulating Unix-style filenames is especially painful.)
Here’s an example of what can be done:
select works like
open except that it opens a Finder window with the arguments selected:
The AppleScript code is complicated and involves far more workarounds than it really should but, hey, it does work.
Some tips and tricks in no particular order.
&for string concatenation:
return "Hello, " & name
as [type]to coerce to a string (you’ll need this for sane debugging):
return foo as stringwould be written in C-like languages as
return (string) foo
- Built-in properties can contain spaces:
track number of current trackis equivalent to something like
- AppleScript tries to be English-like (never a good idea): assignment is via
set foo to bar(not
set foo = bar) and you see things like
count of every media item of container src
log to dump output for debugging, though note that some types are coerced into strings for output–compare the output of
log obj and
log class of obj if not sure.
osascript’s error messages are pretty awful; for a slightly (slightly!) better development experience, use the Script Editor application. This gets you syntax highlighting, and slightly better
For best results click on the “page” icon in the bottom of the window to reveal the “messages” tab:
(Unfortunately Script Editor can’t open files that start with a shebang (see below)–you’ll need to copy and paste AppleScript to and from a text editor into Script Editor.)
There’s a few different ways you can run AppleScript from the command line. First of all, you can use the standard
#! shebang with
osascript as the interpreter:
To read arguments, use the
You might often find that you often want to do some pre-processing via
bash, especially for anything that involves filenames. This can be achieved (as in the
select example) via a
Use Script Editor’s “File | Open Dictionary…” to figure out what different applications let you do via AppleScript. Apple applications often expose a reasonably large API; for others you might get something minimal, or even nothing at all.
Spotify, for example, only lets you play/pause/next/prev. You can also get some information about the currently-playing track. One the other hand Apple Photos has a fairly rich API. (Example script.)
Working with filenames
Need the current working directory?
set pwd to do shell script "pwd"
Create file object from Unix-style path (with “/”):
(POSIX file unixpath)
Get Unix-style path from a file object:
(POSIX path file)
For more information on handling filenames, see: