my reservations about Spotlight, it really can do some amazing things. Consider digital photographs. With every image you snap, a wealth of data is recorded with the picture. Exactly what information your camera captures depends on its make, but most include basic items such as aperture, focal length, and exposure (shutter speed). When you place photos on your Mac, Spotlight reads and indexes this information, making it possible to run some interesting searches.
For instance, you might ask Spotlight to find all of your images with longer exposure—perhaps those with exposure times that were over a third of a second or so. (A normal exposure time might be closer to 1/60th of a second.) To do this you’d probably switch to the Finder, hit Command-F to bring up the search box, and then click the Plus sign to add a condition. You’d use the Kind pop-up to choose Other, then select Exposure Time from the long list of options. In the next pop-up, you’d select Greater Than, and then enter .333 in the input box next to that. When complete, the query would look like this:
When you run the search, however, you’ll get quite the surprise: it will find every image that has an exposure time value. Why? Because Spotlight doesn’t like the non-integer value—.33. It converts this number to a zero before running the query. Hence, it finds every photo that has a “greater than zero” exposure time, which will be all of them.
This doesn’t just happen with values that are less than one; it happens any time you want to try to find a non-integer value for an image-related field. Spotlight isn’t rounding the entries up to the next whole number, either, it’s just taking the integer portion. So if you were to try searches with values of 2.99, 2.50, and 2.25 for a certain field, Spotlight would simply use 2.0 as the search value.
How can you avoid this problem when searching for image-related Spotlight data? You need to use a Raw Query . In other words, you need to compose your query in Spotlight’s native language. Every time you search for something, Spotlight converts your natural-looking query into this language. So when you search for files using the search string Exposure Time Greater Than 2, for example, Spotlight converts that string into a math expression that looks like this:
kMDItemExposureTimeSeconds > 2
kMD… bit is the name of the actual field (or metadata ) that holds the exposure time.
To create a Raw Query that solves our integer problem, bring up the New Search window (Command-F) and choose Other from the Kind pop-up menu. In the list that appears, choose Raw Query and click on OK:
(Notice that I’ve selected the Add to Favorites option. This places the Raw Query entry directly on the list of items in the Kind pop-up menu, making it much easier to reach in the future.)
In the Raw Query text field, type
kMDItemExposureTimeSeconds > 0.333. The search will run as soon as you type in the numerical value. When the search is done, you’ll find the results to be much different than what you got with the search string you used before:
As you can see in the bottom window, there are only 15 images on my machine with exposure times over a third of a second, not 8,152 as indicated by the results in the topmost window.
So that’s how you solve the problem. But how did I find out the crazy term (
kMDItemExposureTimeSeconds ) Spotlight uses instead of “Exposure Time”? That’s good to know, because, of course, you can use Raw Query mode for any search, not just to work around this integer value problem. The easiest way to find out what metadata you can search for is to open Terminal (/Applications -> Utilities) and then change into a directory containing images, such as your user folder -> Pictures or your user folder -> iPhoto Library folder.
To do this with your Pictures folder, type the following in Terminal, and then press Return:
Now you just need to find a photograph so you can see what metadata it holds. The command
ls will show you what’s inside the folder. To see what Spotlight fields you can query on the file, type
mdls “somephoto.jpg”, where
somephoto.jpg is the name of the photo you’d like to check.
When you press Return, you’ll see a long list of items that start with the letters
kMDItem…. These are the metadata items that you can use in a raw query. If you’re searching for time-related items in a photograph, for instance, these are the three fields you need to know:
kMDItemAperture – Aperture setting.
kMDItemExposureTimeSeconds – Exposure time.
kMDItemFocalLength – Focal length.
You can use any of these you like in a Raw Query to find photographs based on non-integer values. Once you’ve run a search once, remember you can use the Save button to make it easy to recall that query again in the future. If you do this kind of thing a lot, consider setting up three saved queries, one for each field. When needed, just opened the saved search, and then edit the comparison and value as necessary for the search you’d like to run. Hopefully a future OS X update will address this problem, as these fields should be fully searchable using the standard Spotlight interface.
Intrigued? To learn more about using Raw Queries, see
“Cut Through the Clutter.” To learn more about Spotlight metadata, see
“Command the Spotlight.”