search spotlight from terminal

I would like to be able to search spotlight from the terminal.

I'm aware of mdfind, but it doesn't sort by relevance.


ex:

for me, typing "vs" into spotlight returns "Visual Studio Code" as the instant top hit with 17 other options

in contrast `mdfind "vs"` spews hundreds of possibilities, matching any file/folder that has "vs" in it.


I've looked at the man pages and spotlight query syntax, but haven't seen anything about sorting by relevance or partial searches. Sorting by time/date/location is easy, I can do that with the regular unix find command, I just need a way to tap into the "top hits" section of spotlight from terminal.


I imagine there is a debugging tool somewhere that would be able to do this.

I'd also be fine with an Applescript solution since AppleScript can be run through the terminal.


Thanks in advance.

Posted on Jan 20, 2018 9:01 PM

Reply
4 replies

Jan 21, 2018 7:13 AM in response to hum0nx

From the Terminal, had you run the following, it likely would have drastically narrowed your search result to the Visual Studio Code application:


$ mdfind 'kMDItemKind == "Application"c && kMDItemFSName == "Visual*"c'

or

$locate -i -l 3 visual


In my case, changing Visual to Sublime results in finding only Sublime Text 3.app on my Mac.


From an AppleScript perspective:


$ osascript visual.applescript


Would pop a Finder window with the application revealed in it.


-- visual.applescript

-- perform a the Spotlight query and return a Finder window with the selection(s)


property NSWorkspace : a reference to current application's NSWorkspace


use framework "Cocoa"

use AppleScriptversion "2.4" -- Yosemitie or later

use scripting additions


-- find the application whose name has "Visual" in it.

set query to "kind:application AND name:Visual"


-- pop a Finder window with the query results as if entered into the Finder search panel

set result_code to (NSWorkspace'ssharedWorkspace)'s showSearchResultsForQueryString:query

return

Jan 21, 2018 3:42 PM in response to hum0nx

VikingOSX Is correct. Writing a shell script that looks in Applications is a few lines of bash. Place that script in your path, mark the script as executable, and you’ll have what you want.


Here are the commands central to the script...


mdfind fooh -onlyin /Applications


Or for this case, maybe....


find /Applications/ -iname foo


Related:


https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPag es/man1/mdfind.1.html


https://developer.apple.com/library/content/documentation/OpenSource/Conceptual/ ShellScripting/Introduction/Introduction…

Jan 21, 2018 7:49 AM in response to VikingOSX

Thanks, but I was looking for a more general solution.

For example have "ch" return the chrome app, "renderer" return my renderer.js file, "sch" return my scheduler.numbers file, and "Downloads" return the downloads folder.


Each of those searches do return those items as the top hit in spotlight without specifying additional parameters (application/folder/file). Having to manually specify application/file/folder in terminal world defeat the point of it being quick to use.


Thanks for responding though.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

search spotlight from terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.