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?
-
2should +1 solely for the avatar, though i like the topic too. – Matt Bannert Aug 17 '10 at 08:37
-
You might want to consider switch accepted answers to the 2nd one @Wilco. I think it's a better answer. – Gray Jun 11 '20 at 22:04
-
See also: [How can I launch a script from the MacOS X dock?](https://stackoverflow.com/questions/813625) – hippietrail Mar 04 '21 at 03:18
12 Answers
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!)

- 165,801
- 69
- 278
- 343
-
6@dbr, GUI button pusher! :) Just kidding, that is a good suggestion! – willasaywhat Nov 11 '08 at 18:20
-
Yeah, it was an excellent suggestion :) I've even written Automator actions for some of my apps and I still forget about it in times like this :) – Jason Coco Nov 11 '08 at 23:42
-
I can't get this to work with nohup, a la "nohup /usr/bin/myscript & ; disown -a && exit 0". I need the app/workflow to run a script and exit immediately. Any ideas? :-) – hourback Jan 23 '10 at 03:02
-
@Draco You don't need the `;` after the first `&`, and it might help using the full path to `disown`? – dbr Jan 23 '10 at 14:30
-
Can't get this to work... My simple script: R CMD Sweave $1 | pdflatex | open – Matt Bannert Aug 17 '10 at 10:50
-
I have a script which launches an GUI appl with some command line arguments and used this technique to turn it into Automator "Application". It works fine, but I'd like to know if there's also some way to shift focus to the launched appl. – martineau Feb 06 '14 at 02:19
-
3@martineau If you open the application by doing `open -a SomeAppThing.app --args --etc=2` it should focus. Alternatively you can focus it using Applescript (e.g `osascript -e 'tell application "Google Chrome" to activate'`) – dbr Feb 08 '14 at 02:20
-
Thanks, the [open](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/open.1.html) command does exactly what's needed. – martineau Feb 08 '14 at 09:06
-
2Note: if your script is launching a GUI app which will remain open for a while, this will show a constantly-spinning gear icon in the taskbar until the app is closed. One work-around is to add ` &>/dev/null &` to the launching-line of the script so Automator doesn't wait around. – ewall Aug 12 '15 at 01:23
-
-
FYI Does not work with "fancy" scripts from my couple of attempts. If I issue basic commands in the script it works but I have a script that launches logins to other systems and it never works right. Get an error every time. – Mike Q Apr 26 '17 at 21:28
-
(MacOS 10.12.6) This answer, by and large, works well. However, my 'app' does not always open in front. I have tried both suggestions made by @dbr to move the window to the front but neither work consistently. For example, if I have Firefox focused and I invoke my app through the launchpad, Firefox remains in front with my app behind it (even if I click the desktop first to take focus away from Firefox). However, if I give focus to Finder and then launch my app, it is placed at the front. How to make this work consistently? – gone Sep 05 '17 at 13:28
-
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.
-
2You don't happen to know if there is an easy way to have files passed as arguments to the shell script? For files opened using "open with...". – Fuzzy76 Jun 14 '11 at 12:05
-
-
11For anyone trying to do this--the name of the app must exactly match the script name. So it really has to be `example.sh.app`, not just `example.app`. You will get a warning about damaged app if these do not match – Kip Aug 15 '13 at 02:35
-
1To clarify -- I'm an OSX noob -- you mean just create a folder structure like the one shown in your example with the script file placed in it as shown? – martineau Jan 27 '14 at 21:37
-
4@martineau Yes, that's correct. If you create a folder whose name ends in `.app` and with that structure, it will be treated as an executable Application. If you want to look at the contents of that folder in the Finder, you'll need to right click on it and select "view package contents" (because double-clicking on it would run the application). In the Terminal, it will just appear to be a folder. (I'm not sure if Finder will let you rename the folder to .app by itself; you may need to do the rename in the Terminal.) – Jeremy Jan 27 '14 at 21:48
-
@stiemannkj1 not so, I'm using 10.9.5 here and just followed these instructions to successfully make a custom launcher for Emacs (so it inherits the shell settings from .bashrc). I also combined it with [this answer](http://stackoverflow.com/a/24073988/2801707) to another question to get my EmacsLauncher.app to use the standard Emacs icon. When you tried it, you did remember to make the script part executable, right? – Ben Dec 31 '14 at 08:36
-
@Ben, I stand corrected. I'm pretty sure that I made the script executable, but I also had to make the `example.sh.app` part executable to get this to work. Maybe that's what I missed. Do you think that is worth editing into this answer or is that obvious to everyone but me? – stiemannkj1 Dec 31 '14 at 14:59
-
@stiemannkj1 I believe the executable bit is set on directories such as `example.sh.app` by default, so it shouldn't be necessary to manually set it. However, this post *could* probably be improved and made clearer, and I haven't tested it against current versions of OS X. I'll probably edit it soon. – Jeremy Dec 31 '14 at 20:30
-
@stiemannkj1 I believe Jeremy is correct, the executable bit is active by default (755) and it is the executable bit which allows anything within to be run. Web servers need the same thing for directory contents to be viewed (loaded by the web server). This isn't an OS X thing, BTW, it's the same on all *nix systems. – Ben Jan 04 '15 at 12:27
-
2I just attempted this, and received an error in El Capitan (10.11) of: "You can't open .... because powerpc applications are no longer supported." :D That's quite interesting! – AllenH Oct 09 '15 at 09:49
-
@AllenH [Related question on Apple.SE](http://apple.stackexchange.com/questions/225368/osx-10-11-incorrectly-sees-application-as-powerpc). But I tried this myself on El Cap, and it works without error... maybe some kind of intermittent bug. – Jeremy Mar 21 '16 at 16:58
-
I have the same problem (powerpc app no longer supported) on El Capitan... – Pierre Henry Nov 24 '16 at 13:35
-
-
depends what kind of script it is as well. I was trying to run a test server in the background, via `node_modules/.bin/cypress open &`, and it completed and existed right away, leaving me with no server. – JL Peyret Feb 17 '18 at 21:33
-
Works great for me on macOS Mojave if you do exactly as this answer plus some of the comments say. – jorisw Jun 14 '19 at 10:35
-
My shell script calls a python script and even with the full path and both scripts set to `+x` executable, this doesn't work. The python script outputs a file path and the shell script opens it. Works if I call it from the terminal but always opens the install HD when I click on the icon. No idea how to debug from here. dbr's solution with Automator does work though. Also with this solution the app does not show up in Launchpad. – hippietrail Mar 04 '21 at 03:59
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.

- 1
- 1

- 4,418
- 3
- 25
- 45
-
Nice, this worked for me, any way to chance the icon I wonder. FYI: ALso step 7 is referring to the file and not the menu item ... – Mike Q Apr 26 '17 at 21:29
-
-
@stiemannkj1, Does this show the icon while the app is running? Is there anyway to hide it? – Pacerier Dec 15 '17 at 21:10
-
@Pacerier, sorry, I'm not on OSX anymore (switched to Linux :) ). You might want to try [Jeremy Bank's answer](https://stackoverflow.com/questions/281372/executing-shell-scripts-from-the-os-x-dock/21048589#281389). I don't have any other ideas and since I don't have OSX, I cannot test or mess with anything to try and show/hide the icon.. – stiemannkj1 Dec 15 '17 at 21:24
-
2If you remove .sh suffix, it will run in a terminal in MacOS Catalina by default. Otherwise it will try to run it in XCode. – vwvan Nov 16 '20 at 02:52
-
If you put an icon on your script at step 2... initially the icon will be empty in the dock [step 4], but after running it the first time [step 7], the icon will appear in the dock [tested on Big Sur]. – MichaelR Jan 22 '22 at 00:27
-
1Supernice trick! works when you remove .sh. You can add program icon by opening cmd+i (info box) and drag a photo to it – Wim den Herder Feb 20 '22 at 06:43
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.

- 2,036
- 3
- 21
- 25
-
wonderful, thanks! Now all need want to make it a perfect solution would be a way to close the terminal window when the script completes. – Peter M Sep 01 '11 at 16:50
-
1well if you have close window on exit selected in the terminal preferences you can add the exit command to your script and you'll have what you want. – Jason Tholstrup Sep 06 '11 at 15:40
As long as your script is executable and doesn't have any extension you can drag it as-is to the right side (Document side) of the Dock and it will run in a terminal window when clicked instead of opening an editor.
If you want to have an extension (like foo.sh), you can go to the file info window in Finder and change the default application for that particular script from whatever it is (TextEdit, TextMate, whatever default is set on your computer for .sh files) to Terminal. It will then just execute instead of opening in a text editor. Again, you will have to drag it to the right side of the Dock.

- 77,985
- 20
- 184
- 180
-
1**NOTE**: This works like a champ in OS X 10.10 Yosemite. I cannot comment on 10.9 – Bryan Ash Sep 04 '15 at 14:57
-
1
-
Works on 10.12 too! Perhaps my comment about 10.9 was incorrect, but I noticed that you need to make sure you drag it to the part of the dock below the separator. I have no way to re-test 10.9 unfortunately. – stiemannkj1 Dec 05 '16 at 18:54
-
No need to add it to the Dock. It can be run from the desktop. But there can be no extension. Make it executable and you can just double-click. This is the simplest option I've seen. – Rich Apodaca Oct 18 '17 at 22:29
In the Script Editor:
do shell script "/full/path/to/your/script -with 'all desired args'
"
Save as an application bundle.
As long as all you want to do is get the effect of the script, this will work fine. You won't see STDOUT or STDERR.

- 3,266
- 21
- 33
-
2I would suggest: "Save as an application BUNDLE". The bundles launch much faster than applications. – e.James Nov 21 '08 at 21:06
I think this thread may be helpful: http://forums.macosxhints.com/archive/index.php/t-70973.html
To paraphrase, you can rename it with the .command extension or create an AppleScript to run the shell.

- 2,374
- 20
- 23
-
On Snow Leopard at least, adding a .command extension to a script makes it run in the terminal if you double-click it, but for some reason you still can't drag it into the dock. dbr's and Jeremy's suggestions both worked for me, though. – Jo Liss Dec 03 '10 at 13:25
As joe mentioned, creating the shell script and then creating an applescript script to call the shell script, will accomplish this, and is quite handy.
Shell Script
Create your shell script in your favorite text editor, for example:
mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"
(this runs the w32 executable, using the mono framework)
Save shell script, for my example "StartKeepass.sh"
Apple Script
Open AppleScript Editor, and call the shell script
do shell script "sh /Volumes/Media/~Users/me/Software/StartKeepass.sh" user name "<enter username here>" password "<Enter password here>" with administrator privileges
do shell script
- applescript command to call external shell commands"sh ...."
- this is your shell script (full path) created in step one (you can also run direct commands, I could omit the shell script and just run my mono command here)user name
- declares to applescript you want to run the command as a specific user"<enter username here>
- replace with your username (keeping quotes) ex "josh"password
- declares to applescript your password"<enter password here>"
- replace with your password (keeping quotes) ex "mypass"with administrative privileges
- declares you want to run as an admin
Create Your .APP
save your applescript as filename.scpt, in my case RunKeepass.scpt
save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case
Copy your app file to your apps folder

- 51
- 1
- 1
Exact steps to achieve that in macOS Monterey 12.3
- Open
Automator
File
->New
- Choose
Application
- Go to
Library
->Utilities
- Double-click
Run Shell Script
- Type in whatever command you want to run. For example, try the command to toggle Dark Mode:
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode'
File
->Save
- Drag the saved file to the Dock, done!

- 15,807
- 13
- 47
- 78
pip install mac-appify
I had trouble with the accepted solution but this command worked for me.
Install
pip install mac-appify
Run
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/appify ~/bin/webex_start.sh ~/Desktop/webex.app

- 25,183
- 12
- 93
- 106
Adding to Cahan's clear answer ... to open a shell script from the dock without passing any arguments to it, try:
open [name of your script].scpt"
example:
open "//Users/user/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/myScript.scpt"

- 19
- 1
Someone wrote...
I just set all files that end in ".sh" to open with Terminal. It works fine and you don't have to change the name of each shell script you want to run.

- 15,955
- 18
- 111
- 232