Wed, 13 Dec 2006

Workflow AppleScripting: Part 1

David LeBer

AppleScript is an indispensable tool for automating your Mac. It's been part of the Mac OS since System 7 and over the next few weeks we are going to examine AppleScript and how you would use it automate your workflow. We'll look at the AppleScript components, workflow analysis, candidate identification, and finally a real world-case study. This week we'll start by looking at the fundamentals of AppleScript and some resources for mastering the language.

NEWS FLASH: We've added a thin AppleScript Tutorial to give you a feel for the language. If this article makes you want to get your hands dirty give it a try.

AppleScript Basics

According to Apple, "AppleScript is an English-like language used to write script files that automate the actions of the computer and the applications that run on it." You will hear the term "English-like" used quite regularly to describe the AppleScript language, so what exactly does that mean? Well, lets look at an example:

tell Application "Finder"
   open every file of desktop whose name begins with "openMe" 
end tell

Let's examine the contents of this script:

The first line and the last line define what is known as a Tell Block. Tell blocks are used to target the actions of a script. In this case all of the commands inside the tell block will be directed at the Finder.

Inside the the tell block, we can tell the Finder what we want it to do, and in this case, it is pretty clear what that will be. The script is going to ask the Finder to open every file [on the] desktop whose name begins with "openMe". This example should make it obvious why AppleScript is referred to as having an "English-like" syntax.

AppleScript Features

AppleScript is a rich language. It contains all of the things you would want in a programming language including variables, conditionals (if, else, then statements), looping (or iteration) and subroutines (handlers). Unlike macro languages that lock you into a prerecorded set of steps, AppleScripts are much richer. They can possess the smarts to make decisions based on user input, current conditions, or the results of previous steps.

AppleScript Parts

AppleScript is built into Mac OS X, and contains a core set of commands and objects. It's power however, comes when used in conjunction with AppleScriptable applications. For instance the script example we looked at previously relies on the Finder to do all of the work. The Finder is a highly scriptable application and it is not alone. Quark Xpress, Adobe Indesign, BBEdit and Fetch are just a few of the many applications that have rich AppleScript Dictionaries

The Object Model

When a developer commits to adding AppleScript support to their applications, they are exposing an Object Model of the kinds of object they work with to your scripts. That means we can issue commands to the Finder like open every file of desktop whose name begins with "openMe" and know it will do what we want. Without a well implemented object model we would have to ask for all of the files on the desktop, and then iterate through each one checking to see if it's name matched our criteria. The Finder's object model and the whose clause in the script did all of that for us. Note: It must be said that not all AppleScript implementations are equal, some applications do it far better than others. So: "You're milage may vary, void where prohibited by law, for external use only and if swelling persists please consult a doctor."

Where To Start

If you've installed OS X on your Mac you have everything you need to start learning AppleScript. Go to /Applications/AppleScript/ and you will find the Script Editor application. This is a fine script editor to get you started (you'll find a list of others below). Start with one of the tutorials or books lited below and also be sure to check the sample scripts in the /Library/Scripts/ folder for plenty of examples.

Tutorials

Books

Documentation

Community

Tools

Coming Up Next

Next week we will look at how to decide where to apply our new found AppleScript prowess as we dive into workflow analysis. Deciding where to automate is as important as how to automate, and that's what we'll examine next. So until then....

[/AppleScript] Static link

Content Image

Recent Articles:

Get notified when there are new articles.

News + Opinion

We're not the only ones with bandwidth and a need to share. Here are some of our favorite technical resources on the web.

MacJournals

We've been lifetime subscribers to MDJ for, like, ever. Insightful, biting and timely. Well worth the cash.

Versiontracker

Need software? Versiontracker will help you find it.

MacMinute

High on news, low on press releases. I like the layout too.

Daring Fireball

Great layout, like the writing style. Gruber writes for MacJournals too.

Mac OS X Labs

Need to learn about the deeper levels of Mac OS X? Mac OS X Labs can help.

MacWindows

Heterogeneous, Heterogeneous, Heterogeneous!