Every computer user has to deal with boring and time-consuming operations such as sending a lot of similar emails, converting files between formats, making backups and so on. Mac OS has a handy tool that is called AppleScript which can perform all these tasks for an user. This post shows how to start with AppleScript and how to use it in order to "communicate" with MAC applications.
AppleScript is a scripting language created by Apple. Designed for easy learning, it provides syntactic constructions that resemble the written English. The main feature of AppleScripts is that it allows to send commands to different applications in order to automate some repetitive or time-consuming operations, basically, AppleScript can "tell" applications what to do next.
Mac OS comes with its own tool - AppleScript Editor which provides quite a basic functionality for scripting. So, to write complex scripts, in my opinion, it is better to use Script Debugger that has debugging facilities and many other handy features.
Let's have a quick look at Script Debugger:
The text area in the left part of the window is just used for script editing, but at the right side you can find the Result, Variable and Expression sections that work the following way:
- Result shows the result of the last executed expression
- Variable contains the list of all variables with their values
- Expression shows values for the selected variables or user-entered expressions
To learn more about Script Debugger, take a look at the video tutorials on Script Debugger website.
AppleScript syntax basics
AppleScript is similar to English, so it is quite easy to read and understand. Here is the code example which shows how the most frequently used code elements look like:
1 2 3 4 5 6 7 8 9 10 11
One of the most interesting, distinctive features of AppleScript is global variables. For example,
AppleScript's text item delimiters variable which holds a delimiter value that is used to split strings:
1 2 3 4 5 6 7
Another must-use feature is the
result global variable:
After the statement is executed, the script stores the resulting value, so it can be accessed later via the
result script variable.
It is often needed to execute the same piece of code several times, in order to make such a code more readable it is possible to use handlers. The handler is a group of one or more AppleScript statements that are associated with a single command.
The handler definition always begins with the words on or to followed by its name and parameters. The statements that should be executed by the handler come after the first line of the definition. The last line of the handler always begins with the word end, followed by the name of the handler.
1 2 3 4 5
In the example, the handler's name is
showDialog, and parameters in brackets are
The most useful feature of the AppleScript is that it can "tell" applications what to do. The commands that can be used for a specific application are located in the AppleScript dictionary. To open the dictionary just use the Open Dictionary option in the File menu of the Script Editor.
As an example, let's look at a code sample that works with Text Editor application:
1 2 3 4 5
Since the code located inside the tell-block is executed by Text Editor, the second line of the example will create Text Editor document with name “First doc”.
Lastly, if you want to call your own handler inside a tell block, use the reserved words
of me or
to indicate that the handler is a part of the script, not a command of the tell statement:
1 2 3 4 5 6 7 8 9 10 11 12
That is pretty much it, for more details on AppleScript, take a look at:
Hope this post helped you to understand the very basics of AppleScript. Give a try if you liked the verbosity of the syntax and the simplicity with which you can control other applications.