'Basics of AppleScript: Mac OS scripting Language' post illustration

Basics of AppleScript: Mac OS scripting Language

avatar

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.

About AppleScript

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.

Development environment

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:

Applescript 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

Statements

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
-- the compiler ignores everything from -- to the end of the line
-- this code sums all even numbers of the specified array
set sum to 0 -- set variable value to 0
set theArray to {1, 2, 3, 4} -- set variable value to {1, 2, 3, 4}
    -- iterates through the list, each time one list element is assigned to the i variable
    repeat with i in theArray
        if (i mod 2) = 0 then -- checks if current value is even number
            set sum to sum + i -- sums even numbers
        end if -- closes if statement
    end repeat -- closes loop statement
display dialog sum -- displays dialog box with the result

Global variables

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
-- splits the string, default text delimiter value is ""
set theCharacters to every text item of "Hello" -- the result is {"H", "e", "l", "l", "o"}

-- sets the text delimiter value to " "
set AppleScript's text item delimiters to " "
-- splits the string
set theArray to every text item of "Hello world" -- the result is {"Hello","world"}

Another must-use feature is the result global variable:

1
2
2+3
set sum to result -- the sum value is 5

After the statement is executed, the script stores the resulting value, so it can be accessed later via the result script variable.

Handlers

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
on showDialog(message, displayTime) -- handler definition
    display dialog message giving up after displayTime -- body of the handler
end showDialog -- end of the handler

showDialog("Hello World", 1) -- calls the handler

In the example, the handler's name is showDialog, and parameters in brackets are message and displayTime.

Inter-application communication

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
tell application "TextEdit"
    make new document with properties {name:"First doc"}
    -- the value of currentDocument will be an instance of the created document
    set currentDocument to result
end tell

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 my 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
on showDialog(message, displayTime)
    display dialog message giving up after displayTime
end showDialog

tell application "TextEdit"
    make new document with properties {name:"First doc"}

    my showDialog("Hello World", 1)
    showDialog("Hello World", 1) of me
    -- showDialog("Hello World", 1) -- the wrong way to call handler inside the tell block

end tell

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.

If you're looking for a developer or considering starting a new project,
we are always ready to help!