Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Run shell scripts from GUI Apps
[Editor: Please see the comments; this is probably NOT the best way to do this!! Kill is fairly abrupt, and will end ALL terminal sessions. The comments have some alternative.]

This is one I've been trying to figure out for quite some time and finally managed to figure out so I thought that I'd share it with you.

I wanted to run a shell script from the GUI and have the Terminal close itself down at the end of the script. The only way I knew to do this was to use a modified version of the Dock Restart script from elsewhere on this site. i.e.

TERM=`ps aucx| grep Terminal | cut -d ' ' -f 4-5`
kill $TERM

but this would result in a "Application unexpectedly quit" error message. If you change the line "kill $TERM" to "kill -9 $TERM" the error message no longer appears. Simply add those lines at the end of your script to make the Terminal vanish when the script is complete.

So now to make a script executable from the GUI.
1) Type the script in your favourite plaintext editor, include the above script at the end.
2) from the terminal run "chmod +x Text.file" (Where Text.file is your files name)
3) In the Finder do a 'get info' on your text file and change the Application to Terminal.
4) Double click the file and voila a script that runs itself and then quits the terminal with no error.

I hope this helps a few people.
    •    
  • Currently 3.00 / 5
  You rated: 5 / 5 (4 votes cast)
 
[7,108 views]  

Run shell scripts from GUI | 8 comments | Create New Account
Click here to return to the 'Run shell scripts from GUI' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
kill -9 wil close ALL terminals
Authored by: a1291762 on Jul 02, '01 06:33:39PM

Fortunately or Unfortunately (depending on how you see it) Mac OS X will only let applications run once. When you kill terminal, you will close ALL open windows. While this may be fine for people that only use one window at a time, it's not an optimum situation.

You can set a preference so that terminal windows automatically close (or close if no error).

If you set the terminal preferences to close when no error and set the last line of your script to "exit 0" then the window should automatically close anyway and you won't force other open terminal windows to close. If you did "exit 1" then the window would stay open (which is great when things don't work as you can see what was on the screen)

eg.

#!/bin/sh

read text
if [ "$text" = "" ]; then
# print an error if not text was entered (eg. Ctrl + D)
exit 1
fi

echo $text

exit 0



[ Reply to This | # ]
A better way to do this...
Authored by: sjonke on Jul 02, '01 06:58:21PM

... is to, in Terminal, make a new window and then Save it to disk. Now drag that file (will have the extension .term) onto BBEdit. Change the lines:

<key>Shell</key>
<string>/bin/tcsh</string>

to whatever command you want to do. For example:

<key>Shell</key>
<string>top</string>

Lastly, make sure the shell exit action is 0:

<key>ShellExitAction</key>
<string>0</string>

Save the file. Now you can double click it to open a top window (put in the Dock or wherever.) If you press Control-C in the window TOP will stop and the window will go away automatically. Or you put whatever you command you want in there. Other ideas: put in commands like telnet some.place.org to make a double clickable file that takes you to a particular site. Etc. etc. Also, you can set the terminal windows name - just look at all the option availble in the file and set to what you want. You can also provide commands to run after the shell command is run. So if you want to do multiple commands, keep the shell command as is, but add commands to the "ExecutionString". Put a return after each command or you will have to type the return manually. Enjoy.

Steve



[ Reply to This | # ]
A better way to do this...
Authored by: owain_vaughan on Jul 03, '01 05:08:49AM

If you don't like editing xml files y hand, then use the PropertyListEditor from /Developer/Applications (if you've installed the Developer Tools CD)



[ Reply to This | # ]
Setting more options easily, plus drop scripts
Authored by: sjonke on Jul 03, '01 11:17:28AM

Another way to edit various settings of the .term file is to run it and then move the window and/or resize it, change title, set colors, etc, and then save the file. Whatever you did will be saved and the next time you run it all those settings will be stored. Also you can manually set the TerminalOpaqueness property if you want a specific window to be more or less transparent then your normal terminal windows. To do that you have to edit the .term file with BBEdit or the PropertyListEditor.

Lastly, if you want to use AppleScript to perform shell commands, download the very cool scripting addition called OS X Power Additions Lite. It provides a Shell command and returns the results of whatever was run. See:

http://www.versiontracker.com/moreinfo.fcgi?id=10549

It creates some really cool possibilities for integration of apple-scriptable OS X and Classic applications with shell commands. I just wish more OS X software was scriptable with AppleScript. I can't find the DropScript tool mentioned in another messsage either, however, you can create these kind of drop scripts with AppleScript via this scripting addition, and then you have both the power of unix and the power of AppleScript and whatever you want to integrate it with. Unfortunately, currently the addition does not allow for running commands with password-required sudo. Hopefully that will be added.



[ Reply to This | # ]
Setting more options easily, plus drop scripts
Authored by: sjonke on Jul 03, '01 11:18:28AM

Another way to edit various settings of the .term file is to run it and then move the window and/or resize it, change title, set colors, etc, and then save the file. Whatever you did will be saved and the next time you run it all those settings will be stored. Also you can manually set the TerminalOpaqueness property if you want a specific window to be more or less transparent then your normal terminal windows. To do that you have to edit the .term file with BBEdit or the PropertyListEditor.

Lastly, if you want to use AppleScript to perform shell commands, download the very cool scripting addition called OS X Power Additions Lite. It provides a Shell command and returns the results of whatever was run. See:

http://www.versiontracker.com/moreinfo.fcgi?id=10549

It creates some really cool possibilities for integration of apple-scriptable OS X and Classic applications with shell commands. I just wish more OS X software was scriptable with AppleScript. I can't find the DropScript tool mentioned in another messsage either, however, you can create these kind of drop scripts with AppleScript via this scripting addition, and then you have both the power of unix and the power of AppleScript and whatever you want to integrate it with. Unfortunately, currently the addition does not allow for running commands with password-required sudo. Hopefully that will be added.



[ Reply to This | # ]
Another option
Authored by: _merlin on Jul 02, '01 07:51:21PM

Apple has release an application called DropScript as a code example. You can get it by going to http://www.apple.com/developer/ and following the links to Mac OS X code examples. It can run various kinds of scripts, and exit after they run. It actually lets you create "droplets" from scripts as well.





I know it's already been said, but killing the terminal application is a pretty silly idea. All your open terminals will die, and you can corrupt its preference file.





Vasantha Crabb



[ Reply to This | # ]
Another option
Authored by: robh on Jul 03, '01 08:35:47AM

Can you please provide a full URL or better directions from /developer/.

I can't find the DropScript app, and the search engine reports no matches.

thanks.



[ Reply to This | # ]
Dropscript URL
Authored by: metafeather on Jul 04, '01 02:05:13PM

can be got here:

http://www.apple.com/downloads/macosx/development_tools/dropscript.html



[ Reply to This | # ]