AppleScript is wonderful in that it makes many otherwise intractable automation problems no worse than simply slow and irritating, but it is nonetheless still pretty crude tool for certain tasks. Some of those tasks are simple to do with a simple shell command, but other times you'd like the shell script to do something complex before returning to the happy-go-lucky land of AppleScript.
Indeed, it's no problem to call a shell script from AppleScript. For example, use the ipconfig utility to get the IP address of your primary network interface:set myIP to do shell script ("ipconfig getifaddr en0")
Doing something more complex is more challenging because your script has to know how to execute the shell tool. That is, the tool needs to reside in your normal search path, or you need to provide a path to the utility. If it's a custom shell script you need to run, and you need to distribute your AppleScript to other people, or if you just want something tidy, it may be a frustrating problem. Fortunately, it's not hard to solve. Since AppleScript lets you save your scripts as Application Bundles, you can put your utility right inside the bundle. Then to call the utility, you can reference the path to the AppleScript's Application Bundle.
For example, if I have a shell script client
and an AppleScript named Connector
that needs to execute client
to do whatever it does, I can do the following:
- Save the Connector AppleScript as an Application Bundle.
- Open the bundle and place the shell script client inside it.
- Then I'll modify the Connector script as follows, to get the path to client and run it via the familiar do shell script(...).:
tell application "Finder"
set myFolder to path to me as alias
set clientPath to quoted form of (POSIX path of myFolder & "client")
set theResults to do shell script (clientPath)
Moral: In this manner you can bundle external scripts (shell, python, perl, whatever...) in to your AppleScripts so that you can use AppleScript for an interface, without having to jump around ripping out clumps of scalp trying to make AppleScript do anything non-trivial.