Filed under: Features, How-tos, AppleScript
AppleScript: Integrating shell scripts

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

![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
Just Cause said 10:16AM on 6-23-2008
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
Reply
Scott Jann said 10:52AM on 6-23-2008
And if you write shell scripts, osascript lets you do the opposite sort of thing, executing AppleScript from the command line.
Reply
FredrikL said 11:39AM on 6-23-2008
Nice post. This reminded me of why I began liking TUAW in the first place. Keep it up!
Reply
David said 12:03PM on 6-23-2008
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/
Reply
Andrew said 2:09AM on 6-24-2008
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.
Reply