Language Guide

Using Subroutines 221
CHAPTER 8
Handlers 8
Figure 8-0
Listing 8-0
Table 8-0
Handlers are collections of statements that are executed in response to
commands or error messages.
This chapter describes
subroutines, which are handlers for user-defined commands
command handlers for system or application commands
command handlers for script applications
the scope of script variables and properties declared in handlers and
elsewhere in scripts
Using Subroutines 8
Subroutines are collections of statements that AppleScript runs in response
to user-defined commands. They are similar to functions, methods, and
procedures in other programming languages. This section explains how to
write and call subroutines.
Subroutines are useful in scripts that perform the same action in more than one
place. For example, if you have a series of statements for comparing values and
you need to use those statements at several places in a script, you can package
the statements as a subroutine and call it from anywhere in the script. Your
script becomes shorter and easier to maintain. In addition, you can give
subroutines descriptive names that make their purposes clear and make scripts
easy to read.