Launch iPhoto only on camera, not iPhone, connect

Mar 10, '08 07:30:00AM

Contributed by: darick

I have been continually annoyed with the way that iPhoto would start whenever I plugged my iPhone into my computer. The only pictures I take with my iPhone are to help me find my space in a parking lot, or so I can reassemble something I am taking apart. I do not want to archive these pictures. However, I do still want to use iPhoto to take the pictures off my digital camera. My previous solution was to set Image Capture to disable launching iPhoto in every case of camera attachment, and then manually launching iPhoto when necessary.

Luckily, Sam Stephenson at 37signals provided me with a solution that will launch iPhoto when a camera is attached, but not when my iPhone is attached. Sam realized that within the preference pane of Image Capture, an arbitrary application can be set to launch whenever a camera is connected. This solution utilizes the shell command ioreg combined with grep to search through the USB devices connected to the computer.

The script is able to sort through the connected devices and see if a specific device is connected. If that device is connected, iPhoto will be launched. Since the application is set to run whenever a camera is connected, it provides a method of sorting between the iPhone and a digital camera. Sam does a really great job of explaining every step for those not proficient with the terminal or scripts.

While this is quite useful as is, I wanted a slightly more generic form. I wanted to launch iPhoto for every camera that was connected, but never for my iPhone. Instead of searching the I/O Kit registry for a specific camera, I decided to search for any entry containing the word Camera.

My first plan was to just edit Sam's script such that it would look for the word Camera and when found, launch iPhoto. That code looks like this:

--initialize variable to a known value
set cameraName to "0"

-- perform script to see if there is any attached USB
-- device that has the word "Camera" in its name
try
  set cameraName to ¬
   (do shell script "ioreg -Src IOUSBDevice | grep 'Camera'")
end try

-- if there is a camera attached, start iPhoto
if length of cameraName is greater than 6 then
  activate application "iPhoto"
end if
Upon realization that grep throws an error if it cannot find what it is searching for, I decided to attempt a minimalist script. Below is a version that is slightly less intelligible by utilizing try to perform the logic.
try
  do shell script "ioreg -Src IOUSBDevice | grep 'Camera'"
  activate application "iPhoto"
end try
This works because if grep throws an error (i.e. the word Camera wasn't found), then the try routine is terminated before the activate... line is reached.

After copying/pasting either of these scripts into Script Editor and saving as an application, one can go to the Preferences in Image Capture and set this application to open when a camera is connected. This can be done by clicking on Other in the drop down menu, then selecting the application that was just created. Image Capture is located in the Applications folder.

I was even able to snag a snazzy icon from within the iPhoto 6 bundle to make my script look a little more professional. By control-clicking on iPhoto.app, choosing Show Package Contents and navigating to Contents » Resources, I found an image I like called import_camera.tiff. This can be set as the icon for the application by first opening import_camera.tiff in Preview and copying it to the clipboard (Command-C). Next, click on the application and bringing up the Get Info window by pressing Command-I. The process is completed by selecting the generic script icon in the upper left (with a click) and pasting the clipboard contents (Command-V) over the top.

This works well for me, but you might find Sam's specific method much more useful -- it depends on your situation. Both methods have issues when the camera and iPhone are connected at the same time.

[robg adds: This worked as described (both versions) in my testing.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080301133957211