05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

414<br />

CHAPTER 23 MAC <strong>OS</strong> X AUTOMATION WITH AUTOMATOR AND APPLESCRIPT<br />

AppleScript is quite loose, with several ways to say the same thing. The problem is, this leaves<br />

you guessing at the proper way to say something, with many grammatical constructions that<br />

seem like they should work but don’t.<br />

Hence, AppleScript tends to happen organically, with each phrase leading to a compile and<br />

test. If you’re used to banging out pages of code between runs, you might want to reconsider that<br />

habit, or learning AppleScript is going to be a painful experience.<br />

Analyzing the Code<br />

Although AppleScript and Automator accomplish similar things, they don’t exactly translate<br />

back and forth, so we have to do some things a little differently. It’s probably best to just run<br />

through the code sample and point out some sights along the way.<br />

Comments in AppleScript are preceded by --, like so:<br />

-- Remove any existing images<br />

Since it is mainly concerned with controlling applications, the central syntax element of an<br />

AppleScript program is the tell block:<br />

tell application "Finder"<br />

Each application’s vocabulary is contained in a special dictionary file. When working with<br />

an application in AppleScript, the first thing you should do is open its dictionary by selecting<br />

Open Dictionary... from the File menu or by typing Shift+Cmd+O.<br />

Variables can be named on the fly. Their values are set with the set...to construct:<br />

set _imageFolder to path to pictures folder<br />

There is no real way of telling what’s a variable and what’s a keyword, so we like to precede<br />

our variables with an underscore. That’s just a personal preference, though.<br />

The path to construct locates known folders. In this instance, we’re trying to get a path to<br />

the user’s Pictures folder, which in UNIX terms we would call ~/Pictures.<br />

Literal terms are enclosed in quotation marks, as are strings:<br />

set _imageFolder to folder "Keyword Screensaver" of folder _imageFolder<br />

Most objects on the system are considered containers for other objects. A folder that’s inside<br />

another folder is of that folder. The of form can be extended to a ludicrous degree. You could<br />

refer to a character of a word of a paragraph of a document of an application, and so forth. You<br />

can also use the possessive form:<br />

set _imageFolder to _imageFolder's folder "Keyword Screensaver"<br />

AppleScript is a lot like LISP in that its only real data structure is the list. This underlies constructs<br />

like this:<br />

delete every file in _imageFolder<br />

You can also refer to things like the first file in the folder or file 3 in the folder. AppleScript<br />

also has explicit looping constructs, as you’ll see later.<br />

AppleScript is not whitespace sensitive, but it does use newlines to end its statements. Multiline<br />

syntax structures, such as tell blocks, require end statements:<br />

end tell<br />

As a testament to the flexibility of the AppleScript language, the entire first tell block could<br />

also have been written as a single line:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!