How to Launch Spotlight from the Terminal

2

I used this tip top hide my menu bar in a bunch of applications, which is a great way to get more free screen space. The one downside is that (for inexplicable reasons) it disables Spotlight when I'm in those applications--e.g. even if I hover over the menu bar to make it appear, clicking on the Spotlight icon does nothing.

I have a plan to work around this: I'd like to launch Spotlight using QuicKeys, which lets you run terminal commands using keyboard shortcuts. But to do that, I need to know how to launch Spotlight with a terminal command. Does anyone know how to do this?

I'm on OS X 10.6.

Jack7890

Posted 2010-12-25T02:40:27.460

Reputation: 229

The official keyboard command for Spotlight is Cmd+Space. Does that work with the menu hiding script? – digitxp – 2010-12-25T04:21:49.650

No, unfortunately it doesn't. – Jack7890 – 2010-12-25T05:39:10.087

Answers

3

You can use the command-line program mdfind as a complete substitution for the Spotlight GUI. Accessing results is a bit inconvenient, though.


You can, using a little GUI scripting, open a Finder window, and perform a Spotlight search there.

For this to work, you need support for assistive devices in System Preferences » Universal Access and Full Keyboard Access in System Preferences » Keyboard » Keyboard Shortcuts.

Open /Applications/Utilities/AppleScript Editor.app, and enter the following script:

on run argv # we expect program arguments
    tell application "Finder"
        activate # focus Finder
        tell application "System Events"
            keystroke "f" using command down # press Cmd-F
            keystroke (item 1 of argv) # enter the program argument into search box
            key code 36 # press enter
            key code 48 # press tab
            keystroke " " # press space
        end tell
    end tell
end run

The steps "press tab" and "press space" are only necessary if you have not specified Search This Mac in Finder » Preferences » Advanced -- then we tab&select the This Mac option in the Finder search window.

Now save as Script, and execute from Terminal like this:

osascript spotlight.scpt test

What happens then looks somewhat like this:

alt text


The "Spotlight" action for Automator is probably not a good idea, since it runs until all results are available. Combined with Reveal Finder Items is a great way to see what opening 10,000 Finder windows looks like.


Other options, as always, are using applications such as LaunchBar, Alfred or Butler to initiate Spotlight search or provide an alternative UI.

Daniel Beck

Posted 2010-12-25T02:40:27.460

Reputation: 98 421

0

From the Spotlight manual :

There are no configurations to Spotlight, and users should not run manually.

Guess that explains it all.

ismail

Posted 2010-12-25T02:40:27.460

Reputation: 507

It refers to the Spotlight GUI process. Not Spotlight more broadly. There is e.g. the mdfind command-line application. – Daniel Beck – 2010-12-25T17:45:47.907

And the author asks for the Spotlight "GUI" – ismail – 2010-12-25T22:20:06.880