Mac OS X 10.4 (Tiger): Using raw Spotlight queries to fine-tune file searches

Posted by Pierre Igot in: Macintosh
May 3rd, 2005 • 7:41 am

If you start using Spotlight seriously in Tiger (and you should), then you might soon find that the functionality available through the regular Tiger interface is too limited to meet your needs.

For example, you can easily create a smart folder in the Finder that shows all files of type “Microsoft Word document” that were last modified “This week“. But you might find that this smart folder only displays some of the files that you’d like it to display. Why? Because there are actually several kinds of Microsoft Word documents. If you use the criterion “Kind” and then choose “Other…” in the second pop-up menu, and then look under “Microsoft Word” in the third pop-up menu next to the field intended to contain the other file kind that you want, you’ll see that there are all kinds of different “Microsoft Word” files. Here’s only a fraction of the list:

Different kinds of MS Word files

If you still have Word 6 files on your hard drive, they won’t be found by a search for files of the kind “Microsoft Word document“. And don’t ask me what the difference is between “Microsoft Word Document” and “Microsoft Word document“!

I frequently save Word documents as RTF files, which are still Word documents, but not “regular” Word documents. And I want my smart folder to include these files as well. So how do you do this?

In the normal smart folder interface, you cannot have anything other than a series of criteria connected by the logical link “AND“. If you create a smart folder with the criterion “Kind > Others… > Microsoft Word document” and the criterion “Kind > Others… > Microsoft Word RTF document“, the smart folder will try to find files that are both a “Microsoft Word documentand a “Microsoft Word RTF document“. In other words, it won’t find any.

This means that you cannot use the regular smart folder interface. Fortunately, there is another solution: raw Spotlight queries. They have a bit of a learning curve and are not properly documented by Apple yet, but they work perfectly well, and enable you to do all kinds of things that the regular smart folder interface doesn’t let you do.

For example, in order to find all the Microsoft Word documents (including RTF files, Word 6 files, etc.) last modified during this week, all I have to do is define a smart folder with the criterion “Last Modified > This Week” and the criterion “Raw Query > (kMDItemKind== “*Microsoft Word*”)

This raw query instructs Spotlight to look for all files that contain the phrase “Microsoft Wordsomewhere within their “Kind” field (which explains the “*” symbol before and after the phrase in the query). Et voilà!

Now that’s what I call a smart folder!

As you can see, thanks to the “*” wild card, I didn’t even have to use several criteria connected by the logical link “OR“. But with raw queries, such a logical link is possible. You can simply use the “||” operator between two criteria (as opposed to “&&“, which is the operator for “AND“).

For more on this, you can read this page and this page, as well as John Siracusa’s review of Tiger (scroll down to the section about building queries).

Excellent! (Thanks to Sven for the pointer.)

7 Responses to “Mac OS X 10.4 (Tiger): Using raw Spotlight queries to fine-tune file searches”

  1. Pierre Igot says:

    The “query language” is like all query languages :). It’s not really designed for non-geeks. Apple hasn’t solved the issue of making more advanced boolean logic available to the non-geek user. It’s just been kind enough to make the geeky stuff available to semi-geeks like ourselves :).

  2. Warren Beck says:

    Very nice! Some real power there, under the hood, but still there is the easy interface for casual use. This is why we use Macintosh.

    Immediately after installing Le Tigre, I wanted an OR operator in the Spotlight search criteria. I wanted to find all the recent files that I had opened that were either pdfs or pages documents. I was disappointed, since the supplied interface only provides AND operators, in effect. But using your directions, and the linked pages, I obtained the raw query:


    Works great.

    My only quibble is with the somewhat cryptic computer-geek query language. Why didn’t the designers just use


    instead of the uber-geeky



    Nice post, Pierre.

  3. Paul Ingraham says:

    I get highly erratic results from even simple raw queries: sometimes they work as expected, but mostly they give no results at all (endlessly spinning “Searching…” indicator). This is the unchanged query, mind you, producing results one minute and failing the next. Even now my finder window is still “Searching…” while the smart folder is selected, while the previous time it produced the expected results.

    Sometimes I seem to be able to “reset” the results by changing the search domain to a different folder, and then switching back to the folder I want. Other times, it just stays stuck.

    My general impression after two hours of futile experimentation is that it’s broken and I probably won’t try again until OS 10.5.

  4. Pierre Igot says:

    Strange. I haven’t had any problems with my raw-query-based smart folders since I created them. What are your exact criteria?

  5. Paul Ingraham says:

    I got erratic results with several simple queries. For instance, taken from Waren Beck’s example above, I tried:


    When that didn’t work at all, I assumed perhaps there was just something wrong with the syntax, and so I simplified it to just:


    But that produced results only erratically as well, as did several other dirt simple matching queries, and barely more complex queries like this one:

    (kMDItemKind == "Pages*" || kMDItemKind == "Text*")

    This query, as of this morning, is producing results without complaint… but last night, while creating it, I had given up hope that it would ever be useful.

    The real concern I had was that the same, unchanged query would behave differently when changing the search domain. For instance, I would switch from applying the search from the “Home” folder to limiting it to a specific folder where the find was initiated (full of relevant files!), and the query would produce results from “Home” but fail in the specific folder… or vice versa, giving a pretty clear impression of bugginess or latency. What worked one moment didn’t work the next.

  6. Pierre Igot says:

    First of all, I don’t think quotation marks need to be “escaped” (with the “\” character) in raw queries, at least not when entered in a “Raw Query” criterion in a smart folder. It doesn’t look like this is the issue here, but I thought I’d mention it, just in case.

    I don’t have much experience with switching the scope/domain of a search with a raw query, but when I do try it it seems to be working fine on my machine (with a little latency depending on how narrow the search is, obviously).

  7. Paul Ingraham says:

    I agree, and I’m not sure why Warren escape them in his example, but in any case I duplicated the query both with and without the escaping and had the same problems either way.

    At some point I’ll probably investigate this further and try to reproduce the problems. If I do, I’ll certainly post my results here.

Leave a Reply

Comments are closed.