Team LiB   Previous Section   Next Section

Chapter 34. Executing Scripts with the Terminal App

In a dramatic departure from prior operating systems, but a welcome one for many Macintosh scripters, Mac OS X comes equipped with a command-line interface (CLI) to its underlying filesystem and applications. In a throwback to old-fashioned ways of interacting with a computer, a CLI involves entering text commands from the keyboard into a window that contains only a prompt in the form of a solid square cursor, percent sign, or some other symbol. You can access the command line from the Mac OS X graphical user interface (GUI) by executing the Terminal app from the directory startup disk:Applications:Utilities (or, in Unix parlance, /applications/utilities/ ). This program displays the CLI window. Some users, viewing Terminal as primitive and an unforgivable violation of Apple Computer's rich tradition of visual interfaces, will steer clear of the CLI. (Okay, so the Terminal isn't that primitive; you can control the size of its window and the font of its displayed text, among other attributes.) However, Terminal gives you access to system directories and files you cannot see in Finder windows, which is critical for system administrators. In addition, the Unix-derived software you can use from the command line, including the text editors pico, vi, and emacs, as well as the scripting languages Perl, tcl, and awk and all of the built-in BSD Commands (e.g., ls, pwd, mv, rm), are often indispensable accompaniments to AppleScript and other familiar Mac tools.

You can even create, compile, and execute AppleScripts from Terminal (otherwise, I might not have included this chapter!). Some scripters might be fond of popping open pico or vi and creating their AppleScripts in this manner, but I still prefer Script Editor. The true power of the marriage of Terminal and AppleScript, however, will perhaps come from the integration of AppleScript with the CLI tools, which Apple Computer has suggested will be included with future OS X releases. For example, I would like AppleScript to be able to get and deal with the return values from the execution of Perl scripts, so that AppleScript's eventual tight integration with OS X and ease-of-use could be combined with Perl's tremendous versatility (e.g., it's much easier to do network/HTTP programming and XML parsing with Perl than with AppleScript). You can already take standard input from a shell script and run this input as compiled AppleScript code, as this chapter's section on the osascript command discusses.

The remainder of this chapter describes how to use the three Terminal commands that can be used with AppleScript: osacompile, osalang, and osascript. Figure 34-1 shows what the Terminal window looks like after the osacompile command was used to compile a text file called s; then on the next line of the Terminal window, the ls command was used to show the contents of the user's current working directory. This directory contains the result of compiling the file s, which is a script called a.scpt, the default name that osacompile gives a script if its name option is not used. You can use the osacompile command to compile one or more text or compiled-script files into a single script.

Figure 34-1. Using the osacompile command to compile a text file into a script
figs/ascr_3401.gif
    Team LiB   Previous Section   Next Section