AppleScript_ The Definitive Guide - Matt Neuburg [218]
Do Shell Script
Your first step in getting acquainted with the do shell script scripting addition command should be to read Apple's excellent technical note documenting it (http://developer.apple.com/technotes/tn2002/tn2065.html ).
The direct object of do shell script is a string representing the text you would type at the command-line prompt in the Terminal. Actually, that's not quite true, so do not imagine that you can blithely test a prospective do shell script command simply by typing it in the Terminal, as there might be some differences. Your Terminal shell is probably bash or tcsh , whereas the shell for do shell script is sh (you probably won't experience this as a difference in Tiger, though, where sh is bash). Also, the default paths used by do shell script might not be the same as your own shell's paths, so to specify a command, you might have to provide a full pathname, such as /usr/bin/perl instead of just perl. (That's not a real example, though, as perl will probably work just fine.)
The result of do shell script is whatever is returned from the command via standard output (stdout). Unix newline characters are converted to Mac return characters by default, but you can prevent this if you wish. If the command terminates with a nonzero result (an error), an error by the same number is thrown in your script, and you can use this number (along with the man pages for the command) to learn what went wrong.
For example, the following code requests a (decimal) number from the user and converts it to hex by means of the Unix printf command:
set theNum to text returned of (display dialog "Enter a number:" default answer "")
set s to "printf %X " & theNum
display dialog (do shell script s)
The do shell script command both accepts and returns Unicode text. But the actual medium of communication between AppleScript and the shell is UTF-8—that is, the direct object of do shell script is converted to UTF-8 before passing to the shell, and the reply from the shell is assumed to be UTF-8 and is converted to Unicode text (UTF-16) before arriving in your script. This is a sensible approach. Most Unix tools are probably unprepared to deal with Unicode input, and for characters in the ASCII range, the UTF-8 representation is the ASCII representation, so in effect the direct object is coerced automatically from Unicode text to a string, as long as it consists of just ASCII characters—which it usually will, so you usually won't have to worry about it. On the other hand, some Unix tools produce UTF-8 output, and it's nice to know that you can capture this seamlessly as the result; indeed, this can be a useful technique, as you saw in "Unicode Text" in Chapter 13.
The optional parameter with administrator privileges lets you run a command as root. Such a command will require administrator authentication; you can provide it as part of the command (with the user name and password parameters) or permit the authentication dialog to appear. The implementation of this option has been tweaked several times over the course of its history; this is unfortunate, as it means that the precise version of AppleScript your script runs on can make a crucial difference to the script's behavior. For example, before Tiger, multiple commands (separated by semicolons) in a do shell script command with the with administrator privileges parameter broke; in early versions of Tiger, scripts that called sudo or perl from do shell script along with the with administrator privileges parameter broke. Be careful, and test thoroughly.
The shell set up by do shell script is not interactive, so beware of tools that expect an interactive shell. Many such tools provide