oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Automating iPhoto 2 with AppleScript

by Derrick Story, coauthor of iPhoto 2: The Missing Manual

Author's note: One of the new sections in "iPhoto 2: The Missing Manual" shows you how to apply the power of AppleScript to your iPhoto images. A particularly powerful script, "Do Photoshop Action," is covered in detail in this article. It lets you add Photoshop muscle to iPhoto's digital shoebox.

When you think about it, you use batch processing to upload your images from your digital camera to iPhoto 2. You plug in the camera, iPhoto launches, you click the Import button, and your Mac does all of the work.

This is why we love scripts -- your Mac provides the elbow grease and you get to take a short break.

But once your pictures are safe and sound in iPhoto, the labor camp is open and your mouse becomes the modern-day pickaxe. There's no batch processing in iPhoto that lets you automatically adjust those 400 vacation pictures you just brought home.

Fortunately that's not the case in Photoshop 7, which has a nifty automation tool built in, called Actions. Essentially this is a user-friendly front end that allows you to create scripts by simply recording the tasks as you do them, and then applying those actions to a new set of photos. I use these actions for reducing image dimensions, converting to grayscale, tonal adjustments, and a host of other chores that are just too boring to do manually.

When you're working strictly in the Photoshop environment, you can use the Batch command to apply an action to multiple images. Everything works great, except that you're working with folders full of pictures sitting in various locations on your hard drive -- the exact problem that iPhoto strives to correct.

In a perfect world, you'd have the organizing power of iPhoto, combined with the processing muscle of Photoshop. Well, nirvana has just appeared, and the angel delivering the good news is called AppleScript.

When the Apple engineers rewrote iPhoto 2 to better integrate with the iLife suite, they remembered to make our digital shoebox AppleScriptable. In fact, they even provided us with a handful of nifty scripts that we can download. There's plenty of fun stuff in those scripts, such as creating automated photo summaries and opening pictures in Preview. You should play with them all. But the one diamond in the rough that I want to show you today is called Do Photoshop Action.

The script isn't perfect in its original state, but you can download an improved version here that allows you to apply any set of Photoshop actions to your images residing in iPhoto.

Getting Set Up

Obviously you need iPhoto 2 and Photoshop 7. AppleScript is built right into Mac OS X, and you can find it sitting there in your Applications folder. But you also need to download the scripting plug-in for Photoshop and install it, and you should have the custom script you downloaded from this article handy too.

Create an Action in Photoshop

Now you're ready to begin some serious automation. First thing you have to do is decide what you want to automate, then create an action in Photoshop that executes it. One thing that I like to do when I give people photos from an event (such as a wedding or graduation) is to provide them with a black and white gallery, as well as the standard color offering. Black and white looks great for portraits and group shots, and many people really like the look. So I have an action that converts my color images to grayscale, resizes them, adjusts their levels, and applies a little sharpening.

Screen shot.
One of my favorite actions converts color images residing in iPhoto to black and white, then applies Auto Levels and two rounds of Unsharp Mask (at 12 percent). Check out the round button at the bottom of the window; that's the recording button for new actions. The square button to its left is the "stop recording" control.

To record the action, make sure you have the Actions window activated (Window->Actions), then open a sample image. I suggest you create a new Action Set to contain your custom actions. (You could put them in the Default Actions folder, but that gets a little messy.) To do so, simply click on the triangle in the upper right of the Actions window to reveal a pop-up menu, then select "New Set." Give your new set an easy-to-remember name, because later on, you'll have to enter that name exactly in the AppleScript.

Now you can create your first custom action. Click on the triangle again and select New Action. Give it an easy-to-remember name, and click the Record button. From this point forward, Photoshop will remember your every command until you hit the stop button.

Execute the steps you want Photoshop to retain, including the Save command (specifying the level of JPEG compression you want). Before you close the image, however, stop recording the action by hitting the square stop button located right next to the record button. For reasons I'll explain later, a "close" statement in your action wreaks havoc with your AppleScript. Congratulations! You've now recorded an action that you can apply to images in your iPhoto 2 library.

If you make a mistake while creating your custom action, don't worry. Note the faulty command and keep on working. Once you've stopped recording, simply go back and click once on the bad action item to highlight it. Click on the triangle icon and select "Delete" from the pop up menu. Your problem has been eliminated, but the rest of your action items are left untouched.

Customize Your AppleScript

