Although the Mac is prized for its ease of use, repetitive tasks such as changing large groups of file names and creating workflows that span several applications can be anything but easy. Sooner or later, you’ll want to automate an action or series of actions to reduce your toil and increase your productivity—even if you’re simply reducing the number of steps you must take to reply to an e-mail message. Software that does some of these things for you is available—usually designed for a specific purpose or application, and often for a price. But luckily, Mac OS already has the ultimate automation solution: AppleScript. It’s easy, powerful, and free—and it’s already on your computer.
At first sight, learning to script may seem daunting. But with a little guidance, you can be on your way to automating many of your computing tasks. In this introduction to AppleScript, we’ll take a look at the fundamentals of understanding and writing AppleScripts, and then we’ll walk through the creation of a sample Finder script. (This tutorial is written for Mac OS X 10.1 or higher and will not work with earlier systems.)
What Is AppleScript?
AppleScript is a language used to automate the actions of Mac OS and many of its applications. Whether a task is as simple as copying a file or as complex as building a catalog, AppleScript can intelligently perform the requisite actions for you, controlling applications and making decisions based on its observations or on information provided by its interaction with the person running the script.
Who Uses It
Every day, businesses and individuals use AppleScript to create newspapers and books, manage networks, build DVDs, process images, generate Web pages, back up files and folders, make videos, and much more. In addition to loads of Apple applications such as iTunes, AppleWorks, Mail, and Terminal, many third-party applications—including QuarkXPress, Microsoft Entourage, Adobe Photoshop, FileMaker Pro, and Qualcomm Eudora—can take advantage of AppleScript.
How It Works
You create AppleScripts with Apple’s free Script Editor application, using the AppleScript language. Each script contains a series of sequential instructions. To automate actions, you launch a script; your Mac communicates those instructions to your applications, which then perform the actions as instructed.
Scripts can be launched, or
, from within Script Editor, or they can be saved as either script files or script applications that are available from within other applications. You can access script files from an application’s script menu or from Mac OS X’s systemwide Script Menu utility. You launch script applications, or
, just as you do other applications—by double-clicking on their icons in the Finder or by clicking on their Dock or Finder toolbar icons.
Nuts and Bolts
AppleScript is based on the concept of scriptable objects belonging to or being contained in other scriptable objects—it’s sort of like having sets of nested Russian dolls inside your Mac. A file sits in a folder, which belongs to the hard disk; or a word is part of a paragraph, which belongs to a story. Understanding the relationship between scriptable objects is a key to learning how to write scripts.
All Together Now
Consider the following statement:
On the Macintosh computer, everything is an object
. The computer, the desktop, the disks it displays, the folders on the disks, and the files in those folders—all of these items are objects. So are applications, their documents, and the data in those documents.
Now consider this:
Everything belongs to, is related to, is contained in, or is part of something else.
The file is in the folder, which is in the disk, which is on the desktop, which is on the computer. This relationship can apply to text as well:the letter is in the word, which is in the line, which is part of the paragraph, which is contained in the story.
This relationship between objects is referred to as hierarchical or, in military terms, the chain of command. AppleScript uses this hierarchical structure to identify particular scriptable objects, and it will be in the scripts you write. Just remember that in scripts, objects are described in terms of their position in the hierarchical structure.
Properties and Values
Disks, files, folders, font suitcases, packages, and windows are all objects or elements belonging to the Finder application. They are the items the Finder uses in the organization and display of information. Each of these items has particular
that define or describe it. Some of these properties are unique to each item, while others are shared by all items.
For example, while an Internet location file (or link) is the only Finder element that has a property describing a location on the Internet, it has some properties common to all Finder elements, such as its icon’s size and position in a folder window or on the desktop. And like other Finder elements, such as a folder or document file, a link file has a name property whose value is displayed with its icon and can be edited by the computer user.
An important rule to remember is that every scriptable application contains elements or objects that have properties. These properties have values that can be read or manipulated.
This rule applies to the Finder application, as well as all scriptable applications. All elements of the Finder have properties—for example, their name, size, and location. And all these properties have values, some of which can be edited and some of which can only be read.