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 407<br />

The Google results page will have a lot of links besides the image results, so you’ll want to<br />

apply a filter. Drag over a Filter URLs action from the Internet group. This uses a rule editor,<br />

similar to the one used in Mail. Again, by experiment, we determined we want links with URLs<br />

that begin with the following:<br />

http://images.google.com/imgres?imgurl=<br />

Running the workflow will show 20 URLs, as shown in Figure 23-7. Loading one of these<br />

URLs in Safari, you can see that these don’t lead to the image directly. Rather, they lead to a<br />

multiframe Google results page that requires some clicking to get the image.<br />

Figure 23-7. Filtering URLs in Automator<br />

However, the URLs themselves contain the link to the image. Once again, you will have to<br />

turn to a simple shell script to extract the text you need. Add another Run Shell Script action,<br />

just as before, and enter this script:<br />

for url in $@<br />

do<br />

url=${url##*imgurl=}<br />

url=${url%%&*}<br />

echo $url<br />

done<br />

This script loops through the arguments and extracts the text between imgurl= and &, returning<br />

the image URL.<br />

TIP If you find it confusing having two actions called Run Shell Script, you can rename any<br />

action from its context menu.<br />

To download the image, drag a Download URLs action from the Internet group, as shown<br />

in Figure 23-8. Note that when presented with a list of inputs, rather than a single input,<br />

Automator actions will automatically loop over the list item, eliminating most of the need for<br />

this most common programming control structure.

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

Saved successfully!

Ooh no, something went wrong!