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.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20080301133957211