Or how to debug why your Spotlight plugin is not finding your files (on Lion).

Let’s say that you’re writing a Core Data, and you’re having a store where you’re saving all your entities (e.g. books). There are a couple of tutorials on how to integrate this Core Data store with Spotlight, for example Marcus Zarra’s presentation “Spotlight and QuickLook vs. Core Data” (NSConference ‘09) or his excellent book Core Data: Apple’s API for Persisting Data on Mac OS X.

Basically, each time you’re adding/updating/deleting a row, you are creating/updating a file containing some data you want to index in Spotlight (e.g. book title, description, ISBN, etc).

However, things might not work from the first run. If it happens, I suggest to first try all the steps from Troubleshooting Spotlight Importers.

If the above steps doesn’t help, here are a couple of steps you can also try. Test if your import works with your file:

bash$ mdimport -d2 -g ~/path/to/TestSpotlight.mdimporter ~/path/to/book-isbn-123456.book

There are two important parts in the output of this command. The first one is this:

loading plugin at path '/Users/.../TestSpotlight.mdimporter' for UTIs: com.coralmagnetic.book.

This part says what UTI does your importer know to read (in this case com.coralmagnetic.book).

Import: magic_file returned "XML  document text" for path "/Users/.../book-isbn-123456.book" of type "dyn.ah62d4rv4ge81s7psqvw1e2xtr7zxe"

This part says what type is the file you’re trying to import. In this particular case, it says ‘XML document text’. Because there is a missmatch between the importer plugin and the importer file and this file will be scanned and imported as ‘generic’ file. You will be able to find it using the file name (or extension name), but you won’t find it by looking for content. However if this line says:

Importing type 'com.coralmagnetic.book' using ...

then it should import your content. In case there is a missmatch between these UTIs, you need to check both your Info.plist files (from the App and from the Spotlight importer) for the array key ‘CFBundleDocumentTypes’ to contain key ‘LSItemContentTypes’ with the value ‘com.coralmagnetic.wunderbook’.

Let’s say you made it so far, and the correct importer is reading all the info from your meta file, and somewhere in the output you’re seeing it as:

kMDItemDisplayName = "Alice's Adventures in Wonderland - Lewis Caroll";
kMDItemKind = {
  "" = "Book";

kMDItemTextContent = "It tells of a girl named Alice...";
kMDItemTitle = "Alice's Adventures in Wonderland";
kObjectId = "x-coredata://63CB1FB7-9F56-465B-8104-1A0DC8AA383E/Books/p112";

Now, you should try to look for them using Spotlight search (and his command line equivalent: mdfind). If both commands return something, you’re done.

But let’s assume that at an earlier point, as a good MacOSX developer, you’ve decided to save the the files somewhere in the caches directory: ~/Library/Caches/Books/. Running:

mdimport -d2 -g ~/Downloads/Spotlight/TestSpotlight.mdimporter ~/Library/Caches/Books/book-isbn-123456.book 

will return the correct, matching UTIs, but it wouldn’t list the content of the meta file (e.g. the kMDItemTitle key). However, if you move the same “book-isbn–123456.book” file to a different place (e.g. ~/Desktop), the content is properly read. And now is when ‘mdls’ it comes to the rescue, to list the metadata for each specified file.

If you run mdls for the same file, both in ~/Library/Caches/Books and ~/Desktop you’ll notice a couple of things.

First, in the ~/Library/Caches directory you have this part:

kMDItemSupportFileType = (

And in ~/Desktop directory you have this part:

kIsbn        = 12345
kMDItemTitle = "Alice's Adventures in Wonderland"
kObjectId    = "x-coredata://63CB1FB7-9F56-465B-8104-1A0DC8AA383E/Books/p112"

The first part, it tells Spotlight that this is a system file. This means that you won’t be able to find it via the right top Spotlight search (command + space). To find these files, use Finder search, click on the + and add “System files” “are included”.

I don’t know the exact reason that it doesn’t index the files from ~/Library/Caches, but it indexes them from other ~/Library directories. I suppose it has a list of ‘Exclusions’ saved somewhere (it’s not in the regular Spotlight preferences panel). I found this accidentally, and it could be that it wasn’t present before Mac OSX Lion.