Set Photo Dates with Apple Script

6:00 pm in Programming, Technology by Gaz

I’ve been pretty annoyed about how dire my Motorola V3i phone is for several months now, although I did manage to work around one of the phone’s stupidities: Although the phone knows the time and date when you use it to take a picture, rather than set the timestamp correctly in the metadata, it just creates a stylised filename from the date and time. The bad thing about that is that iPhoto can’t tell when the photo was taken as it imports it, so it guesses by setting it to the time of export… unfortunately, that means dragging and dropping a block of photos from the phone essentially randomises their order in iPhoto.

Trying not to sleep on the plane back from San Diego a few weeks back (to minimise the effects of jet lag), I wrote my first ever Apple Script to parse the file names and use that data to tell iPhoto to correct the timestamp. It’s probably not an elegant way to do it, but I only had the bundled help files and an hour of battery charge left…

tell the application "iPhoto"
  set v3i_photos to photos of photo library album where title contains "-07_"
    
  repeat with aphoto in v3i_photos
    set date of aphoto to title_to_date(title of aphoto) of me
  end repeat
end tell

-- title: dd-mo-yy_hhmi
on title_to_date(title)
  set dd to characters 1 thru 2 of title as string
  set mo to characters 4 thru 5 of title as string
  set yy to characters 7 thru 8 of title as string
  set h to characters 10 thru 11 of title as string
  set hh to (h - 1) as string -- otherwise the dates are all 1 hour too late!
  set mi to characters 12 thru 13 of title as string
    
  return date (dd & "/" & mo & "/" & yy & " " & hh & ":" & mi)
end title_to_date

I’m beginning to see what is so cool about Apple Script; suggestions on how to improve the script gratefully received!