Whatever happened to Benjamin Ragheb? - Re: Tip: Prevent iPhoto from opening when you plug in your iPhone

> Recent Entries
> Archive
> Friends
> Profile
> benzado.com

Comics I Like
Achewood
Overcompensating
Wigu
The Perry Bible Fellowship
Bands I Like
The Spaceshots
The Lowest of the Low
Stuff I Do
Fat Penguin
Vulva Van

February 27th, 2008


Previous Entry Share Next Entry
12:59 pm - 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.

(read 12 comments | write a comment)

Comments:


From:(Anonymous)
Date:March 11th, 2008 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
[User Picture]
From:benzado
Date:March 11th, 2008 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.
From:(Anonymous)
Date:June 4th, 2008 01:39 am (UTC)

Device Order

(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
From:(Anonymous)
Date:June 4th, 2008 01:55 am (UTC)

Re: Device Order

(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
[User Picture]
From:benzado
Date:June 4th, 2008 05:27 pm (UTC)

Re: Device Order

(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.
From:(Anonymous)
Date:September 3rd, 2008 09:53 pm (UTC)

Didn't work for me

(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!
[User Picture]
From:benzado
Date:September 3rd, 2008 10:00 pm (UTC)

Re: Didn't work for me

(Link)
Hard to say. Are you sure you saved the script as an application, and not as a compiled script?
From:(Anonymous)
Date:September 3rd, 2008 10:28 pm (UTC)

Re: Didn't work for me

(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?
From:(Anonymous)
Date:October 30th, 2008 08:40 am (UTC)

Rock...

(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.
[User Picture]
From:eagle243
Date:August 11th, 2009 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?
From:(Anonymous)
Date:September 5th, 2010 05:50 pm (UTC)

This isn't working!

(Link)
I loaded the following script as an app:

try
do shell script "ioreg -Src IOUSBDevice | grep 'Camera'"
activate application "iPhoto"
end try

It looks like it wants to work and even does when i plug in my iPad and/or iPhone; neither of those launch iPhoto....PERFECT!!

The problem comes in when I plug in my camera. It launches iPhoto (so far so good) but then it asks me the following question:

"Do you want to use iPhoto when you connect your digital camera?"

My only options are "Decide Later", "No" and "Yes".

If I select "Yes" then it automatically updates "Image Capture" preferences with IPhoto thereby setting everything back to normal and not using the above script.

What should I do?
[User Picture]
From:benzado
Date:September 6th, 2010 12:38 am (UTC)

Re: This isn't working!

(Link)
Select "No", so that iPhone won't change the setting.

> Go to Top
LiveJournal.com