Skip to Content

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.


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,...
 

Add a Comment

*0 / 3000 Character Maximum

5 Comments

Filter by:
Andrew

Also, if you want to do something with the output of your shell function, you can say something like set t to do shell script "perl -e 'use Time::HiRes qw(time); print time'". This would set the variable t to the current time in milliseconds.

June 24 2008 at 2:09 AM Report abuse rate up rate down Reply
David

I've been using this method (having saved as an app) to launch iCab and Sunrise browsers on webkit nightlies - gives them a nice performance boost and on iCab comes complete with the nightly Inspector and that 100/100 Acid3 feeling ;)
http://spotthehall.com/running-on-webkit/

June 23 2008 at 11:43 AM Report abuse rate up rate down Reply
FredrikL

Nice post. This reminded me of why I began liking TUAW in the first place. Keep it up!

June 23 2008 at 11:38 AM Report abuse rate up rate down Reply
Scott Jann

And if you write shell scripts, osascript lets you do the opposite sort of thing, executing AppleScript from the command line.

June 23 2008 at 10:42 AM Report abuse rate up rate down Reply
Just Cause

doing it interactively is more fun.....


tell application "Terminal"
do script with command "login oracle"
set name_of_window to (get name of front window)
set id_of_window to (get id of front window)
set active_tab_of_window to (get selected tab of window id id_of_window) as record
set active_tab_of_window_record to active_tab_of_window as list
set active_tab_of_window_record_id to (get item 3 of active_tab_of_window_record) as integer
set custom title of tab active_tab_of_window_record_id of window id id_of_window to "Oracle Interactive"
end tell
repeat 10 times
tell application "Terminal"
set text_content_of_terminal to (get history of tab active_tab_of_window_record_id of window id id_of_window)
end tell
tell application "BBEdit"
make new text document
set properties of text document 1 to {encoding:"Unicodeâ„¢ (UTF-8)"}
set bbedit_document to (get ID of text document 1)
replace "A[^Æ’]*Z" using text_content_of_terminal searching in text 1 of document id bbedit_document options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
find "Password:" searching in text 1 of document id bbedit_document options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
set end_of_query_result to (get found of result) as string
if end_of_query_result = "false" then
replace "A[^Æ’]*Z" using "" searching in text 1 of document id bbedit_document options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
else if end_of_query_result = "true" then
exit repeat
end if
end tell
end repeat

June 23 2008 at 10:16 AM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.