Confused Development

I develop software and I often get confused in the process. I usually find the answers after a while, but a month later I can't remember them. So from now on, I will write them down here.

Thursday, October 12, 2006

Problems with Spotlight

I finally got around to playing with Apple's Spotlight, which I want to use in semiBlog. Somehow, I find Apple's documentation on the topic a bit lacking. Sure there are lots of documents, and they help, but they also confuse.

E.g. the Query Expression Syntax document tells you that you can write the following query:

kMDItemAuthors == "Steve"wc && kMDItemContentType == "audio"wc

Now, when you try to create an NSPredicate to run in an NSMetadataQuery object with that expression, you will get an error. Why? Because, as I later found out, if you want to do Spotlight queries the ObjC way (using the classes I just mentioned), you have to use a similar, but slightly different query syntax. :-( How stupid is that? I'm sure there are reasons for that (This document explains it all...), but it was a somewhat frustrating process to find out. The same query in an NSPredicate has to look like this:

(kMDItemAuthors LIKE[wc] 'Steve') && (kMDItemContentType LIKE[wc] 'audio')

Another problem I had is that I found it hard to figure out what the kMDItemContentType of various kinds of data like AddressBook entries or iCal events are (I want to ask things like "give me all contacts" or "give me all events". The System-Declared Uniform Type Identifiers document lists a vast number of those types. But - I had two problems here:

  • Contacts: A type public.contact is mentioned as the "Base type for all contacts". That is true, but if you query for kMDItemContentType == 'public.contact' you will probably get nothing. This is because AB contacts actually have the type com.apple.addressbook.person (not mentioned in the document...), which is a more specific type (a little like a subclass). And, asking for the type will only give you the most specific one - it doesn't give you the super-types by inference. Instead, you have to query for the kMDItemContentTypeTree - this attribute contains a collection of all types, from the most specific to the most general. So, either kMDItemContentTypeTree == 'public.contact' or kMDItemContentType == 'com.apple.addressbook.person' will get you what you want (if you want all contacts).
  • Events: The document also mentiones a type public.calendar-event as the "Base functional type for all scheduled events". Annoyingly, this type doesn't seem to be used at all, not even as a super type for com.apple.ical.bookmark (again not mentioned in the document). So, if you are looking for all events, query for kMDItemContentType == 'com.apple.ical.bookmark'.

2 Comments:

At 7:30 am, Blogger Peter Hosey said...

Have you filed a radar about the iCal bug?

 
At 11:49 am, Blogger Knud Möller said...

Thanks Peter, for reminding me. I just reported the bug. Bug ID is 4860724.

 

Post a Comment

<< Home