Benjamin Ragheb ([info]benzado) wrote,
@ 2008-02-27 12:59:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Re: Tip: Prevent iPhoto from opening when you plug in your iPhone
A post by Sam on 37signals explains how to write a script so that iPhoto will automatically launch when you plug in your camera but not your iPhone. Sam's script checks the list of USB devices connected to the system and only launches iPhoto if your camera is connected. That method doesn't work so well if you use a USB card reader, since it appears in the list as "USB Storage Device", the same name given to any USB disk. Using Sam's method, iPhoto would open if I connected my iPhone while I had any USB disk connected to my computer.

To overcome this problem, I wrote a new script that finds the name of the last USB device connected, which presumably is the device that triggered the system to execute the script. The script launches iPhoto except when the last device connected is an iPhone. Here is the source:

on get_device_name()
    set list_devices to "ioreg -Src IOUSBDevice | grep '^\\+'"
    set get_name_of_last to "tail -n 1 | sed 's/^\\+-o \\(.*\\)@.*/\\1/'"
    set command to list_devices & " | " & get_name_of_last
    return (do shell script command)
end get_device_name

on run
    if get_device_name() is not equal to "iPhone" then
        tell application "iPhoto" to activate
    end if
end run


Paste that into Script Editor, save it as an application, then go to the Image Capture preferences and set the script application to be launched whenever a camera is connected. You don't need to do all the Terminal stuff that Sam requires in his post because we don't need to know your camera's USB name.

P.S. The comments on the original post make for a fascinating case study in people aggressively missing the point.



(10 comments) - (Post a new comment)


(Anonymous)
2008-03-11 04:53 pm UTC (link)
I slightly modified this script to work with Aperture and it still opens on my MacBook Pro with OS X 10.4.11 and my iPhone connected. I was under the impression that the app in the script is not supposed to open at all if the iPhone is connected. Is this how the script is supposed to act?

Otherwise, thanx for the script!

~Scott

(Reply to this) (Thread)


[info]benzado
2008-03-11 05:20 pm UTC (link)
Using my script:

If the iPhone is connected, and then you also connect your camera, Aperture should open.

If the camera is connected and you don't already have Aperture open, and then you connect your iPhone, Aperture should NOT open.

The script assumes that the system is returning USB device names in the order they were connected, which may not be the case. If you are seeing different behavior then that assumption might be wrong.

(Reply to this) (Parent)(Thread)

Device Order
(Anonymous)
2008-06-04 01:39 am UTC (link)
I noticed the order the devices are returned is NOT the order that they were connected, but I did observe that the sessionID attribute increases as devices are connected. With that in mind, I tweaked the magic command line code to return the device name with the highest sessionID. It seems to work well for what I have tested so far:

ioreg -Src IOUSBDevice | egrep '(^\+)|(sessionID)' | sed 's/^ | "sessionID" = /:/' | sed 's/^\+-o \(.*\)@.*/~\1/' | awk 'BEGIN{FS=":"; RS="~"} {sub(/\n/,"",$1); sub(/\n/,"",$2); print $2"\t"$1}' | sort -rn | head -n 1 | awk 'BEGIN{FS="\t"} {print $2}'

This still needs to be put in to an AppleScript friendly format, but works great from just testing from Terminal. Hope this is helpful for someone..

Enjoy,
-Anson

(Reply to this) (Parent)(Thread)

Re: Device Order
(Anonymous)
2008-06-04 01:55 am UTC (link)
Sorry, I should have used a code block; there is an important spacing issue when replacing the sessionID header:

ioreg -Src IOUSBDevice | egrep '(^\+)|(sessionID)' | sed 's/^  |   "sessionID" = /:/' | sed 's/^\+-o \(.*\)@.*/~\1/' | awk 'BEGIN{FS=":"; RS="~"} {sub(/\n/,"",$1); sub(/\n/,"",$2); print $2"\t"$1}' | sort -rn | head -n 1 | awk 'BEGIN{FS="\t"} {print $2}'

That should definitely work better now. :)
-Anson

(Reply to this) (Parent)(Thread)

Re: Device Order
[info]benzado
2008-06-04 05:27 pm UTC (link)
That is a crazy complicated command line! So far getting the last line has worked well enough that I don't feel the need to change it. For my purposes, this is the sort of script where "good enough" is good enough.

(Reply to this) (Parent)

Didn't work for me
(Anonymous)
2008-09-03 09:53 pm UTC (link)
Hey -

Thanks for the helpful post. I did what you said, and set it in my prefs in Image Capture, but when I reconnected my iPhone, it still launched iPhoto, though it did ask me at the start if I wanted to use a different app.

Am I missing something?

Thanks!

(Reply to this) (Thread)

Re: Didn't work for me
[info]benzado
2008-09-03 10:00 pm UTC (link)
Hard to say. Are you sure you saved the script as an application, and not as a compiled script?

(Reply to this) (Parent)(Thread)

Re: Didn't work for me
(Anonymous)
2008-09-03 10:28 pm UTC (link)
Don't think so - saved it as a .app. I just copied it in again and hit 'Run', and it popped open iPhoto...

Do the 'Run Only', 'Startup Screen' and 'Stay Open' boxes need to be checked?

(Reply to this) (Parent)

Rock...
(Anonymous)
2008-10-30 08:40 am UTC (link)
Beautiful. Much better than the original script—there was a good idea there, but this is the (now) obvious way it should have been implemented. Works a charm. Thanks.

(Reply to this)


[info]eagle243
2009-08-11 05:18 pm UTC (link)
I seem to recall seeing a screenshot of an app that did something like this, but I am unable to locate that screenshot again; does an app exist for this now?

(Reply to this)


(10 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…