Wolfram Research

Function Repository Resource:

Spotlight

Source Notebook

Access the Spotlight feature in macOS from within the Wolfram Language

Contributed by: Lou D'Andria

ResourceFunction["Spotlight"][query]

uses the macOS Spotlight feature to search files at any level in your current working directory for the given string or query expression and returns a list of file paths.

ResourceFunction["Spotlight"][query, dir]

searches files at any level in the given directory.

ResourceFunction["Spotlight"][query, All]

searches all available file systems.

Details and Options

Spotlight uses the macOS command line mdfind utility, which only searches locations that you have allowed Spotlight to index in your macOS system preferences.
Spotlight[query] is equivalent to Spotlight[query, Directory[]].
dir can be any string or File object representing a directory on your machine.
Spotlight queries can include Booleans AND, OR and NOT and keywords like name:, date: and kind:.
Spotlight query expressions can include arbitrary Spotlight metadata attribute keys, such as those found here.
The command line mdls utility can list the metadata attributes for a given file.

Examples

Basic Examples

Search files in your current directory for the string “elephant”:

In[1]:=
ResourceFunction["Spotlight"]["elephant"] // Short
Out[1]=

Search on your current file system:

In[2]:=
ResourceFunction["Spotlight"]["elephant", $RootDirectory] // Length
Out[2]=

Search on all available file systems indexed by Spotlight:

In[3]:=
ResourceFunction["Spotlight"]["elephant", All] // Length
Out[3]=

Search for files that contain all the given words:

In[4]:=
ResourceFunction["Spotlight"]["raven quoth"] // Length
Out[4]=

Search for files that contain one word but not another:

In[5]:=
ResourceFunction["Spotlight"]["raven NOT quoth"] // Length
Out[5]=

Search for files that were created or modified on a particular date:

In[6]:=
ResourceFunction["Spotlight"]["date:2/14/2019"] // Length
Out[6]=

Search for any folder that has “Library” in its name:

In[7]:=
ResourceFunction["Spotlight"]["name:Library kind:folder"] // Length
Out[7]=

Search for music files by a particular artist:

In[8]:=
ResourceFunction["Spotlight"]["Wynton kind:music"] // Length
Out[8]=

Scope

Use metadata attributes to find Wolfram notebooks at any level in the current directory:

In[9]:=
ResourceFunction["Spotlight"][
  "'kMDItemContentType == \"com.wolfram.notebook\"'"] // Length
Out[9]=

Find notebooks that have “the” in their file name:

In[10]:=
ResourceFunction["Spotlight"][
  "'kMDItemContentType == \"com.wolfram.notebook\" && kMDItemFSName == \
\"*the*\"'"] // Length
Out[10]=

Find notebooks that have “the” in their file name and “elephant” in their text content:

In[11]:=
ResourceFunction["Spotlight"][
  "'kMDItemContentType == \"com.wolfram.notebook\" && kMDItemFSName == \
\"*the*\" && kMDItemTextContent == \"*elephant*\"'"] // Length
Out[11]=

Find movie files that are longer than 60 seconds:

In[12]:=
ResourceFunction["Spotlight"][
  "'kMDItemKind == \"QuickTime movie\" && kMDItemDurationSeconds > \
60'"] // Length
Out[12]=

Find PNG files that have an alpha channel and more than 5 million pixels:

In[13]:=
ResourceFunction["Spotlight"][
  "'kMDItemKind == \"PNG image\" && kMDItemHasAlphaChannel == 1 && \
kMDItemPixelCount > 5000000'"] // Length
Out[13]=

Find files that have been changed in the last week:

In[14]:=
ResourceFunction["Spotlight"][
  "'kMDItemFSContentChangeDate >= $time.this_week(-1)'"] // Length
Out[14]=

Properties and Relations

File lists can be analyzed in all the usual ways:

In[15]:=
Tally[FileExtension /@ ResourceFunction["Spotlight"]["elephant"]]
Out[15]=

Import a random PNG image with an alpha channel:

In[16]:=
Import @ RandomChoice @ ResourceFunction["Spotlight"][
   "'kMDItemKind == \"PNG image\" && kMDItemHasAlphaChannel == 1'"]
Out[16]=

Files found by Spotlight can be opened with SystemOpen:


FindList can be used to extract lines containing a given string:

In[17]:=
FindList[RandomChoice @ ResourceFunction["Spotlight"]["blue"], "blue"]
Out[17]=

Possible Issues

Spotlight does not index files inside of .app bundles, such as your Wolfram System installation directory:

In[18]:=
ResourceFunction["Spotlight"]["DSolve", $InstallationDirectory]
Out[18]=

Spotlight can find matches in a file’s metadata, so a file’s contents may not contain the search string:

In[19]:=
Select[Echo[ResourceFunction["Spotlight"]["elephant"], "total: ", Length], Quiet[FindList[#, "elephant"] === {}] &] // Length
Out[19]=

If quotes in your search string are not properly escaped, mdfind will give an error and return a nonzero exit code:

In[20]:=
ResourceFunction["Spotlight"]["fish'"]
Out[20]=

Neat Examples

Learn about notebook-specific metadata keys and values with the mdls utility:

In[21]:=
result = RunProcess[$SystemShell, All, "mdls " <> ToString[NotebookFileName[], InputForm]];
Select[ImportString[result["StandardOutput"], "Lines"], StringContainsQ["notebook"]] // Column
Out[22]=

Use that metadata to find notebook files that have more than a thousand input cells:

In[23]:=
ResourceFunction["Spotlight"][
  "'kMDItemContentType == \"com.wolfram.notebook\" && \
com_wolfram_notebook_InputCellCount > 1000'"] // Length
Out[23]=

Find notebooks that use a nondefault stylesheet:

In[24]:=
ResourceFunction["Spotlight"][
  "'kMDItemContentType == \"com.wolfram.notebook\" && \
com_wolfram_notebook_StyleSheet != \"Default.nb\"'"] // Length
Out[24]=

Find palette notebooks:

In[25]:=
ResourceFunction["Spotlight"][
  "'kMDItemContentType == \"com.wolfram.notebook\" && \
com_wolfram_notebook_Type == \"Palette\"'"] // Length
Out[25]=

Learn about metadata keys and values for any file of interest with the mdls utility:

In[26]:=
result = RunProcess[$SystemShell, All, "mdls /System/Library/Spotlight/iCal.mdimporter"];
ImportString[result["StandardOutput"], {"Lines", Range[5]}] // Column
Out[27]=

Use that metadata to search for files with the same content type:

In[28]:=
ResourceFunction["Spotlight"][
  "'kMDItemContentType == \"com.apple.metadata-importer\"'", \
$RootDirectory] // Length
Out[28]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information