How to run a shell script using Spotlight?
Asked Answered
W

4

49

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.

Watts answered 20/5, 2010 at 20:55 Comment(0)
H
67

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.

Henshaw answered 20/5, 2010 at 21:5 Comment(11)
It works, but terminal with message [Process completed] comes up, which has to be closed manually. Is there a way to avoid this?Urceolate
@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. Finally, you could make a small application to run shell scripts and set this as the default for running all or particular shell scripts.Demount
I 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"Toughie
@user179589: try posting a new question with more detail as to what you want to achieve, what you tried and what went wrong.Henshaw
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?Toughie
@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.Henshaw
I'm explaining better in this post. What I meant is to pass the parameter when calling the script from the spotlight.Toughie
@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...Henshaw
You 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 "<script_name>.command")' & Just adjust your script name.Exhilarant
remember to use chmod u+x /path/to/file to grant it execution privilegeFoah
You also need to select "Developer" in Spotlight preferences. If you unchecked this script won't appear.Celeski
V
10

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

Velites answered 28/3, 2021 at 20:35 Comment(0)
S
4

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.

Schwann answered 31/3, 2013 at 12:58 Comment(0)
S
2

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

Soursop answered 31/8, 2020 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.