blog advertising is good for you


blog advertising is good for you
User login

How to Execute Raw Spotlight Queries in the Finder

Sometimes you need to do nested queries or more advanced boolean logic than “all or nothing” as the Finder gives, and the Finder gives you a way, though it’s a might complicated.

When you press Command-F to start making a new Spotlight query, choose Other… from the menu and scroll down to Raw Query (might want to add this to your Favorites).

Now you can enter in raw Spotlight queries. How? Well …

Spotlight queries are both easy and cryptic. They’re easy to understand as they’re basically logical expressions as used in any programming language. They’re cryptic because of the keywords you have to use. As an example, the following query finds all PDF files not made with Acrobat for Windows:

(kMDItemKind “PDF“) && (kMDItemEncodingApplications != ‘Windows‘)==

To get the keywords, browse a few files of the type you want to find with the mdls command in Terminal. Here’s what it has to say about a photo I took:

Pictures/iPhoto Library/2004/06/12/DSCF0063.JPG -------------
kMDItemAcquisitionMake         = "FUJIFILM"
kMDItemAcquisitionModel        = "FinePix2800ZOOM"
kMDItemAperture                = 3
kMDItemAttributeChangeDate     = 2005-04-15 23:25:10 -0500
kMDItemBitsPerSample           = 32
kMDItemColorSpace              = "RGB"
kMDItemComment                 = "Last-minute breathing and relaxing before we ruin their morning with questions and 
general harassment."
kMDItemContentCreationDate     = 2004-06-12 15:48:12 -0500
kMDItemContentModificationDate = 2004-06-12 15:48:12 -0500
kMDItemContentType             = "public.jpeg"
kMDItemContentTypeTree         = ("public.jpeg", "public.image", "public.data", "public.item", "public.content")
kMDItemDisplayName             = "DSCF0063.JPG"
kMDItemEXIFVersion             = "2.2"
kMDItemFlashOnOff              = 1
kMDItemFocalLength             = 32
kMDItemFSContentChangeDate     = 2004-06-12 15:48:12 -0500
kMDItemFSCreationDate          = 2004-06-12 15:48:12 -0500
kMDItemFSCreatorCode           = 0
kMDItemFSFinderFlags           = 1024
kMDItemFSInvisible             = 0
kMDItemFSLabel                 = 0
kMDItemFSName                  = "DSCF0063.JPG"
kMDItemFSNodeCount             = 0
kMDItemFSOwnerGroupID          = 501
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 972778
kMDItemFSTypeCode              = 1246774599
kMDItemHasAlphaChannel         = 0
kMDItemID                      = 513388
kMDItemISOSpeed                = 7
kMDItemKind                    = "JPEG Image"
kMDItemLastUsedDate            = 2004-06-12 15:48:12 -0500
kMDItemOrientation             = 0
kMDItemPixelHeight             = 1200
kMDItemPixelWidth              = 1600
kMDItemRedEyeOnOff             = 0
kMDItemResolutionHeightDPI     = 72
kMDItemResolutionWidthDPI      = 72
kMDItemStarRating              = 3
kMDItemTitle                   = "Inside the Store"
kMDItemUsedDates               = (2004-06-12 15:48:12 -0500)

As you can see, there’s a ton of info here, and a lot of it is from iPhoto (like the comments and the star rating). Build your query with the codes on the left, like the following, which finds all good photos taken without a flash:

(kMDItemStarRating > 3) && (kMDItemFlashOnOff == 0)

Why search for photos in the Finder based on things like this? Well:

  1. You can make complex queries, if needed, and even base your query on the focal length when used with a certain model of camera.
  2. Once you have said selection, you can press the slideshow button in the Spotlight results for an instant slideshow in the Finder without starting iPhoto.

Of course, it works for other things as well, so have fun, and post any good recipes below! Smiling

Average rating
(0 votes)
About Adam Knight
Adam Knight's picture

Author Biography

Adam Knight is one of the founders of Mac Geekery and is a geek at heart. Programmer by day, hacker by night, his daily life revolves around the Macintosh platform, which he has been a user and programmer for since the early days of System 7 when his LCII replaced his Apple //c.

In-between tech jobs, he’s managed to learn the basics of any web hacker: PHP, MySQL, Perl, Apache, Linux, *BSD, and the intricacies of ./configure —prefix=~/bombshelter/. Today, codepoet is concentrating on blogging again, writing some software for the Mac by himself (including Notae) and for his company (such as Switchblade) and has a few other toys coming out soon.

Bug him over AIM or email [link fixed].

You know, episodes of the new Doctor Who seem so short, after having gotten used to story arcs spread across multiple episodes. Lovely baby-faced aliens, though. I was a little afraid that, after installing Tiger, I might not be…

Maybe I’m just having a slow day, but I don’t see where I can choose Custom… from the menu in order to enter a manual query. The Finder menu doesn’t change from what it usually is when I enter find mode, and the only thing in the local window that is remotely window like says “Servers Computer Home Others…” I tried Others… and it only lets me select different locations to search within.

Adam Knight's picture

“Custom…” is in the criteria menu, not the scope bar. Where it says “Kind” and “Last Modified” and so on.

cp

Hmm… Not mine. I have:

