[HOWTO] open (Terminal) shell scripts by double-click

ropers

Registered
Here's a neat trick:

If you use shell scripts, such as:

#!/bin/sh
<your shell script here>


you can actually make them double-clickable from Finder, so they *almost* work like fully-fledged Applications.

You will need to be an administrative user to do these things:

- Create your shell script, with an editor that won't include extra characters (eg. pico in Terminal).
- In Terminal, make that script executable, eg. w/
chmod a+x <yourscriptname>
This will all be known to Terminal-savvy users so far.
But now:
- In Finder, locate the script and select Get Info (command-I) on it.
- Select Open With: Other...
- Change from Recommended Applications to All Applications.
- Browse to /Applications/Utilities and select Terminal.
- DO NOT select the Always Open With checkbox (unless you expressly want to associate a certain file extension with Terminal -- really this would be a Very Bad Thing).
- After clicking Ok and closing Get Info, you should be able to simply double-click on the file in question from Finder and what will happen is, Terminal will open up and execute that very script.
If your script is non-interactive, ie. if it will just do something and then exit, the Terminal session will be finished as well (ie. you can't go on and type in further commands at the prompt). By default, the shell window will however stay open, allowing you to review any output from your script. Also, the actual Terminal application will NEVER quit automatically (though this is not much of a problem).
There is one potential cosmetic problem:
If there is any output from your script, that will possibly already start to get written to the window while the initial shell prompt is still being written. This again is only a cosmetic problem in that it can jumble up what effectively gets displayed in the terminal screen. A workaround is to include
sleep 1
clear

at the beginning of the script (before any output).
This just makes the thing literally wait a second (so that everything that might still get written to the shell has time to get written) and THEN clears the screen.

Enjoy,
rop

PS:
- You can of course also give those scripts custom icons (the usual Get Info copy/paste - way.
- To really quit Terminal as well, you would effectively need to do some ps -ax and grep/kill stuff, but that may not necessarily be safe because you never know if the user really needs Terminal open. There may be a joint shell script - AppleScript solution, but I don't know it.
 
Also, adding the extension .command after making it executable w/ the terminal to a script will make it open with the Terminal.
 
Back
Top