Shearer Software

Andrew Shearer’s Drivel

99 and 44/100 percent pure.

Sunday, January 18, 2004

iPhoto 4 has comments no more

I bought the upgrade to the Apple’s iLife suite, released on Friday. Here’s a gotcha for developers who parse iPhoto’s AlbumData.xml file, though it doesn’t directly affect most users. It affects me, because my own code parses AlbumData.xml to generate my web-based photo albums (such as the England trip pictures I just posted).

Though the overall format of iPhoto’s XML file stays the same (and my script had no trouble reading it), the Comments and Date fields are gone! The Date field is renamed and in a different format, which is no problem to work around because the image file’s embedded EXIF data contains the date as well. The missing Comments field is a different story.

From my quick inspection, the comment data seems to be only stored in a newly introduced iPhoto.db file, which is in some binary format. The rationale for this is presumably performance, but that doesn’t completely make sense, since the photo title is still stored in the XML file and it may be changed just as often.

In any case, here’s a workaround that uses AppleScript to write a parallel folder structure holding just the comments, one per text file. Paste the following into a Script Editor window and run. Use this anytime you’d like to protect your comments from the vagaries of software or platform transitions or upgrades. (The parallel folder structure helps this; the script could have used iPhoto’s internal IDs and generated all the files in a single folder, but that wouldn’t have been as forward-compatible.) GPL-licensed.

-- Export iPhoto Comments
-- Creates a parallel folder structure to the iPhoto Library, with a file corresponding to each picture with a comment, containing just the comment.
-- Note: this does not remove files in the parallel folder when a comment disappears (due to deletion of either the comment or the image). To guard against this, you may want to delete the whole comment folder before rerunning this script. (Using a parallel folder structure rather than storing comment files alongside the image makes this easier; you can flush the whole cache at once.)
-- Written to work around the fact that iPhoto 4 no longer stores photo comments in the AlbumData.xml file.
-- For automatic folder creation, requires the BSD subsystem (which is installed by default).
-- by Andrew Shearer, 2004-01-18 <mailto:ashearerw at shearersoftware dot com>

-- config
set commentsFolderName to "iPhoto Library - My Comments Cache"
set stripJPG to false --whether to strip .JPG extension
set openFoldersInFinder to false
set commentFileSuffix to ".comment.txt"
set requiredAlbumPrefix to "Web-"
-- end config

set commonRootPath to POSIX path of (path to pictures folder)
set origFolderName to "iPhoto Library"
set origFolderPath to commonRootPath & origFolderName
set commentsFolderPath to commonRootPath & commentsFolderName

tell application "iPhoto"

repeat with a in (every album whose name starts with requiredAlbumPrefix)

repeat with p in (every photo of a whose comment is not "")

set imagePath to image path of p
set commentText to comment of p as Unicode text

if imagePath does not start with origFolderPath then

-- make sure image is inside iPhoto Library; otherwise we won’t know where to put the comment file. This AppleScript comparison is case-insensitive.
error "Image does not appear to be inside iPhoto Library. Image path: \"" & imagePath & "\". Expected library path: \"" & origFolderPath & "\""

else

-- construct new path in parallel folder structure
set commentFilePath to commentsFolderPath & text from (1 + (length of origFolderPath)) to -1 of imagePath
if stripJPG then

-- strip .JPG suffix (optionally)
if commentFilePath does not end with ".JPG" then

error "Error: file does not end with .JPG: \"" & imagePath & "\""

end if

set commentFilePath to "" & text 1 through -5 of commentFilePath

end if
-- add suffix to comment filename (.txt extension, etc.)
set commentFilePath to commentFilePath & commentFileSuffix
-- create intermediate folders as necessary with mkdir shell command. Finder-based alternatives for this are awkward. +++ This code has not been checked for proper shell escaping, though it does at least enclose its arguments in double quotes.
do shell script "mkdir -p \"`dirname \"" & commentFilePath & "\"`\""
if openFoldersInFinder then do shell script "open \"`dirname \"" & commentFilePath & "\"`\""

-- rest of the code writes out the comment file

set commentFile to commentFilePath as POSIX file -- make into Mac filespec

set f to open for access commentFile with write permission
set eof f to 0 -- truncate file, or old data can remain
write commentText to f as Unicode text
close access f

