Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Introduction to AppleScript: What you can do with AppleScript, OS X's scripting language

Kenny Hemphill | Aug. 31, 2015
Understand and get more from AppleScript.

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.

image1
Macworld UK 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.

 

1  2  3  Next Page 

Sign up for CIO Asia eNewsletters.