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.

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

tell application "Finder" to delete every file in (path to pictures folder)'s ➥<br />

folder "Keyword Screensaver"<br />

It’s a bit awkward but still understandable. Note the use of parentheses to clear up syntactical<br />

ambiguities and the lack of end tell for the single-line version.<br />

As expected, this displays the dialog:<br />

display dialog "What keyword shall I search for?" default answer ""<br />

The empty default answer clause gives you an empty text area for the user’s input. Without<br />

it, the dialog would have just been informational. Note that the dialog is part of AppleScript<br />

itself, so it doesn’t appear in any tell block.<br />

This strange sentence is based on the fact that result is a secret keyword for the return value<br />

of the dialog called earlier:<br />

set _keyword to text returned of result<br />

Concatenation is accomplished with the & operator:<br />

set _queryURL to "http://images.google.com/images?q=" & _keyword<br />

We’re not necessarily sure what type _keyword is, but it doesn’t matter. Although AppleScript<br />

has variable types, a fact that occasionally rears its ugly head, most of the time type coercion<br />

happens automatically.<br />

As before, this block could have been rendered as a single line:<br />

set _queryURL to "http://images.google.com/images?q=" & text returned ➥<br />

of (display dialog "What keyword shall I search for?" default answer "")<br />

Automator has several convenient Internet methods that AppleScript lacks. Instead of fetching<br />

the results in the background and returning a list of links, you have to, somewhat tediously,<br />

open Safari and load the results page:<br />

tell application "Safari"<br />

open location _queryURL<br />

There’s no way to know when the page is done loading, so you have to poll it. Since it might<br />

not actually load, you have to set a timeout:<br />

set _timeoutInSeconds to 20<br />

Since it will take at least a couple of seconds, you wait for 2 right off the bat:<br />

delay 2<br />

As mentioned, AppleScript has several looping constructs. This one iterates from one number<br />

to another:<br />

repeat with _waitInSeconds from 1 to _timeoutInSeconds<br />

AppleScript also has branching, via the if then...else if then...else construct. Although<br />

the then keyword is required, the Script Editor will fill it in for you if you forget it:<br />

if (do JavaScript "document.readyState" in document 1) is "complete" then<br />

Not unlike Automator, AppleScript can call other languages, though technically it’s Safari,<br />

not AppleScript, that provides the do JavaScript functionality.<br />

This is equivalent to the break statement in C. It is similar to, but not interchangeable with,<br />

end repeat, which ends the loop syntax.<br />

exit repeat

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

Saved successfully!

Ooh no, something went wrong!