Kind
Last Opened
Last Modified
Created
Keywords
Color Label
Name
Contents
Size
—-
Other….

If I choose Other I just get a huge list of attributes from Album to Year recorded. But there is no Custom in this list either. Are you running 8A428? Maybe they had a custom feature in an earlier build and took it way for the final version?

Adam Knight's picture

Yep, you’re right, it’s Other.

cp

delImport is a Spotlight plugin that imports your del.icio.us bookmarks and makes them available for searching through the Spotlight interface. Its mdls output looks like this:

19ee79083e52a5878eb10279ac571bbf.delicious -------------
kMDItemAttributeChangeDate     = 2005-05-05 20:16:36 +0100
kMDItemContentCreationDate     = 2005-05-05 20:15:21 +0100
kMDItemContentModificationDate = 2005-05-05 20:15:21 +0100
kMDItemContentType             = "org.ianhenderson.delicious.bookmark"
kMDItemContentTypeTree         = (
    "org.ianhenderson.delicious.bookmark",
    "com.apple.safari.bookmark",
    "public.data",
    "public.item" )
kMDItemDisplayName             = "How to Execute Raw Spotlight Queries in the Finder"
kMDItemFSContentChangeDate     = 2005-05-05 20:15:21 +0100
kMDItemFSCreationDate          = 2005-05-05 20:15:21 +0100
kMDItemFSCreatorCode           = 0
kMDItemFSFinderFlags           = 0
kMDItemFSInvisible             = 0
kMDItemFSLabel                 = 0
kMDItemFSName                  = "19ee79083e52a5878eb10279ac571bbf.delicious"
kMDItemFSNodeCount             = 0
kMDItemFSOwnerGroupID          = 501
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 520
kMDItemFSTypeCode              = 1145392233
kMDItemID                      = 353548
kMDItemKeywords                = (toread)
kMDItemKind                    = "Del.icio.us Bookmark"
kMDItemLastUsedDate            = 2005-05-05 20:16:36 +0100
kMDItemURL                     = "http://www.macgeekery.com/tips/how_to_execute_raw_spotlight_queries_in_the_finder"
kMDItemUsedDates               = (2005-05-05 01:00:00 +0100)

The kMDItemKeywords value is (toread) which accurately reflects the tags I’ve assigned to it on del.icio.us. Typing kMDItemKeywords == "*toread*" into a Finder search field yields nothing however. I am not sure what the function of adding asterisks either side of the required value is but I put them in because there seemed to be a pattern of it in your example. Could you explain why you used them?

Adam Knight's picture

I downloaded and ran the program (spiffy program) and then went into the Finder and made a Raw Query for kMDItemKeywords == "*osx*" and got a list of the proper items. Did you make sure you had set the type to “Raw Query”?

Also, for this use, there is a Keywords field already in the Finder that maps to kMDItemKeywords so you can just choose “Keywords” and enter “toread” in that field to do what you are trying to do in this example.

cp

Thanks, that was helpful.

It looks like the Kind can be set to Del.icio.us Bookmark in the drop down menus too. I guess Spotlight automatically recognises this metadata when a new file kind is introduced into the filesystem, very clever.

So now my Smart Folder search (for part of my GTD Read/review folder) looks like this:
Kind — Others… — ‘Del.icio.us Bookmark’
Keywords — ‘toread’

I’d like to work harder on using Raw Queries to get all my bookmarks, labelled files, and whatnot into one folder. I’ll post my recipes when I do.

I used to keep a manually updated ‘Read/review’ folder for my GTD system by dragging aliases back and forth and deleting item no longer relevant. This took a lot out of my wrist and my memory—having to remember what you have dragged to Read/review when managing your support materials is an unnecessary effort IMO.

Thanks to Spotlight and codepoet’s raw query tips, I can now instead use a Finder label every item that counts as ‘Read/review’ (GTD users will understand this). This gives instant visual feedback from whereever I am viewing the file in the Finder: from the file’s containing folder or from the Read/review folder. Each file only needs updating—relabelling—once for the results to appear in both contexts i.e. it is removed from my Read/review folder and isn’t highlighted in my support materials.

I also tag all my del.icio.us bookmarks with ‘toread’ if I intend to read them later on and wanted to include them in the Smart Folder.

((kMDItemKeywords "*toread*") && (kMDItemKind "Del.icio.us Bookmark")) || (kMDItemFSLabel == '5')

The kMDItemFSLabel is set to 5 because I chose to use yellow as my Read/review colour (thinking it looks like a highlighter pen) and 5 mdls’s representation of that particular label colour (5th in the list of colours, I guess). I surrounded the query to find del.icio.us bookmarks tagged ‘toread’ with parenthesis so it could be compared to the single-part query that looks for yellow labelled files without affecting that query.

Required ingredient:

I still want to find a way to get my Safari bookmarks that I have put into a bookmark folder named—yes—Read/review. I also want to be able to include mail messages in my Read/review Smart Folder in Mail.app, which collects all my flagged messages.

The full documentation for Spotlight queries can be found at the Apple Developer Connection site:

Spotlight Query Programming

Query Expression Syntax

Post new comment
The content of this field is kept private and will not be shown publicly.
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.