Learn How to Use Applescript on a Mac
Simple syntax
The syntax of Applescript is fairly simple, and it’s made to be as understandable as possible, even if you’ve never programmed before. You might have to install it from the install disc, but AppleScript is generally included in most OS X default installations. If you have it, you will probably find it in Applications → Applescript → Script Editor. All programming is done in the upper field of the window that pops up.
To set a variable you write set variable to value. This will create the variable if it doesn’t already exist. Next thing you must learn is tell. It’s used to communicate with other programs. For example the phrase tell application “VLC” to quit is a valid line of a program.
Indentation
The Script Editor has automatic indentation when you compile your code. To create indented code, or blocks as they are called, you will have to surround your code with a statement. A statement like that is most often created by writing something like keyword (arguments) … end keyword. For example we could use tell, like we did before. This would be:
tell application “VLC”
quit
end tell
This is used for other keywords too, such as if and repeat. Set is illustrated here:
set repeats to 5
User interaction
Most scripts are unusable if you can’t affect them. The way to do this in Applescript is called display. It’s used like this:
set dlg to (display dialog “Hello boy” default answer “Test” buttons {“m’key”, “no”})
display alert “You said " & (text returned of dlg) & " and pressed the button " & (button returned of dlg)
Note the parentheses. They are there to make it clear to the program what we mean, so that it for example doesn’t think we want to get text returned of (dlg & " and pressed the button “). You should always be crystal clear about what you mean when programming.
Application interaction
Now comes the useful part of this how-to. Application interaction can mean for example closing an application (as demonstrated before with VLC) or to use your address book to print out your whole address book on paper. This is where the tricky part comes in; different programs have different commands. The easiest way to find out how to script a program is to open up the dictionary (File → Open Dictionary…) and find the program you want to interact with in that list. Different programs have different sophistication levels, and you might sometimes get disappointed with what you find in the dictionary, as some programs are even made without the ability to script for them. We will come back to that again later.
Now we’re going to take a look at QuickTime, because I feel like making a script that starts Spotify when I’m finished looking at a movie, and have it on until I fall asleep. In the QuickTime Player Suite we find a lot of different interaction methods. We also see that we have to name the document we want to interact with. This can be done in a very simple way. If you want to test this you can try the command tell application “QuickTime Player” to display alert (name of front document as string). This will tell you which movie you have on top.
Supported Applications
Now, we want to check in with QuickTime once in a while, I’ll check in once per second, and we want to know if the movie is done playing. To do this we look in the dictionary under document and find the variable done. The variable information says that it is a boolean (which means that it can be true or false) and that it’s read-only. We will want to keep this in a loop until we’re done watching.
tell application “QuickTime Player”
repeat
delay 1
if done of front document then
exit repeat
end if
end repeat
end tell
That was the easy part, the (well-)supported application.
Unsupported applications
This is a little bit trickier, because what you have to do now is not beautiful. Lets say that I listen to music through spotify, which has no applescript support at all. To do this there’s two ways. One is to use the shortcuts of what you want to do and one is GUI scripting (which is disabled by the system by default).
The shortcut version would be:
tell application “Spotify” to activate
delay 3 # Wait just in case the program was turned off, and needs to login
tell application “System Events” to keystroke space
Now this is fairly simple, and can handle all shortcuts in the menu there is (keystroke “S” using command down will save the topmost application, for example). This covers most of what GUI scripting can do, and for this guide to keep to simplicity, we will not jump into GUI scripting.
Finished code
Now we have everything we need to make my program work as I wished. We start by setting some variables. You will see in the code what the variables mean.
set sleeptime to 60 * (text returned of (display dialog “For how many minutes do you wish to have media?” default answer 60)) as integer
set precision to 1
set spotifyStartTime to 3
tell application “System Events”
if (name of processes) contains “QuickTime Player” then
tell application “QuickTime Player”
repeat
delay precision
set sleeptime to sleeptime - precision
if done of front document or sleeptime is less than 1 then
quit
exit repeat
end if
end repeat
end tell
end if
end tell
As you can see, precision is how long we wait between the checks with QuickTime. Also note that we ask System Events if QuickTime is running, or it would start QuickTime to ask it if it’s done.
if sleeptime is greater than spotifyStartTime then
tell application “Spotify” to activate
delay spotifyStartTime
tell application “System Events” to keystroke space
end if
Here we check if there’s any time left to play music, and if there is we put Spotify on top (or start it), wait for it to logon (in case we started it) and send a space to it to make it play.
activate application “ScreenSaverEngine”
delay sleeptime - spotifyStartTime
tell application “Spotify” to quit
Now all we have to do is to start the screensaver (why not?) and wait for the time to run out. When the time is out we shut spotify down. The reason we don’t just send another keystroke to it is that that keystroke would be caught by the screensaver, and the music wouldn’t stop playing.
Finished
Now we are finished with the script, and all that’s left to do is to save it. This is done by simply pressing File → Save As… and making sure that the File Format is Application. If we don’t want it to be readable we set Run Only. Now we have done a simple application and user interaction script, and hopefully learned something in the process.