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.
Share
Categories
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
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 Permalink rate up rate down ReplyI'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/
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 Permalink rate up rate down ReplyAnd 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 Permalink rate up rate down Replydoing 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
Hot Apps on TUAW
Deals of the Day
more deals- Refurb Apple MacBook Air Laptops: 12" 64GB SSD for $699 + free shipping
- JVC Motion Sensing Clock Radio with Dual iPod Docks for $55 + free shipping
- Apple iPhone Headset with Mic for $4 + $2 s&h
- miFrame Picture Frame Dock for iPad for $64 + $8 s&h
- Refurb Apple iPod nano 8GB MP3 Player for $99 + free shipping, 16GB for $119
- Hannspree Apple-Shaped 28" 1080p LCD HDTV for $270 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



5 Comments