Advertisement

AppleScript: Integrating shell scripts

We've talked about AppleScript how-tos before. AppleScript is fun and all, but what if you already know how to write shell scripts? Well, did you know that by integrating shells scripts into AppleScript, you can create simple applications that do useful things? It's true, and I am going to show you how.


Continue reading to learn how to integrate shell scripts into AppleScripts.



What's a shell script?
A shell script is a way to automate a task by using a command line (i.e. using Terminal to access the underlying UNIX system in Max OS X). A simple "program" can be written that uses command line access to do a specific task.

How do I "Integrate shell scripts"?
Integrating your shell script into AppleScript is extremely easy. To do this, just use the "do shell script" command for AppleScript. So, your shell script will look similar to this:

do shell script "your script here"


What can I do with this shell script?
If you've never written a shell script in your life, don't worry. I have included some sample shell scripts below that can be used to forcequit applications and eject a disc from your Mac. Just copy these AppleScript into ScriptEditor.app (/Applications/AppleScript/ScriptEditor.app) and click the "Run" button to see the magic happen.

Kill an application

do shell script "killall 'application-name-here'"

Eject a disc from the Mac's drive

do shell script "drutil eject"


Saving shell scripts as Mac applications
You can save your completed AppleScript/shell script combos as applications (.apps) if you wish. To do this, simply type in your script and click File > Save as in the Script Editor's File menu. You can read this post for more information about saving AppleScripts as applications.


Important Update: Since last week, everyone seems to be a bit on the edge about running scripts (especially shell scripts). TUAW would like to remind everyone that you should only run AppleScripts/shell scripts from trusted sources.