Reveal Finder files with multiple criteria

As a long-time user of, admittedly, simpler aspects of KM, I tried to write a macro that would ask me for a word, then have the Finder real all files with multiple criteria--including of course, that the file's name includes that word, but also that it's a document and perhaps with the extension docx and so forth. Searching the manual, I do not see this. (I also tried Automator, which includes an action (tool) for criteria...but the variable I wish for the word was never passed to that tool.


I'm pretty sure you will need a script to achieve that, using the Spotlight search tools, either mdfind in a shell script, or using AppleScript Objective-C commands.

If you do a Forum search on "mdfind" you will find numerous examples. A Google search will also provide more info and more examples.

Also the Bash "find" command is very powerful, and may work as well, or better for your needs. It just depends on your requirements.

If you are unable to solve your problem, post back here and we'll try to help.
It would be best to provide the specific criteria you want to search for, including which folders to search.

Please read:
Tip: How Do I Get The Best Answer in the Shortest Time?

Thanks very much for writing. I'll pursue these!


Here's an example AppleScript using mdfind:

--»  Set Search Criteria Here for mdfin

set searchDir to "~/Documents/Test/"
set fileNameKeyWord to "TEST"
set fileExt to "docx"

# Example using literal text
#  set mdQueryStr to "  '(kMDItemFSName=\"*TEST*\"c && kMDItemFSName=\"*.docx\"c)'"

# Example using Variables
set mdQueryStr to "  '(kMDItemFSName=\"*" & fileNameKeyWord & "*\"c && kMDItemFSName=\"*." & fileExt & "\"c)'"

--»  Run the mdFind Query

set cmdStr to "mdfind -onlyin " & searchDir & mdQueryStr
set fileList to (do shell script cmdStr)

This is designed to be run from the Script, or better, Script Debugger 7, but it can be easily converted for use with KM using the Execute an AppleScript action.

You could easily setup a KM Prompt for User Input action to enter the search criteria, and then pass it to the script.

If you need help with that, just let us know.

Here's some references and data on using Spotlight mdfind that you may find useful:

Common Spotlight Metadata Attribute Keys

Spotlight syntax, mdfind examples, and metadata attributes

kMDItemPath: absolute path.
kMDItemFSName: basename.
kMDItemFSSize: size in bytes.
kMDItemDisplayName: display name.
kMDItemContentType: UTI.
kMDItemContentTypeTree: UTI and parent UTIs.
kMDItemKind: a localized name for the content type.
kMDItemFSContentChangeDate: modification time.
kMDItemFSCreationDate: creation time.
kMDItemLastUsedDate: date last opened.
kMDItemDateAdded: date added.
kMDItemFinderComment: Spotlight comment.
kMDItemTextContent: plain text content of for example text, HTML, or PDF files.
kMDItemDurationSeconds: for example the duration of an audio or video file.
kMDItemTitle: for example the title of a webloc, PDF, or MP3 file.
kMDItemURL: for example the URL of a webloc file.
kMDItemWhereFroms: the URL or URLs that a file was downloaded from.
kMDItemUserTags: tags in 10.9 and later.
kMDItemAuthors: for example the artist of an MP3 file.

I think one way to do this would be to programmatically create a "Saved Search" and put the file in

~/Library/Saved Searches/

or I would checkout HoudahSpot and see if there's a way to use that to do what you are trying to accomplish.

Thanks to both of you. I will try those....