Chapter 23. Unix

AppleScript is a powerful way to get information and to make things happen through scripting on Mac OS X—but it’s not the only way. Mac OS X is Unix, and comes with many Unix command-line tools, along with scripting languages such as Perl and Ruby. AppleScript and Unix are different worlds, but there’s a communicating door between them, and it’s open in both directions: you can call into the Unix shell from within AppleScript code, and you can call AppleScript from the Unix command line. That means you can combine the power of Unix with the power of AppleScript. This chapter talks about how to do it; see also Section 2.7.

Do Shell Script

The key to calling the Unix shell from your AppleScript code is the do shell script scripting addition command.

Apple provides an excellent Tech Note on this command, and your first step should be to read it (http://developer.apple.com/technotes/tn2002/tn2065.html). The direct object is a string representing the text you would type at the command-line prompt in the Terminal. But not quite, because your Terminal shell is probably bash or tcsh, whereas the default shell for do shell script is sh. Also, the default paths used by do shell script might not be the same as your own shell’s default paths, so in order 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, since perl will probably work just fine.)

Optional parameters let you provide ...

Get AppleScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.