end if

end repeat -- photos in album

end repeat -- albums

end tell

That’s the AppleScript code. The comments are now in a human-readable text format, and you could use a script such as this Python snippet to read a given picture’s comment:

commentCommonBaseDir = os.path.expanduser(“~/Pictures/”)
commentOrigDir = os.path.join(commentCommonBaseDir,
    “iPhoto Library”)
commentParallelDir = os.path.join(commentCommonBaseDir,
    “iPhoto Library - My Comments Cache”)
commentFileSuffix = “.comment.txt”

def getCommentForFile(imagePath):
    if not imagePath.lower().startswith(commentOrigDir.lower()):
        raise (‘Error: image does not appear to be in iPhoto Library; ‘ +
           ‘cannot compute comment path. Image: “%s”. Library: “%s”.’ ) 
           % (imagePath, commentOrigDir)
    commentPath = os.path.join(commentParallelDir,
           imagePath[len(commentOrigDir)+1:]) + commentFileSuffix
    if os.path.isfile(commentPath):
        print “Read comment for “ + imagePath
        return open(commentPath, ‘r’).read()
    return 
   Mac OS X, Python, Open Source, Pictures, Software  Posted at 4:57 PM   

8 Responses to “iPhoto 4 has comments no more”

  1. Jeremy Higgs Says:

    Hi!

    How hard would it be to modify that script so that it exports the comments of each of the images to the same folder, in the filename format .txt? I’ve been looking for something like this so I can use JAlbum (http://www.datadosen.se/jalbum) on MacOS X, without having to manually type each comment out again…

    Thanks!

  2. Charles Martin Says:

    I’m probably missing something in your post, Andrew, but the comments are still present in iPhoto 4. One merely has to press the “i” button and there they are.

  3. Andrew Shearer Says:

    This isn’t a problem for regular users, just developers who parse the AlbumData.xml file, as well as people who want to future-proof their photo comments. The comments field is still there in iPhoto, but the comments themselves are now stored in an unknown (proprietary?) binary format instead of XML, so you need to run iPhoto to read them. If you’re writing a program which relies of this data, or if you are archiving your photo album to be read many years in the future (when iPhoto might not be around), run this script every so often to back up the comments in a format you have a fighting chance of reading.

  4. Lao Says:

    It should be possible to extract the comments directly through Python without having two seperate scripts, by using OSA from Python. I’m currently awaiting my new copy of iLife, so I cannot provide more detailed information that I can be sure will work. However, I’ve written a weblog entry on extracting and setting track information from iTunes (http://stompstompstomp.com/weblog/technical/2003-09-22) which may serve as a basis for doing a similar activity with iPhoto.

  5. Andrew Shearer Says:

    Jeremy, I’ll post a version that uses a flat filename-based structure as you suggested soon.

  6. nj Says:

    do you know of a way to extract photo ratings as well? i browsed the iphoto applescript dictionary, but there doesnt seem to be a way…

  7. Andrew Shearer Says:

    Good question on the photo ratings. Maybe creating five “smart albums” that only selected photos with a particular rating and querying those would serve as a workaround.

  8. Hardy Says:

    Hi,
    I have exactly the vice-versa problem, still unsolved: I have a list of picture filenames and comments, and a directory with the pictures themselves. And, I want to bring them together in iPhoto. Do you have _any_ idea how this could be done?
    Thanks a lot in advance for any suggestion.
    Hardy

Leave a Reply

January 2004
M T W T F S S
« Dec   Feb »
 1234
567891011
12131415161718
19202122232425
262728293031  
Recent Reading

A Heartbreaking Work of Staggering Genius, by Dave Eggers

Harry Potter and the Order of the Phoenix, by J. K. Rowling

Player Piano, by Kurt Vonnegut

Bad News, by Donald E. Westlake

The Blank Slate: The Modern Denial of Human Nature, by Steven Pinker

The Jungle, by Upton Sinclair

Gödel, Escher, Bach: An Eternal Golden Braid, by Douglas R. Hofstadter

Speaking With the Angel, by Nick Hornby (Editor)

In Progress

The Language Instinct, by Steven Pinker

The Corrections, by Jonathan Franzen