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.

416<br />

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

All the operators have a long list of synonyms. Aside from is, you could say equals or use<br />

the mathematical equals sign:<br />

else if _waitInSeconds is _timeoutInSeconds then<br />

error number -128<br />

Error -128 means “User canceled.” This will basically press the Cancel button for you after<br />

20 seconds. Although AppleScript errors in practice are often obtuse, the language actually has<br />

decently robust error handling, including try blocks and targeted error handlers based on both<br />

the type of error and the object that caused it. Errors can include numeric codes, text descriptions,<br />

partial results, and more.<br />

Since if statements and repeat statements are multiline, they require end statements.<br />

end if<br />

end repeat<br />

We are, admittedly, no AppleScript gurus. JavaScript has access to things like links, but<br />

there’s no way we know of to pass an array of links from JavaScript back to AppleScript without<br />

writing some seriously funky JavaScript. Since that’s not what we’re going for, we’re going to<br />

parse the links out manually in AppleScript. If it isn’t obvious, there are several different ways<br />

to do this.<br />

The Google image results page returns a massive string:<br />

set _searchResults to source of document 1<br />

Strings in AppleScript are basically lists. Aside from typical components such as paragraphs<br />

and words, strings have an amorphous text items component, which is defined as the components<br />

of a string as separated by the global text item delimiter.<br />

NOTE Although there is technically a list of delimiters, it’s a list of one. Thus, we talk about the<br />

text item delimiter, but the actual keyword is pluralized. We set the delimiter to a single string,<br />

but behind the scenes, AppleScript will coerce our string into a single-item list.<br />

If you examine the raw source of a Google image results page, it’s not plain HTML but a<br />

bunch of JavaScript and other goings on. The URLs we want are quoted as part of a commaseparated<br />

list of parameters, so the easiest way to get at them is to split the string by the token<br />

",".<br />

However, strings in AppleScript are also quoted, so we have to escape the quotes within the<br />

string with backslashes, setting the delimiter to "\",\"":<br />

set AppleScript's text item delimiters to "\",\""<br />

This converts the string _searchResults into the explicit list _rawResults, which we’ll loop<br />

through to filter out the URLs:<br />

set _rawResults to text items in _searchResults<br />

Although we declared _searchResults in a different try block, it’s still in scope.<br />

Since the text item delimiter is global, it is considered good form to set it back to the default,<br />

which is the empty string, "":<br />

set AppleScript's text item delimiters to ""<br />

This is a different loop construct, which enumerates the list, assigning each of its members<br />

to the named variable in turn:<br />

repeat with _text in _rawResults

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

Saved successfully!

Ooh no, something went wrong!