Now it's time to turn your attention to AppleScript for a few minutes. If you haven't set up shop already, open up the AppleScript folder in Applications. The first thing you want to do is double-click on Script to install scripts access on your menu bar. This also gives you easy access to the folder that contains all of your scripts (see it at the bottom of the drop-down menu called "Open Scripts Folder"?

Screen shot.
If you want to add AppleScript to your menu bar, simple double-click the Script icon.

I also like to keep the Script Editor on my Dock. You can drag the Script Editor to the Dock to add it there. Now place your folder of Photoshop scripts (that you downloaded earlier) in the Scripts folder (via the menu bar dropdown menu or follow this file path: home/Library/Scripts). There's a default "Do Photoshop Action" in that folder. I recommend that you leave it alone for now, and instead add my Photoshop_Actions_Two.scpt that's ready to roll.

Now you can access that script via the dropdown from the menu Bar. But before you get to full automation, open your Scripts folder, and double-click Photoshop_Actions_Two.scpt. This will open it in the Script Editor enabling you to do a little customization. (You can later save your changes and go back to using it from the Menu Bar while you're in iPhoto 2.)

Since the adjustments to the images themselves are controlled by the Actions script in Photoshop, all you have to do is tell AppleScript which set of actions to use. When you run the script, you have the option of setting the action you want via some primitive dialog boxes that don't allow you to browse. It's a little slow to go that route when you're in a hurry.

Instead, if you know you're going to be using a particular set of actions repeatedly, just open the script (in the Script Editor) and adjsut those first two lines (as shown in the figure below), then save.

Screen shot.
AppleScript does allow you to choose your actions once it's running. But it's the slow way to go.

Screen shot.
Instead, just open the script in the Script Editor and set the default actions yourself. You can "Save as" the script with a descriptive name for that particular action.

You can run the script from the Script Editor application, or from the AppleScript menu bar icon. Open iPhoto and select a few images bye CMD-clicking on them, run the script, and watch it go to work.

Tap the Power of Photoshop

Now that you have your operation fine-tuned, it's just a matter or creating the custom actions you want to use. You can string together just about any series of Photoshop commands, but there are three controls under the Image Menu that are perfect candidates for automated photo processing. You should take a look at each of them:

  • Auto Levels: Moves the Levels sliders automatically to set highlights and shadows, ensuring that the white and black values are based on representative (rather than extreme) pixel values. I really like this one.

  • Auto Contrast: Adjusts overall contrast and mixture of colors in an RGB image.

  • Auto Color: Adjusts the overall tonal range of an image. You can set the color correction points by clicking on the Options button in the Levels dialog box. Auto Color will then use these settings to correct your images.

Note: be sure to test these on a few sample pictures before processing an entire batch.

Screen shot.
Photoshop provides you with powerful, automated image adjustments that you can use in your scripts. It's usually best just to use one at a time, though.

These controls are only a taste of what you can do in Photoshop. You also have a variety of file formats to which you can save, dozens of image filters you can apply, and you can even convert your RGB pictures to CMYK for offset printing. All you have to do is create the action, and AppleScript and Photoshop will take it from there.

Screen shot.
You also have many file formats you can automatically convert your images to in Photoshop. iPhoto only allows you to export using JPEG, PNG, and TIFF formats.

A Few Hints

You'll notice that this AppleScript saves your iPhoto images to your hard drive as separate files, and it doesn't overwrite your original iPhoto pictures. That's on purpose. Because of the way iPhoto handles files in its database, it's difficult to replace the edited clone, its thumbnail, and update the database with a simple AppleScript. Possibly a reader will contribute such a script, but for now, your images are saved in a separate folder of your choosing.

If you want to add those newly adjusted images back to your iPhoto library, simply drag the folder to the album pane, and iPhoto will import them all. One thing I like to do sometimes is create a separate iPhoto Library that's just for corrected images. (You can switch between libraries using iPhoto Library Manager 2.0 -- it's free and fully scriptable too.)

When you're creating your action in Photoshop, don't add a "close" statement to it. Unless you want to get in there and tinker with the actual AppleScript itself, a "close" statement will cause an error message.

If you're in a production department, you can use this script to adjust images and place them in a network directory accessible by coworkers. AppleScript really makes the work flow smoother.

Fine tune your actions. Usually you will only want to use one auto adjustment, such as Auto Color, per action. If you're reducing the size of your pictures using the Image Size command, then be sure to include Unsharp Mask in the process to restore crispness to your photo. Play with your actions to get them just right.

Screen shot.
You have dozens of filters you can apply using Photoshop. One of my favorites is Unsharp Mask.

Try to shoot your pictures with as consistent settings as possible for better results when you batch process your images. If you're on vacation for a week, you're not going to want to hand-correct all 400 pictures. It's much easier to upload them to iPhoto, then batch process the snaps... and have them look good without too much more fiddling around. If you need to, you can always go back and fine-tune your favorites by hand.

Final Thoughts

If you take a lot of pictures, but you don't have a lot of time to work on them, using AppleScript with iPhoto and Photoshop can really help you out. I think it's easiest to clone your favorite AppleScripts and have them labeled for each specific set of actions you use in Photoshop. That way, you all you have to do is select your photos, choose the appropriate script, and let your Mac do the work.

Derrick Story is the author of The Photoshop CS4 Companion for Photographers, The Digital Photography Companion, and Digital Photography Hacks, and coauthor of iPhoto: The Missing Manual, with David Pogue. You can follow him on Twitter or visit

Return to Mac DevCenter.