[Hacking Mac]: iPhoto Structure – Revisit


See here for more information regarding iPhoto database

Looks like I didn’t have the proper understanding of the structure of the iPhoto library package. iPhoto appears to use the internal structure even if the photos are imported by reference. In the directory iPhoto Library/Originals/ an alias to the original photo is still stored where the image would have been imported. This alias file also needs to be updated for iPhoto to properly reference the new file location instead of just updating the iPhoto database with the new file location.
This alias location can be queried from either applescript, or the SQL database. From applescript you can get the filename with the following:
repeat with i_photo in (get every photo of (get album s_album))
get original path of i_photo
end repeat

From the database, the location can be queried from the column aliasPath in the SqFileInfo table using:
select SqFileInfo.primaryKey,SqFileInfo.aliasPath from SqFileInfo,SqFileImage where SqFileImage.sqFileInfo=SqFileInfo.primaryKey and SqFileImage.photoKey=ID
You will need the primaryKey later if you want to update the image location. For using the SQL database query, the photoKey ID can be queried via applescript as
get id of i_photo as unsigned integer
The Id for the photo needs be be converted to unsigned integer from what is normally returned by applescript as it’s going to appear as a floating point number due to the number of bits used.

Both of these return the full path to either the original file that was imported, or the reference to the original file if it’s imported by reference. In the case of importing by reference, iPhoto will store a file at that location
-rw-r--r--@ 1 user staff 168340 May 26 12:38 DSC_0001.JPG
That file is basically a mac alias to where the image is actually stored.

sidenote: This is different than a hard or symbolic link in UNIX. You can see that it doesn’t dereference to the target location as a normal symlink would. It’s differentiated with the “@” after the list of UNIX file attributes. This alias appears to be a holdover from OS9. You can view the attributes that are associated with it using the xattr command like:
attr -vrx DSC_0001.JPG
DSC_0001.JPG: com.apple.FinderInfo
DSC_0001.JPG: com.apple.ResourceFork

The ResourceFork attribute contains the target information for the alias. Using applescript you can get the target also with
if kind of fp_alias is "Alias" then
get original item of fp_alias as alias)

The location of the original referenced file can be updated by correcting the location of the file in the database, and the alias file that iPhoto uses. This would involve correcting the datebase entry such as:

update SqFileInfo set relativePath=UPDATED_PATH where primaryKey=ID

The UPDATED_PATH would be where the image is being moved to, and the id ID would be returned as the primaryKey from the appropriate entry in the table returned by the database query above.

One the database is updated, the alias file that iPhoto uses would have to also be updated to reflect the new location. This could be done in applescript by creating a new alias such as:

set fp_newAlias to make new alias file at fp_dir to targetName with properties {name:s_name}

For good measure you can also update the timestamp of the database with:

update SqGlobals set modificationDate=(julianday(\"now\") - julianday(\"2001-01-01 00:00:00\")) * 60 * 60 * 24 ;

2 thoughts on “[Hacking Mac]: iPhoto Structure – Revisit

  1. Hey, just wanted to know your blog post here was a life saver… the update statement at the end was my key to figuring out how Apple is storing the SqPhotoInfo.MetaModDate & SqPhotoInfo.modificationDate in the iPhotoMain.db and the SqPhotoInfoOther.archiveDate in the iPhotoAux.db.

    I’ve written about the above (giving your blog lots of credit) and more about the iPhoto database in general on my Tumblr Blog @

    http://tumblr.maccodemonkey.com/post/9111450792/more-on-iphotomain-db-iphotoaux-db-date-formats

    I sure wish this stuff was better documented someplace.

  2. Glad that someone is even looking at this. I spent quite a bit of time looking at the database for iPhoto trying to figure it out. There appears to be almost no information out there, but I was pretty sure that I couldn’t be the only one trying to manipulate it. You’ve got some good stuff there, but I didn’t hit on your blog during my searches.

    Also, regarding the post http://tumblr.maccodemonkey.com/post/9080035545/iphotomain-db-table-relationships (iPhotoMain.db Table Relationships) the third entry with just the filename, I’m pretty sure that is the entry for the image as a key photo for an event. It should have small dimensions.

    I’m planning on doing some more work regarding merging events and moving images from one event to another as soon as I can find some free time. …sometime..this millennium…

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.