Applescript: you’ve heard of it, but you have no idea where to begin…This little tutorial will hopefully point you in the right direction to begin making your own scripts and additions (for example for Salling Clicker or iTunes).

Applescript is a scripting language, soley for Macs, allowing free and diverse actions to be performed at a base level on many applications, and at a GUI level for anything. Actions? Well, like turning the iTunes volume up, or adding a photo to iPhoto, or checking the mail, or sleeping your computer, or copying files…

Applescript functionallity must be built into an application in order for full use to be made of it, otherwise you must perform GUI actions on (such as pointing and clicking etc) that application. We will begin by finding out if your application has applescript actions built into it, or an applescript dictionary as it is known.

Open Script Editor (it is in your applications folder, in a folder called ‘applescript’).

A window like the one shown opposite opens…—->

Choose file, open dictionary to see a list of all the applications found on your computer which script editor deems to have applescript dictionarys. Open one (Finder, for example which has the most full dictionary). We’ll discuss how to use these functions later, for the moment: the basics of Script Editor.

Script editor is a simple application, designed to make and test scripts. It doesn’t have many functions to learn about, you only need to know the applescript language to make full use of script editor…

The buttons at the top are not really that useful, I’ve never had ‘record’ work like I want it to, so don’t bother with that; ‘stop’ will stop a currently executing (running) script; ‘run’ will attempt to compile (more on that later) and run yor applescript; ‘compile’ will just compile it.

This brings us neatly onto the main and of course most useful function of script editor, as a compiler. In programming, compiling basically means to take the code you have written, checking it makes sense (this doesn’t mean it will do what you want it to, just that it makes sense in terms of the programming language you are using..) and translating it into actions which the processor can understand and perform. Script editor also makes the code you have entered nicely formatted, making it easy to understand the layout of your script, as well as showing you that your script has been sucessfully compiled.

Lets have a go at compiling our first applescript.

Put the following text into a new Script Editor window (just copy and paste it):

tell application "iTunes"

try
launch
pause
say "I am going to find, then play a random track which you have played more than 20 times"

set shuffle of current playlist to false
set shuffle of current playlist to true
next track
repeat while played count of current track is less than 20
next track
end repeat
play
end try

end tell


Notice how the text is purple, this shows the script is not yet compiled. Now click ‘Compile’ and it changes to look something like the following —>

Now run the script. It does exactly what it says on the tin:-

- Launches iTunes (if it’s not already open)

- Pauses iTunes

- Speaks some text

- Turns shuffle off then on again (to make sure the songs are in a random order)

-Skips a track, and continues skipping until it finds a song in that playlist that has been played more than 20 times

- Plays iTunes

Now you can save the script as a script document (to edit later) or as an application (double click it like a normal appliction to run). Be warned: saving an a script as an application and selecting ‘run only’ will mean you cannot edit it again…you can’t even see the code again so don’t do this unless you absolutely need to.

So how do you know what to write in a script to get it to do what you want? Well, usually you begin by selecting which appplication you want to target with ‘Tell “application”‘, making sure you put ‘end tell’ at the end again. In the script above we also used ‘try’ ‘end try’ which means the script will attempt to perform all the actions enclosed between ‘try’ ‘end try’, but will not give an error warning if it fails, it will just stop.

Lets try a slightly more complex script which uses ‘variables’ which are like algebrayic terms in maths: letters or words made up by you which can be given any value you want:
try
try

set uptime to do shell script ?
"echo `uptime`| awk 'BEGIN {FS = \",\"} {print $1\",\" $2}' | awk 'BEGIN {FS = \"up \"} {print $2}'"

set results to display dialog "Your Mac has been started up for " & uptime ?
buttons {"OK", "OKEY DOKEY", "AMAZING"} default button "OK"

if (button returned of results is "AMAZING") then
say "it really is, isn't it"
else if (button returned of result is "OK") then
say "boring response"
else
beep
say "you said okay doekay"
end if

end try


In the above script we see a few new things. First of all we are using the variable ‘uptime’ which we are setting to the result of a shell script (Unix command) which turns out to be a command which returns the uptime of your computer. We are then using another variable called ‘results’ which is set to the answer to a dialog box we are creating. Notice how we encorporate ‘uptime’ into the dialog text, and how we can choose the answers and even the default highlighted option. Finally we are choosing what to do based on which button was clicked. The ‘?’ tells the computer to continue the current command onto a second line.

So where does this all leave us? Well, obviously I have barely touched on the potential of applescript, and the possibilities are literally endless. To find out which code to use to do exactly what you want, try by following ‘My 3 steps to applescript success’:

  • Check the dictionary of the program you are trying to perform actions with (Use ‘System Events’ to script the GUI).
  • Use Trial and Error; it will get you somewhere and help you learn…
  • Most importantly, start with code already written for you by others to help you learn, and ask people if you are stuck, try www.macscripter.net (or google!).

Leave a Reply...