Apple-Bug-Friday: iPhoto AppleScript Hang

I’ve been waaaay behind in posting my bugs, but here’s one that I tracked down a while ago and just finally put together some sample code for. Before I log it as an actual bug, though, I’d love for somebody else to verify that it breaks for them, too.

The Problem: In Mugshot, I use an AppleScript to import some photos into iPhoto, and then update those photos with the appropriate titles and comments (which I pull from Flickr). In my initial tests, iPhoto kept hanging, which meant the script was hanging while it waited for a response, which meant that Mugshot hung. It didn’t happen all the time, especially if I was just downloading a single photo. But with some tips from Daniel Jalkut, I finally tracked this down to a timing issue - if I put some delay statements before each attempt to set titles and comments, it worked fine. I tried a couple different combinations and it looks like iPhoto hung after most attempts to do anything after an import.

Sample Code: Here is a sample script and two sample photos. From my tests, it looks like you can use any image files - although iPhoto won’t always hang if you’re importing a single photo. Some requirements:

  • I only tried this on iPhoto ‘05. I don’t know if it’s a problem in earlier versions. But if Apple releases iPhoto ‘06 next week, I’ll try it :)
  • Please open the .scpt file before running it - you’ll need to update the paths used for the photos or the test won’t work.
  • You will also need to add an album name to the script, or create one in iPhoto called “HangTest”, which is what the script expects. My Mugshot script will actually create the album if it doesn’t exist, but I took that out of the test script for simplicity’s sake.
  • I don’t think iPhoto has to be running for this to work, but you may want to try it both ways and let me know if there’s a difference.
  • After running the script once - whether it completes successfully, hangs iPhoto, or you get an error, you should delete the imported photos from your library AND empty the trash before running the test script again. The sample code does a very simple search to try and set titles - and if previous imports worked, it may not be able to find the photos it’s looking for. (It searches for files with a specific filename in the specified album - if you move the photos out of the album, but don’t delete them from the library, the new imports will get a “_x” appended to the filename, where “x” is a number - and the script won’t be able to find the files using the filenames it expects.)

I’m going to start writing this up as a bug, but would love to make sure that it hangs for somebody else… so let me know if it does for you as well, along with any other details, like OS version, iPhoto version, etc.

6 Responses to “Apple-Bug-Friday: iPhoto AppleScript Hang”


  1. 1 michaelv Jan 10th, 2006 at 5:34 pm

    Works in iPhoto 6.0, with the caveat that I had to put a delay statement between the second and third lines in the tell statement (the one that starts with “import from..” and “set the_photo to…”, otherwise I’d see an error that ‘the variable the_photo is not defined’.
    Hope that helps.

  2. 2 blake Jan 10th, 2006 at 11:15 pm

    That’s exactly what I wanted to hear - and that means the bug still exists in iPhoto ‘06. (I assume you tried this on a Macworld machine?!). Mugshot’s code has a delay statement before each photo search statement - and that’s the only thing that keeps it from hanging.

  3. 3 michaelv Jan 11th, 2006 at 6:51 pm

    So on iPhoto 6, the script didn’t hang iPhoto, it (the script) just returned an error at the point where it tried to do something with the not-yet-existent ‘the_photo’. I wanted to clarify that.

    As to the rest, __as I understand it__, the ‘bug’ (having to put delay statements everywhere) isn’t really in iPhoto per se, but partly the way applescript works, and partly just timing — it takes a while for the imported images to a) be imported, b) get properly ‘registered’ with the application and library. Since importing happens asynchronously in iPhoto (so you can do other things while you’re importing), i think the applescript ‘import’ command returns immediately. This happens all the time in applescripts / apple events– not just in iPhoto.

    Given something like:
    import from _somewhere to the_album
    set title of the first photo to “blah”

    iPhoto starts importing, and the import is gonna take a few seconds or minutes or whatever. But the script doesn’t check to see if the import has completed, and the import command returns immediately. Since apple events such as those you’re using don’t wait around for a return value (i.e., “OK, I’ve imported the photos.. what now?”), the next line in the script gets executed without regard to the outcome of the first. In this case iPhoto hasn’t finished importing the images when the next line tries to do something with one of those images. You could change this by adding in some checking, try blocks, etc. (maybe you do this already in your production scripts?) There’s some documentation (i think…?) on the apple developer site about error checking and handling exceptions, etc, in applescripts.

    Also, applescript is slow . . . To me, it’s kind of like “snail-mail” — things take a while to get and respond to messages. What’s taking so !@#$ long!!???

    In case it’s not obvious, I’m no applescript expert. I have a love-hate relationship with it– mostly hate, really. But I have had to use it in my work and have had to use some parts of iPhoto’s (meager) script support. I’d rather be writing cocoa code!

    Hope that helps… you can ping me offline if you like.

  4. 4 blake Feb 7th, 2006 at 1:01 pm

    To be honest, it’s not a very obvious bug. And the behavior has changed pretty significantly in iPhoto ‘06. I haven’t tested it yet or updated the scripts, but I think the next code change to the scripts will need to detect which version of iPhoto a user has and do the right thing from there.

  5. 5 Didier May 10th, 2006 at 5:26 am

    You should consider a bug as described here :
    Photos Won’t Import Into iPhoto

    I had the same problem, and finally fixed it this way :

    property theFolder : "HardDisk:Library:Desktop Pictures:Nature:"
    property album_name : "TestAlbum"

    tell application "iPhoto"
    -- activate
    set exAlbum to exists album album_name
    if exAlbum then
    set myAlbum to album album_name
    else
    set myAlbum to (new album name album_name)
    end if
    select myAlbum
    set photoList to {}
    set thePhotos to list folder theFolder without invisibles
    repeat with eachItem in thePhotos
    set newPath to POSIX path of (theFolder & eachItem)
    copy newPath to the end of photoList
    end repeat
    tell myAlbum
    import from (photoList)
    end tell
    end tell

    Hope it helps.

    D.S.

  1. 1 Coyote Highway » Blog Archive » This is broken… Pingback on Feb 7th, 2006 at 12:15 pm

Leave a Reply