Section 7.1. Scripting iPhoto


7.1. Scripting iPhoto

When you're just starting to script graphics programs, iPhoto is a good first step. It's Apple's "digital shoebox" program, the modern-day alternative to keeping thousands of prints in actual shoeboxes. Plus, iPhoto comes pre-installed for free on new Macs, and it's available as part of the iLife suite (www.apple.com/ilife/) for only $50.

For the scripts in this chapter, you'll want iPhoto 4 or newer.

The key to scripting iPhoto, as usual, is understanding its dictionary. The iPhoto Suite holds the information you need to get started (Figure 7-1).

Figure 7-1. The iPhoto Suite has several unique classes for handling your pictures: album, photo, and so on. Better yet, the built-in commands let you automate your iPhoto operations completely from AppleScript.


Take some time to look over iPhoto's dictionary, to get a feel for what commands and classes are available. Keep the dictionary open as you read on, too, so you can look up commands that you're curious about.

7.1.1. Accessing Pictures by Certain Criteria

Although iPhoto has a few basic features for editing photos, chances are you're going to spend most of your time organizing your pictures rather than editing them. That's why the new album command is so useful: it lets you create your own albums straight from AppleScript and add whatever pictures you want to them.

Here's how you use this command:

tell application "iPhoto"     new album name "16x12 pics" --Creates a new album called "16x12 pics"     add every photo whose dimensions equal {1600, 1200} to album "16x12 pics" end tell

When you run that script, iPhoto first creates a photo album, called "16x12 pics". Then the script searches through your Photo Library on a quest for any images that are 1600 1200 pixels in size. Finally, the script takes every image that matches those dimensions, and uses the helpful add command to copy those pictures into your new photo album.

Feel free to edit the dimensions that the script specifies. If you want to find all pictures that are 1024 768 pixelsto use as desktop pictures, for examplesimply replace {1600, 1200} with {1024, 768}.

That's not even scratching the surface of iPhoto's album-handling capabilities, though. Once you've got a set of pictures, you can change their keywords, comments, and so on, using various commands from iPhoto's dictionary. You could add the comment "Dad's birthday party" to every picture you took on your dad's birthday, for example, so you'll never forget where you took that priceless shot of him spilling coffee on himself.

Here's another possibility:

tell application "iPhoto"     new album name "16x12 pics"     add every photo whose dimensions equal {1600, 1200} to album "16x12 pics"     set the comment of every photo of album "16x12 pics" to "Cool snap" end tell

Adding this line makes sure that every 1600 1200 image ends up with the comment "Cool snap."

Frequently Asked Question
AppleScript vs. Smart Albums

OK, OK, I've got the script running. I just don't see the pointwhy would I use AppleScript to sort my pictures when I could do the same thing with Smart Albums from right within iPhoto?

iPhoto's Smart Albums feature is great for creating lists of pictures that match a certain criterion. For instance, you could create a smart album (File New Smart Album) that listed all 1600 1200 pictures; whenever you selected the album from the pane on the left, iPhoto would display an up-to-date list of all pictures with those dimensions.

Smart Albums are fine for everyday use, but AppleScript has one big advantage: power. Once you have a Smart Album set up, that's just about ityou can't do much but look at the album. On the other hand, if you use AppleScript to sort your photos, you can open those pictures in a Web browser, send them to Photoshop for editing, and perform any number of other automatic tasks. To see some of the possibilities, download Apple's iPhoto demo scripts from www.apple.com/applescript/iphoto.

There's no reason you can't use both methods to organize your photos, however. You could keep Smart Albums for pictures you just want to browse, and write AppleScripts for pictures you want to manipulate.


