DekGenius.com
Team LiB   Previous Section   Next Section

1.1 How Is AppleScript Used?

AppleScript can be used for both simple, self-contained solutions, such as a program whose sole purpose is to monitor how much space is left on a disk, and comprehensive systems that automate or control a suite of software programs. Let's begin with a simple script type, a standalone applet that is not attached to or designed to automate another software program.

You generally create an applet by typing AppleScript source code into an Apple Computer scripting program called Script Editor. You then compile the script (if it does not have any errors) into a small program called a compiled script or an applet that can be double-clicked on the desktop. An AppleScript applet is a self-contained program with its own desktop icon, while a compiled script requires a host program like Script Editor or Script Runner (see "Using Script Runner with OS X" later in this chapter) to run it. Figure 1-1 shows an applet icon. Chapter 2 also explains the various options for saving an AppleScript.

Figure 1-1. An applet icon
figs/ascr_0101.gif

AppleScript is a great tool for writing everyday utilities, such as managing files, folders, disks, and networking activities. The utility scripts provide all the functionality you need, without the necessity to automate another software program. These tasks, such as file backups or getting a browser to access certain web pages, would be time-consuming and tedious if they always had to be performed manually. Two examples of scripts that I run at least once every day are:

  • A script that displays a dialog listing the names of all of the running programs on the Mac, including invisible background processes. I can select one or more of these programs and click a button on the dialog window to close them.

  • An applet that calculates the remaining free space on all of the volumes that are mounted on the desktop, then displays the result for each volume and the total free storage space on all of the volumes put together.

A single hard disk can be divided into several volumes, which the Mac OS represents as disk icons on the user's desktop.

By now you would probably like to see just what applet source code looks like. The script in Example 1-1 displays the largest unused block of Random Access Memory (RAM) remaining on the computer where the script is run.

Example 1-1. AppleScript Displaying the Largest Block of Free Memory
tell application "Finder"
   activate
   set memblock to (largest free block / 1024 / 1024)
display dialog "The largest free block is now about " & (memblock) &¬ 
" megabytes."
end tell

This script asks the Finder application for a piece of data that the Finder maintains called "largest free block." This represents the size of the largest free memory block in bytes. The following script fragment:

(largest free block / 1024 / 1024)

divides this byte-size figure twice by 1024 to represent the result in megabytes, since most people convey the amount of computer memory they have using this measurement. display dialog is an often-used extension to the built-in AppleScript language called a scripting addition, which I explain later in this chapter (Appendix A, of the book is devoted to descriptions of the standard scripting additions that are installed with Mac OS 9 and OS X). display dialog shows a dialog window containing the message label that you specify in the source code following the display dialog command, as in this part of Example 1-1:

display dialog "The largest free block is now about " & (memblock) & ¬ 
" megabytes."

The tell statement that opens the script, such as:

tell application "Finder"

is AppleScript's method of targeting an application to request some data from it or to control the program in some manner. Since the script displays some Finder information to the computer user, the activate command is used to make the Finder the frontmost program (i.e., its windows, if any are open, become the active desktop windows). tell statements, commands, and other syntax elements are described elsewhere in this chapter, as well as in detail in Chapter 3 through Chapter 8.

1.1.1 Automation

Along with creating a number of useful utilities, AppleScript has won a reputation as a premier tool for automating software workflows. In workflows, one or more separate software programs cooperate in a sequence of actions to complete a job. This means that launching an AppleScript can orchestrate several actions that involve software applications that are not otherwise designed to share data or call each other's menu commands. AppleScript does the calling of each program's commands (targeting them in a similar manner to how the Finder is targeted in Example 1-1), acting as a conductor for busy software medleys. AppleScript has earned the undying loyalty of many Mac scripters in the print and web publishing industries by its ability to simultaneously control applications such as QuarkXPress, Adobe Illustrator, InDesign, and Photoshop, Canto Cumulus, FileMaker Pro, as well as the Microsoft Office members like Word and Excel.

As an example of automation, I designed an AppleScript in the summer of 2000 to convert thousands of text files to web pages. A company that publishes legal decisions wanted to make them available to a search engine on their web site. Since they were already plain text or Word files, and the page designs were very simple, we used an AppleScript to feed the pages to Word and to trigger its "Save as HTML..." menu command (which creates a simple, almost crude, web-page design at best). The company converted about 20,000 legal decisions in a matter of days, using this rather modest script that I developed in Script Editor.

Apple Computer has traditionally urged Mac software developers to make their programs "scriptable," and thus increase the market and following for those programs. It usually does. For example, Illustrator and Photoshop[1] are generally much more scriptable on the Mac platform than their Windows versions, which may influence some buyers to prefer the Mac versions (along with the fact that graphics professionals tend to prefer the Apple platform).

