7.1. Scripting iPhotoWhen 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.
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).
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 CriteriaAlthough 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.
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."
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 PicturesYou'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 itemTo 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:
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.
7.1.3. Showing SlideshowsOne 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. |