Executing Shell Scripts from the OS X Dock?
How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?
Solution 1:
You could create a Automator workflow with a single step - "Run Shell Script"
Then File > Save As
, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.
The benefit to this is it's really simple to do, and you can very easily get user input (say, selecting a bunch of files), then pass it to the input of the shell script (either to stdin, or as arguments).
(Automator is in your /Applications
folder!)
Solution 2:
If you don't need a Terminal window, you can make any executable file an Application just by creating a shell script Example
and moving it to the filename Example.app/Contents/MacOS/Example
. You can place this new application in your dock like any other, and execute it with a click.
NOTE: the name of the app must exactly match the script name. So the top level directory has to be Example.app
and the script in the Contents/MacOS
subdirectory must be named Example
, and the script must be executable.
If you do need to have the terminal window displayed, I don't have a simple solution. You could probably do something with Applescript, but that's not very clean.
Solution 3:
On OSX Mavericks:
- Create your shell script.
-
Make your shell script executable:
chmod +x your-shell-script.sh
-
Rename your script to have a
.app
suffix:mv your-shell-script.sh your-shell-script.app
- Drag the script to the OSX dock.
-
Rename your script back to a
.sh
suffix:mv your-shell-script.app your-shell-script.sh
- Right-click the file in Finder, and click the "Get Info" option.
- At the bottom of the window, set the shell script to open with the terminal.
Now when you click on the script in the dock, A terminal window will pop up and execute your script.
Bonus: To get the terminal to close when your script has completed, add exit 0
to the end and change the terminal settings to "close the shell if exited cleanly" like it says to do in this SO answer.
Solution 4:
I know this is old but in case it is helpful to others:
If you need to run a script and want the terminal to pop up so you can see the results you can do like Abyss Knight said and change the extension to .command. If you double click on it it will open a terminal window and run.
I however needed this to run from automator or appleScript. So to get this to open a new terminal the command I ran from "run shell script" was "open myShellScript.command" and it opened in a new terminal.