05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

This is analogous to the for...in loop found in Objective-C and other languages.<br />

Since we’re looking for a URL, we know it should start with http://:<br />

if _text starts with "http://" and _text does not end with "/images" then<br />

By observation, all the URLs in the source are either images or internal links that end with<br />

the directory images. Since it’s easier to filter out a single word than it is to test for all the different<br />

permutations of all the file extensions of all the image formats, we did it this way. Your<br />

mileage may vary.<br />

To test for the substrings, you can use the convenient string shortcuts starts with, ends with,<br />

and contains. Of course, we said end with, not ends with, but they mean the same thing. Apple-<br />

Script has a lot of meaningless words you can add to make it seem more like grammatical<br />

English. For example, the word the and the pluralized verb form are both ignored by Apple-<br />

Script.<br />

There are a lot of ways to download a file, but the most reliable is the UNIX program curl.<br />

It’s also a good chance to show off AppleScript’s own do shell script command:<br />

do shell script "cd ~/Pictures/Keyword\\ Screensaver;<br />

curl -sO " & quoted form of (_text as string)<br />

Note the double backslash, which will be translated into a single backslash when it’s sent to<br />

UNIX, which will, in turn, escape the space in the file name Keyword Screensaver.<br />

We used a semicolon to string multiple commands together. We explicitly cast the _text variable,<br />

which is a reference to a list item, to a string using the as keyword. We then converted that<br />

string to the quoted form, which basically means “make sure this is going to be something UNIX<br />

will not choke on” before concatenating it to the invocation for curl.<br />

AppleScript will pause while curl downloads all the images so we don’t have to bother<br />

polling it as we did with Safari. If we really wanted to be clever, we could use curl to download<br />

the results page as well and use grep to parse out the URLs. But, if we were going to do all that,<br />

we might as well write this tutorial in Perl.<br />

Finally, we activate the screen saver and call it a night:<br />

tell application "ScreenSaverEngine"<br />

activate<br />

end tell<br />

Automator vs. AppleScript<br />

Given the choice between Automator and AppleScript for your automation needs, which one<br />

should you choose? These two tutorials should give you a pretty good idea of the strengths and<br />

weaknesses of each. In general, we’d say Automator is easier to use, but AppleScript is more<br />

powerful. Even with the features added in <strong>Leopard</strong>, such as variables and basic looping,<br />

Automator lacks the kind of control that AppleScript provides.<br />

Still, for our dollar we’d pick Automator. You can pass things off to shell scripts, just like in<br />

AppleScript. You can also call AppleScript from Automator, giving you the best of both worlds.<br />

Mostly, though, if we wanted to program, we’d use Objective-C, which we’ll get into in the next<br />

few chapters.<br />

The biggest reason to use AppleScript is being comfortable with it. As with Automator,<br />

AppleScript can be compiled into applications. Apple’s developer tools, Xcode and Interface<br />

Builder, have a feature called AppleScript Studio that lets AppleScript fans write graphical applications<br />

entirely in AppleScript.

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

Saved successfully!

Ooh no, something went wrong!