One thing that particularly interests me with Spotlight is how an application like my own Keep It Together can set additional metadata attributes on files owned by generic or other applications’ Spotlight importers.

KIT keeps all the original files on disk, not in a database or file package, so they can be found by Spotlight and indexed by the relevant importers. What’s missing right now is the extra metadata from KIT itself - rating, tags, etc, that isn’t stored in the actual files.

iPhoto works in a similar way, so it’s a good case study. Metadata in the image files in iPhoto’s library will be indexed using the generic Image importer. iPhoto adds some attributes of its own, such as a rating and keywords. Looking at iPhoto’s importer, it only works on .ipspot files. iPhoto writes out a single iPhoto.ipspot file, which has one line pointing to the XML library that changed.

At first, I wasn’t sure how the importer could handle that, since a number of different items can be changed in one operation, yet the importer can only return details for a single item, namely the file that triggered the change.

I did some digging and it seems the iPhoto importer doesn’t return details for the .ipspot file at all, but rather writes out the file to trigger the importer, which manually sets metadata attributes on the files in its library using a private function, MDItemSetAttributes. Looking at the Metadata framework, there’s also a corresponding MDItemSetAttribute function, also private.

Boolean MDItemSetAttributes (MDItemRef item, CFDictionaryRef dict);
Boolean MDItemSetAttribute (MDItemRef item, CFStringRef name, CFTypeRef attribute);

Caution would be wise with that approach because, private function aside, performance is a crucial factor. The importer needs to do its work quickly as it will be called every time the trigger file is changed.

Another thought is that you could actually use these functions in your application, rather than rely on an importer to do the work for you, but as the function is undocumented, there is no way of knowing whether that would also cause issues.

The missing piece of the jigsaw here is how iPhoto opens and selects the image whenever the file is chosen from Spotlight’s results, since opening the file in the Finder would normally open its default application, such as Preview, Photoshop, or whatever. That’s not something I’d want to do for KIT, but it would be interesting to find out how it’s done. If anyone knows, please comment.

Update: My understanding now is that selecting the photos in iPhoto as mentioned above is something entirely hardcoded into Spotlight and not something possible for just any app. See the AppleScript statements in /System/Library/CoreServices/Search.bundle/Contents/Resources/iPhotoScripts.plist

Leave a Reply