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.

An applet icon
Figure 1-1. An applet icon

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.

Tip

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.

Get AppleScript in a Nutshell 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.