Is there a way to run a shell script using Spotlight on Mac OS X 10.6? I would like to be able to invoke a shell script directly from Spotlight without opening up a terminal.
4 Answers
Save your shell script with a .command
suffix - this makes it double-clickable and you should also be able to run it directly from Spotlight too.

- 208,748
- 37
- 389
- 560
-
2It works, but terminal with message `[Process completed]` comes up, which has to be closed manually. Is there a way to avoid this? – Chakradar Raju Jan 26 '14 at 05:30
-
16@ChakradarRaju You can change your Terminal preferences to make the window close when the process completes. Alternatively, you can wrap your shell script in an application bundle - then it won't pop up the window at all. [See this.](https://mathiasbynens.be/notes/shell-script-mac-apps) Finally, you could make a small application to run shell scripts and set this as the default for running all or particular shell scripts. – Brian McCutchon Oct 30 '14 at 02:28
-
2I was trying to pass a parameter but couldn't make it. Is there some way of doing this? I created a simple script with echo "Parameter $1" but it is not recognized when I execute "myscript.command anything" – Arthur Accioly Feb 29 '16 at 22:41
-
1@user179589: try posting a new question with more detail as to what you want to achieve, what you tried and what went wrong. – Paul R Feb 29 '16 at 22:52
-
Sorry, let me explain better. I want to pass a parameter to the script that I'm executing. For example, let's say that I have echo.command with the following code: `#!/bin/bash echo "That's the word" $1` . How can I execute this script passing a parameter? – Arthur Accioly Feb 29 '16 at 23:02
-
@user179589: you mean when you double-click the .command script on the the Finder desktop? How would you even pass command line parameters in that context? You can still run it from the Terminal command line in the usual way of course, and pass parameters in the usual way. If you want to do something more complicated than that then you probably need to post it as a new question and explain what you're trying to achieve. – Paul R Feb 29 '16 at 23:11
-
I'm explaining better in this [post](http://stackoverflow.com/questions/35711642/how-to-run-a-shell-script-using-spotlight-passing-a-parameter). What I meant is to pass the parameter when calling the script from the spotlight. – Arthur Accioly Feb 29 '16 at 23:16
-
@user179589: OK - I see - no I don't think that will work - Spotlight is not a command line, it's just a search tool - it won't pass parameters to an item in a search result. But I could be wrong - let's see if you get any answers to your question... – Paul R Feb 29 '16 at 23:21
-
2You can close the terminal window by executing a piece of AppleScript at the end of your shell script, e.g.: `osascript -e 'tell application "Terminal" to close (every window whose name contains "
.command")' &` Just adjust your script name. – saschor Nov 08 '16 at 14:59 -
3remember to use `chmod u+x /path/to/file` to grant it execution privilege – Killy Sep 10 '18 at 07:24
-
You also need to select "Developer" in Spotlight preferences. If you unchecked this script won't appear. – Wojciech Kulik Apr 16 '21 at 11:21
Another approach that completely avoids opening a Terminal:
Open Script Editor on your Mac, make sure AppleScript is selected from the language dropdown and type
do shell script "touch ~/testfile"
replacing touch ~/testfile
with your code of choice--as you can see it need not have a .command suffix nor even actually be a script.
Now go to File | Export and select Application from the File Format: dropdown. Make sure all the Options: are unchecked and Don't Code Sign is selected from the Code Sign: dropdown. Name it whatever you like, save it wherever you like. Now you can double-click your new AppleScript application or run it from Spotlight and your script will run without a Terminal window opening.
As a bonus since it's AppleScript it can interact with the Mac UI--show results in a display dialog, get user input, etc. https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html

- 111
- 1
- 1
I would also like to add, if you need the terminal window to disappear after running the command, you can set the terminal settings to do just that.
It's under settings >> shell >> when the shell exits.
This will still leave Terminal running but the script will at least clean up after itself.
I used this for writing a script to hide and show desktop icons.

- 1,180
- 11
- 13
To make the Terminal window disappear when finished as of MacOS >= 10.0:
Terminal
> Preferences
> Select your default profile
Then Shell
> When the shell exits
select Close if the shell exited cleanly

- 2,017
- 1
- 21
- 30