mdfind ( query -- results )


Vocabulary
spotlight

Inputs
querya string


Outputs
resultsa sequence


Word description
Finds files matching a given query.

mdfind [-live] [-count] [-onlyin directory] [-name fileName] query

The mdfind command consults the central metadata store and returns a list of files that match the given metadata query. The query can be a string or a query expression.

Spotlight Keywords
These can be included in the query expression to limit the type of documents returned:
Applicationskind:application, kind:applications, kind:app
Audio/Musickind:audio, kind:music
Bookmarkskind:bookmark, kind:bookmarks
Contactskind:contact, kind:contacts
Emailkind:email, kind:emails, kind:mail message, kind:mail messages
Folderskind:folder, kind:folders
Fontskind:font, kind:fonts
iCal Eventskind:event, kind:events
iCal To Doskind:todo, kind:todos, kind:to do, kind:to dos
Imageskind:image, kind:images
Movieskind:movie, kind:movies
PDFkind:pdf, kind:pdfs
Preferenceskind:system preferences, kind:preferences
Presentationskind:presentations, kind:presentation


Date Keywords
These can be included in the query expression to limit the age of documents returned:
date:today$time.today()
date:yesterday$time.yesterday()
date:this week$time.this_week()
date:this month$time.this_month()
date:this year$time.this_year()
date:tomorrow$time.tomorrow()
date:next month$time.next_month()
date:next week$time.next_week()
date:next year$time.next_year()


Boolean Operators
By default mdfind will AND together elements of the query string.
| (OR)to return items that match either word, use the pipe character: stringA|stringB
- (NOT)to exclude documents that match a string: -string
==equal
!=not equal
< and >"less" or "more than"
<= and >="less than or equal" or "more than or equal"


Examples
Return all files that have been modified today
"date:today" mdfind

Return all files that have been modified in the last 3 days
"kMDItemFSContentChangeDate >= $time.today (-3)" mdfind

Returns files with particular attributes
"com.microsoft.word.doc" "kMDItemContentType" attr== mdfind

Look for files with a particular file name
"Finding Joy in Combinators.pdf" "kMDItemFSName" attr== mdfind

Look for terms in documents
"Document cocoa.messages selector" mdfind

Return all files in the users home folder that have been modified in the last 3 days
"~" [ "kMDItemFSContentChangeDate >= $time.today (-3)" mdfind ] with-directory


Notes
This word uses the current-directory to restrict the search, choosing to search from the root ('/') if not set.

Definition


: mdfind ( query -- results )
"mdfind -onlyin . %s" sprintf process-lines ;