30 Sep 2005

adding arbitrary metadata through spotlight: now possible


last night i spent some time disassembling bits and pieces to find out how Finder actually is able to edit the Spotlight Comment Field in the Get Info pane because that must be the ticket in to editing arbitrary metadata. if Finder can do it, we should be able to.

Picture 3

so going from /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder, you can see that it links to a bunch of different libraries, one of which is named DesktopServicesPriv.


McBook:/System/Library/CoreServices/Finder.app/Contents/MacOS liquidx$ otool -L Finder | awk '{print $1}' | grep -v "/Framework"
Finder:
/System/Library/PrivateFrameworks/DesktopServicesPriv.framework/Versions/A/DesktopServicesPriv
/System/Library/PrivateFrameworks/Bom.framework/Versions/A/Bom
/System/Library/PrivateFrameworks/FileSync.framework/Versions/A/FileSync
/usr/lib/libSystem.B.dylib


and in there it links to a symbol somewhere called MDItemSetAttribute, our ticket into the metadata system.


McBook:/System/Library/PrivateFrameworks/DesktopServicesPriv.framework liquidx$ nm -g DesktopServicesPriv | fgrep MDItem
U _MDItemCopyAttribute
U _MDItemCopyAttributes
U _MDItemCreate
U _MDItemSetAttribute
927e0834 T _NodeCopyMDItemIdentity
U __kMDItemSortIdentityAttr
U _kMDItemContentType
U _kMDItemContentTypeTree
U _kMDItemDisplayName
U _kMDItemFinderComment
U _kMDItemPath


so, where is this MDItemSetAttribute? the logical place would be in the CoreServices.framework, along with all the other MDItem functions, so looking in there ..


cBook:/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework liquidx$ nm -g Metadata | grep SetAttribute
9103d1ac T _MDItemSetAttribute
9103547c T _MDItemSetAttributes
9103d890 T __MDCopyLastSetAttributesStats
910305ec T __MDSetAttributeSchema
91042010 T ___MDItemSetAttributesEllipsis1


bingo! we've found it. so we need to check out what the parameters are and what it returns. that isn't too bad, we just use otool -tV to dissassemble the text section and find the symbol:


_MDItemSetAttribute:
9103d1ac mfspr r0,lr
9103d1b0 stmw r28,0xfff0(r1)
9103d1b4 bcl 20,31,0x9103d1b8
9103d1b8 stw r0,0x8(r1)
...


after all that i sort of guessed that MDItemSetAttribute's prototype should be something like:

int MDItemSetAttribute(MDItemRef item, CFStringRef attribute, CFTypeRef value);


from both the MDItem Reference plus seeing where r3, r4 and r5 goes inside the function both in Metadata.framework and DesktopServicesPriv.framework.

turns out though, i didn't need to do so much guessing, and plus, my guesses were nearly spot on. hetima, of safaristand fame, has a blog entry about exactly this only a month ago. except he went a step further and looked at MDItemSetAttributes too. that would make sense since he's been hacking multi window spotlight recently from his photostream.


You can reply to me about this on Twitter: