Set photo titles to picture capture date in iPhoto

Mar 11, '10 07:30:00AM

Contributed by: juanfal

The titles iPhoto chooses for just-added photos is the annoying "IMG_nnnn" serial number from the camera. This AppleScript simply takes the date of the photo (EXIF info that iPhoto knows very well) and uses it as the photo title. The format is: yyyy-mm-dd-hh.mm.ss, so it's a bit easier to read than the ISO version, but also easily sortable.

Here's the code:

tell application "iPhoto"
  --  activate -- bring iPhoto back to front
  copy (my selected_images()) to these_images
  
  if these_images is {} then error "Please select some images before using this script."
  set thename to ""
  set thepaths to ""
  set thedates to ""
  repeat with i from 1 to the count of these_images
    set this_image to item i of these_images
    
    --set this_file to the image path of this_image    
    set thename to thename & ", " & the title of this_image
    --set thepaths to thepaths & the image path of this_image & ", "
    set thedate to the date of this_image
    
    set theyear to the year of thedate
    set themonth to my format(the month of thedate as number)
    set theday to my format(the day of thedate)
    set theseconds to my format(the seconds of thedate)
    set themins to my format(the minutes of thedate)
    set thehours to my format(the hours of thedate)
    
    set thetitle to theyear & "-" & themonth & "-" & ¬
      theday & "-" & thehours & "." & themins & "." & theseconds
    
    --set thedates to thedates & thetitle & ", "
    set the title of the item i of these_images to thetitle as string
    
  end repeat
  
end tell

on selected_images()
  tell application "iPhoto"
    try
      set these_items to the selection
      if the class of item 1 of these_items is album then error
      return these_items
    on error
      return {}
    end try
  end tell
end selected_images

on format(thenumber)
  if (thenumber as number) < 10 then
    set res to "0" & (thenumber as string)
  else
    set res to thenumber as string
  end if
  return res
end format
Copy and paste the above in AppleScript Editor, and save it as a Script named 'Rename Photos' (or whatever) in /Library » Scripts » Applications » iPhoto (create any of those folders as required). Make sure the AppleScript menu icon is visible (in AppleScript Editor's prefs in 10.6; in AppleScript Utility in 10.5), then switch to iPhoto. Select some photos in iPhoto from the Events or Photos section, then select the saved script from the AppleScript menu and ... voila ... the photo titles will change to their respective capture dates.

[robg adds: This worked as described in both 10.5 and 10.6. I modified the save path in the hint to include the Applications sub-folder, which forces the script to the top-level of the AppleScript menu. If you'd rather have it in an iPhoto sub-menu, save it into an iPhoto folder directly in the Scripts folder.]

Comments (17)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100307050736756