Skip to content

Spotlight

Ken Harris edited this page Aug 26, 2020 · 2 revisions

"Spotlight" is a brand name that refers to a couple of different technologies, which you interact with in some very different ways.

  • Spotlight "classic" is for documents (i.e., files)

    • To index files, you used to need to write a CFPlugin using a special project template, but haven't shipped this in Xcode since Xcode 8 (2016), and it calls deprecated functions, so (TBD!)

    • To search files, use NSMetadataQuery. It's quirky but it mostly works.

  • Core Spotlight is for making parts of your application (not documents) searchable, for up to "a few thousand items", and the index is per-user. I think it appeared on iOS first, but either way, it's definitely an iOS-style search platform.

    • There's a fairly recent CoreSpotlight.framework with ObjC/Swift interfaces for interacting with this.

Some handy command-line tools:

  • mdimport -A lists all the attributes
  • mdls somefile lists all of that file's attributes
  • mdfind -live -onlyin ~/Downloads/ "kMDItemContentTypeTree == 'public.image'" runs a live (and live-updating) search for a Spotlight query (BUT note that the syntax used by this tool is different from the syntax used by NSMetadataQuery!)

Does Spotlight support invisible files? Sort of not really.

  • There's an attribute kMDItemFSInvisible which looks like it would work
  • There's (at least) 3 ways to make files invisible on macOS: chflags hidden, setfile -a V, and giving it a name that starts with .
  • The Spotlight attribute works with setfile-hidden files only. For the other two, it pretends the file doesn't exist.

So you basically can't use Spotlight if you care about hidden/invisible files. Users aren't going to appreciate the whole "3 kinds of invisible files" mess, and your program will just look broken to 2/3 of them.

Clone this wiki locally