To read the comments of your images, select them in iPhoto and click the "i" button in the lower-left corner of the Library window. (You might have to click the button more than once to cycle through iPhoto's various information modes.)

In the Comments field, you can read all the comments that go with the image you're looking at. Or, if you'd like, you can type your very own comments to go with the imagelike, "Don't send this picture to Aunt Elizabeth," for example.

Of course, sorting and commenting your images by their dimensions is no more useful than sorting your shirts by the number of stains on them. The real power lies in AppleScript's ability to help you sort images by date, keyword, and so on.

Luckily, all you have to do is use the appropriate propertyAppleScript handles the rest. In the iPhoto Suite, take a look at the photo class's date, keyword, and title properties in particular, and just use whichever property you want to narrow down your list of pictures.

7.1.2. Getting Random Pictures

You've been taking pictures for yearsof your dog, your kids, even of your hole-infested hiking socks. By now, you've accumulated more than 10,000 pictures, in dozens of albums. What are you supposed to do if you just want to jump to a few random imageslike the iTunes's Shuffle tool, just for pictures?

Program the feature yourself, that's what.

7.1.2.1 Getting some item

To specify a random element in a list, you use the keywords some item. For instance, you could get a random composer by running this command:

display dialog (some item of {"Beethoven", "Mozart", "Britney Spears"})

To get random items from your list of pictures, though, you have to use some item in conjunction with iPhoto's own commands. You'd use a script like this:

tell application "iPhoto"     --Part 1:     activate     if not (exists album "Random Pics") then         new album name "Random Pics"     end if     --Part 2:     set photoList to every photo     --Part 3:     set numImages to the text returned of (display dialog ¬         "Show how many random images?" default answer 10)     repeat numImages times         --Part 4:         set randomPhoto to (some item of photoList)         add randomPhoto to album "Random Pics"     end repeat     --Part 5:     select album "Random Pics" end tell

Here's how the script works:

  • Part 1 brings iPhoto to the front and creates a Random Pics album, if there isn't one already. This is the album that will hold your randomly selected images.

  • Part 2 sets the photoList variable to a list of every picture in your entire Library. This is the list the script goes through to find random pictures.

  • Part 3 presents a dialog box, asking how many images you want to add to your new Random Pics album (Figure 7-2, top). Your response tells the repeat statement how many times to, well, repeat.

  • Part 4 picks out a random picture from your Photo Library, and adds the image to your Random Pics album. Since this code is contained within the repeat statement, it runs however many times you specified in the dialog box. Therefore, by the time the repeat statement is finished, you'll have exactly the number of random pictures you want in your album.

Figure 7-2. Top: Specify the number of images you want to put into your new album. Bottom: iPhoto quickly picks out random pictures, places them neatly into your new album, and brings that album forward.


Due to a bug in iPhoto, you may end up with duplicates of some pictures in your Random Pics album. That's because iPhoto doesn't check to see if a picture already exists before it adds it to an album; since your script just picks random pictures from the Library, there's a possibility your script could end up picking an image that's already in the Random Pics album. Therefore, you might have a little house cleaning to do once the script finishes running, if you want to eliminate all duplicate photos in your new album.

  • Part 5 chooses your new album from the list in iPhoto (Figure 7-2, bottom). You're now shown all your randomly selected images.

Being able to choose random pictures from a list is more than just a cool party trick: it lets you get a feel for your Library without having to scroll through the whole thing.

Power Users' Clinic
Getting Random Numbers

In addition to getting random items in a list, AppleScript lets you get random numbers. You might use this feature when creating a dice-rolling or guessing game, for example.

The trick is to use the random number command, which when provided with a number, gives you a random number between zero and that number. For instance, this command displays a dialog box with some random number between 0 and 10:

display dialog (random number 10)

That means that if you run random number 10, there will actually be 11 possible results0, 1, 2, and so on, up to 10. If you run the command again, it displays a new random number.

If you run the random number command without any number following it, though, you'll get a decimal number between 0 and 1. You might want to use that trick if your script produces a random percentage; for example:

set randomDec to (random number) set randomPerc to (randomDec * 100) display dialog "You have a ¬     " & randomPerc & "% chance ¬     of winning the lottery today."

As a side note, AppleScript's "random" numbers aren't actually random. Your Mac creates such numbers by performing a series of math operations based on the current timewhich, most of the time, is good enough to produce numbers that seem random. On the other hand, if you work someplace where true mathematical randomness is a must (say, a graduate math department), you'd be better off using a more advanced programming language, or a Web site like www.random.org/sform.html.


7.1.3. Showing Slideshows

One of iPhoto's coolest featuresand a trick sure to wow any of your Windows-using friendsis showing a full-screen, cross-fading slideshow of your pictures. When combined with the random-album script from the previous section, you can make AppleScript run a slideshow with any number of random pictures you want.

The first step is to set the preferences you want to use for all iPhoto slideshows. (Unfortunately, you have to do this in iPhoto; there's no AppleScript command for setting slideshow preferences.) Just click Organize, click Slideshow, set the image and sound settings you want (choosing crossfades and background music, for example), and click Save Settings. Then click Cancel to close the window.

Now you just have to modify your script to play a slideshow after it creates your random album:

tell application "iPhoto"     activate     if not (exists album "Random Pics") then         new album name "Random Pics"     end if     set photoList to every photo     set numImages to the text returned of (display dialog ¬         "Present how many images in the slideshow?" default answer 10)     repeat numImages times         set randomPhoto to (some item of photoList)         add randomPhoto to album "Random Pics"     end repeat     select album "Random Pics"     start slideshow --This presents your "Random Pics" album full-screen end tell

That's all there is to it! Now your script not only creates a Random Pics album, but it also shows all the pictures onscreencomplete with musical accompaniment, if you so desire.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net