AppleScript is OS X's naive scripting language and allows you to automate repetitive operations which involve one or more applications. Imagine the time you could save! And the boredom you can avoid!

AppleScripts are triggered by events. These can take a number of different forms, such as the output from an application, a file being placed in a folder, or a file dropped onto a droplet on the desktop.

AppleScripts can also be triggered by Automator, OS X's tool for creating automated workflows. In this case, you'd add an item to a workflow that tells Automator to run a script and place the script in the item's text box.

What can you do with it?

You can use AppleScript with any application, at a basic level. In order to fully control an application with AppleScript, however, the app has to explicitly support it and provide a 'dictionary' of commands that scripts can use to perform operations in the application.

An example of the importance of these dictionaries was provided in late 2013, when Apple released versions of its iWork apps that had been completely re-written. Unfortunately, the re-written apps had AppleScript dictionaries which were much smaller than their predecessors and so significantly limited the way in which the applications could be controlled with scripts. Thankfully this was remedied within a few months and all the AppleScript terminology from the previous versions of iWork apps was added back into the new ones.

How to use the AppleScript Editor

Editing an AppleScript using Script Editor.

AppleScripts are written in Script Editor, an app which lives in your Mac's Utilities folder.

There are two ways you can create scripts in AppleScript Editor. You can record actions you perform on your Mac, using AppleScript compatible apps, or you can write them from scratch.

To get an idea of how recording scripts works, launch Script Editor and select New Document. Click the Record button on the toolbar. Now, click on the Desktop, Press Cmd+Shift+N to create a new folder, and give it a name, say 'My New Folder.' Press stop to end the recording. You'll see the scripting window populate with the script that describes the events you just recorded. You just created your first AppleScript.

Before you can do anything with it, however, you need to compile it. Compiling an AppleScript checks the syntax and prepares the script for use. When a script has compiled successfully, the text in it will be colour-coded to make it easier to read. If it doesn't compile successfully, AppleScript Editor will report an error and you'll have to go back and check where you went wrong. Press the compile button (the one with a hammer icon on it) and save it.

Easy, huh? Sadly not all AppleScriptable applications support recording and those that don't allow you to record every action. Often the only way to find out if an app is recordable and what it allows you to record is to try it.

Mail allows interaction via AppleScript

Write an ApplScript script yourself

A better solution, then, is to write the script yourself. Thankfully, it's not too difficult, once you've learned the basics of AppleScript's syntax.

The first thing you need to do in any AppleScript is 'tell' it what application you want to use. Handily, this, like most AppleScript commands, is done inside a 'tell' block. So, in the example above, the script for creating 'My New Folder', we start with:

tell application "Finder"

Commands in AppleScript are, as far as possible, plain English, making them relatively easy to read and write. There are, however, rules to be adhered to, otherwise your script won't compile. One of those is that every 'tell' block must end with 'end tell.'

So the complete script for our example of making a new folder on the desktop called 'My New Folder' is this:

tell application "Finder"
    make new folder at desktop with properties {name:"My New Folder"}
end tell

The make command can also be used to create files and aliases. And by changing 'desktop' and "My New Folder" to whatever you want, you can change the location and the name of the folder.

Variables in AppleScript

Variables in AppleScript do the same job as they do in other scripting and programming languages – they store data so you can call on it later. The data can be specified in the code or it can be the result of a calculation or user input. So, in our folder example, we could declare variables for the name and location of the folder like this:

set folderName to "My New Folder"
set folderLocation to desktop

After declaring the variables, we'd write the  script like this:

tell application "Finder"
    make new folder with properties {name:folderName, location:folderLocation}
end tell

In this example, declaring and using variables doesn't help us because the variables are only used once and so the effort in creating them outweighs the benefit. Where you need to use the same data multiple times, however, declaring variables at the start means that if you wanted to change, in this example, either the name or the location of the file, you'd on'y need to change one line of code for each variable, rather than every instance of that variable. And where a section if script takes the output from a previous calculation or user interaction, using variables is essential.

Organising scripts in AppleScript

A significant number of applications have AppleScript Dictionaries

Once you start writing scripts that contain more than a handful of lines, you'll realise that hunting bugs and editing sections is much easier if you organise your code into sections with descriptions. These descriptions are known as 'comments.' To add a comment to an AppleScript, type two dashes then the text of your description, followed by another two dashes. When you compile the code, the comments are coloured grey and ignored when the script is run. If you need to, you can use commenting to prevent sections of code from being run – if you're trying to find bugs, it's more efficient to do that than delete the code and re-insert it later.

AppleScript is hugely powerful, and not nearly as difficult to learn as you might think. If this very brief introduction has whetted your appetite, fire up AppleScript Editor, go to the File menu and choose Open Dictionary to see a list of applications on your Macs with AppleScript Dictionaries. Then go to Apple's AppleScript developer resource to learn how to use them.

More like this:

Ten fun Terminal projects

How to use Terminal on the Mac

Manage Macs on a Windows-based network

Mac tricks: 10 Things You Didn't Know Your Mac Could Do