Sub topic 7
Narrowing results by kind
A search for tax on a busy Mac can return a folder, three emails, a spreadsheet, a photo of a receipt, and a PDF. This chapter shows how to ask for only the kind of item you want. It covers the kind: keyword, its companion name:, and the Terminal equivalent that reads the same index.
It assumes you can open Spotlight and run a basic file search, as covered in earlier chapters. The Terminal section also assumes you can open Terminal and run a command.
Why kind matters
Spotlight searches names, file contents, and metadata at the same time. That breadth is useful, but common words match in many places. A word like report can appear in a file name, inside a document, and in a mail subject.
A kind filter limits results by what an item is, not by what it says. It works well with a short word, because the filter removes everything that is the wrong sort of item.
Every indexed item has a content type, such as PDF, image, or application. The kind: keyword matches against that classification.
The kind: keyword
Type kind: followed by a value, then add your search words. Order doesn't matter, and the filter and the words combine with an implicit AND. Both must match.
kind:pdf tax
kind:image receipt
kind:folder projects
tax kind:pdfThe first query returns only PDFs that match tax. The last returns the same results, because the position of the filter doesn't change the meaning.
Values that commonly work include these:
| Value | Matches |
|---|---|
| `kind:app` or `kind:application` | Applications |
| `kind:folder` | Folders |
| `kind:document` | Documents in general, including text and office files |
| `kind:pdf` | PDF files |
| `kind:image` | Photos, screenshots, and other images |
| `kind:music` or `kind:audio` | Audio files |
| `kind:movie` | Video files |
| `kind:presentation` | Slide decks |
| `kind:email` | Mail messages |
| `kind:contact` | Entries in Contacts |
| `kind:event` | Calendar events |
| `kind:bookmark` | Browser bookmarks |
| `kind:font` | Fonts |
Filtering with no search words
A kind filter can stand alone. kind:app lists applications, and kind:folder lists folders. This is a quick way to browse, but the list can be long. Add a word to cut it down:
kind:app calc
kind:folder invoicesThe name: keyword
Normally a word matches a file's name, its contents, and its metadata. If you want only file names, use name:.
name:invoice
name:invoice kind:pdfThe first query ignores files that merely mention "invoice" in their text. The second finds PDFs whose names contain "invoice". This is the single most effective way to reduce noise when a word is common in your writing.
Without name:, a search for budget also returns every email and document that mentions the word. With name:budget, you get the files named for it.
Combining kinds with operators
Kind filters work with the operators from the previous chapter, which must be written in capitals.
kind:pdf tax
budget NOT draft
invoice OR receipt
"quarterly report" kind:presentationYou can use these patterns to refine a search:
- Exclude noise:
kind:image screenshot NOT kind:pdfis rarely needed, butreport NOT kind:emailremoves mail from a broad search. - Accept alternatives:
kind:pdf OR kind:document contractfinds the contract whether it was saved as a PDF or as a word-processing file. Check the results, because grouping of mixed OR and AND queries can differ from what you expect. - Phrase plus kind: a quoted phrase with a kind is the most precise form. It requires the exact words in an item of the right type.
Kind filters in Finder
Finder's search field accepts the same keywords, but it also offers a visual alternative. After you search in a Finder window, click the + button under the search field. Choose Kind from the first menu, then pick a value such as PDF, Image, or Folder from the second.
This adds a condition row. You can add more rows, for example Kind is PDF and Name contains invoice. Rows are combined with AND. Finder's menu is useful when you don't remember the keyword, and it is the path you need to save the search as a Smart Folder, covered in a later chapter.
Doing the same in Terminal
mdfind queries the same index, and its kind condition is more precise than the typed keyword. Two attributes matter:
kMDItemContentTypeis the exact content type identifier of one file type.kMDItemContentTypeTreeincludes the type and all the types it belongs to.
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf'"This returns only PDF files under the Documents folder. The identifier com.adobe.pdf is a Uniform Type Identifier (UTI), a standard name for a file type.
The tree attribute is how you match a whole family of types:
mdfind -onlyin ~/Pictures "kMDItemContentTypeTree == 'public.image'"A JPEG, a PNG, and a HEIC photo all conform to public.image, so one query finds all of them. This is the equivalent of kind:image.
You can combine a kind with a name condition by joining them with &&:
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf' && kMDItemFSName == '*invoice*'c"The * characters are wildcards. The trailing c makes the comparison case-insensitive.
Finding the right identifier
If you don't know the identifier for a file type, ask for the metadata of a file you already have:
mdls -name kMDItemContentType -name kMDItemContentTypeTree -name kMDItemKind ~/Documents/report.pdfThe output shows the content type, the tree, and the human-readable kind. Copy the identifier into your mdfind query. This is the reliable way to build a query for an unusual file type.
Kind versus content type
kMDItemKind holds the text label shown in Finder, such as "PDF Document". That label is localized, so it changes with the system language, and it is a poor basis for scripts. A script that uses kMDItemKind == 'PDF Document' fails on a Mac set to another language. Use kMDItemContentType or kMDItemContentTypeTree instead.
Common problems
- A kind returns nothing. The value may not be accepted on your version, or no indexed item of that kind exists. Try a synonym, then remove the search words to test the filter alone.
- Expected files are missing. The file may be in an excluded folder, or on a volume that isn't indexed. The troubleshooting chapter covers checking the index with
mdutil -s /. - Operators are ignored.
orandnotin lowercase are treated as ordinary words. WriteORandNOT. - Spaces after the colon.
kind: pdfdoes not work as a filter. Keep the value attached to the colon:kind:pdf. - Results include more than you wanted.
kind:documentis broad, and it covers many file formats. Usekind:pdfor a more specific type when you know what you need. - Newly saved files don't appear yet. Spotlight indexes in the background, so a very recent file can take a moment to show up.
Choosing between the methods
| Situation | Best method |
|---|---|
| Quick one-off lookup | Spotlight with `kind:` and a word |
| Common word, only file names matter | `name:` plus `kind:` |
| You don't remember the keyword | Finder's **+** condition rows |
| You want the search to stay available | Finder search saved as a Smart Folder |
| Script or exact file type | `mdfind` with `kMDItemContentType` |
| A family of types, such as all images | `mdfind` with `kMDItemContentTypeTree` |
Exercise
You want PDFs whose file names contain "invoice", but not drafts. Write a Spotlight query for this. Then write an mdfind command that finds PDFs under ~/Documents whose names contain "invoice", and explain why it uses kMDItemContentType rather than kMDItemKind.
Show answer
Spotlight: name:invoice kind:pdf NOT draft. Terminal: mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf' && kMDItemFSName == '*invoice*'c". It uses kMDItemContentType because that is a language-independent type identifier. kMDItemKind holds a localized label such as "PDF Document", so a query built on it breaks when the system language changes.
Exercise
You run mdls ~/Pictures/photo.heic and want one mdfind query that finds all image files, not just HEIC. Which attribute and value do you use?
Show answer
Use kMDItemContentTypeTree == 'public.image', for example mdfind -onlyin ~/Pictures "kMDItemContentTypeTree == 'public.image'". The content type tree lists every type a file conforms to, and JPEG, PNG, and HEIC files all conform to public.image.