[1] Photoshop requires the licensing of the PhotoScripter plugin from Main Event Software (http://www.mainevent.com) to be extensively automated with AppleScript.

Similar to OLE automation on the Windows platform, many software programs allow themselves to be automated by exposing an object model to a scripting tool, in this case AppleScript. Conceptually, an object model is a tree-like diagram (see Figure 1-7 later in this chapter) showing the objects or "things" that the software represents as computer data (such as files, folders, or database records), as well as the objects' attributes or properties. AppleScript talks to these scriptable programs by exchanging Apple events with them. These are high-level operating-system events that are used for interapplication communication on the Mac. See Section 1.2 for more information.

With the release of Mac OS 9 and OS 9.1 in 2000 and early 2001, Apple Computer has made most of the computer's built-in software controllable by AppleScript. These are some of the scriptable OS 9 applications and control panels:

  • Appearance control panel

  • Apple Help Viewer

  • Apple System Profiler

  • Apple Verifier

  • Application Switcher

  • ColorSync extension

  • File Exchange

  • Memory control panel

  • Sherlock 2 (the Find application)

  • Speech Recognition

Some previously scriptable features have not been included in the Mac OS X installation, including preferences scripting, Folder Actions, printing scripting, and program linking. Future OS X releases will address these elements, according to Apple Computer, which adds that a number of Mac OS X applications are scriptable (with some qualifications):

  • Finder (some Finder commands, such as move or duplicate, are not yet implemented or are not yet functioning)

  • TextEdit

  • Mail

  • Sherlock

  • QuickTime Player (the Pro version of this application is quite scriptable; visit http://www.apple.com/applescript to download the QuickTime scripts collection for Mac OS X)

  • Apple System Profiler

  • Stuffit Expander

  • Internet Explorer

  • ColorSync Scripting

  • URL Access Scripting

  • Image Capture Extension (a background application that works with the Image Capture program; its dictionary supports the scaling and rotating of image files)

In addition, the AppleScript engineers are apparently working on ways to let AppleScript interact with the command-line shells that come with OS X, such as the Bourne shell. OS X already permits the launching of AppleScripts from a shell (see Chapter 34).

1.1.2 Attachability/Recordability

If an application is either attachable or recordable (or both), it is considered a near paragon of scriptability. Attachable means that you can create a script and then attach it to a program, so that the script is added to the program's internal menus. Applications usually implement attachability with Mac OS 9 by providing a folder for scripts and a menu item on their menubars that lists these available scripts. Figure 1-2 shows a menubar that contains a list of attached scripts for the BBEdit text editor.

Figure 1-2. Attached scripts in BBEdit
figs/ascr_0102.gif

Attached scripts will often run much faster than scripts that run as self-contained applets even if the script doesn't have anything to do with the application it is attached to (i.e., the script never sends Apple events to the host application). For example, I have an AppleScript that reads large web logs (more than 1 MB in size) looking for and recording for later display certain file paths. When attached to BBEdit 5.1, the script runs about six times as fast as it does when run as an applet outside of BBEdit (40 seconds as opposed to about 240 seconds). Try it with some of your own scripts.

A few applications allow themselves to be recorded by Script Editor, which is a great way to get started with scripting them. To do this, open Script Editor and click its Record button (see Chapter 2). You then activate an application and perform the actions that you are trying to record, or simply go in and manipulate its menus to see what happens. Once you click Stop in Script Editor, the Script Editor window will display the AppleScript source code representing the recorded actions. If the application is not recordable, the Script Editor window will be empty after you click Stop. Otherwise, you can then save the AppleScript as a macro that you can use and/or modify in the future. The Finder, BBEdit, and Microsoft Word are examples of recordable applications.

1.1.3 Scripting Additions

Scripting additions are extensions to the AppleScript language. The Standard Additions and some others are written by Apple; however, there are hundreds of scripting additions the scripters can add themselves. They are added by placing the scripting addition file into the Scripting Additions folder. Once installed, scripting additions can be used by any script.

Another term for scripting addtion is osax (the plural form is osaxen), which stands for Open Scripting Architecture eXtention. The OSA is explained in an upcoming section of this chapter.

In Mac OS 9, the scripting additon files are stored in the startup disk:System Folder:Scripting Additions folder. They are stored in more than one location in Mac OS X, including /System/Library/ScriptingAdditions/. Examples of two scripitng addition commands that are often used are display dialog (see Example 1-1) and current date. The latter command returns a date object that contains data about today's date and time. The Standard Additions are installed with the Mac OS.

There is a large database of scripting additions at http://osaxen.com.

    Team LiB   Previous Section   Next Section