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.
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.
#!/bin/bash echo "That's the word" $1
. How can I execute this script passing a parameter? –
Toughie osascript -e 'tell application "Terminal" to close (every window whose name contains "<script_name>.command")' &
Just adjust your script name. –
Exhilarant chmod u+x /path/to/file
to grant it execution privilege –
Foah 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
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.
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
© 2022 - 2024 — McMap. All rights reserved.
[Process completed]
comes up, which has to be closed manually. Is there a way to avoid this? – Urceolate