Language Guide
CHAPTER 4
Commands
76 Types of Commands
User-Defined Commands 4
User-defined commands are commands that trigger the execution of collections
of statements, called subroutines, elsewhere in the same script. The target of a
user-defined command is the current script, that is, the script from which the
command is executed.
There are two ways to specify the current script as the target of a user-defined
command. Outside of a Tell statement, simply use the command to specify the
current script as its target. For example, suppose that minimumValue is a
command defined by the user in the current script. The handler for the
minimumValue command is a subroutine that returns the smaller of two
values. The target of the minimumValue command in the following example is
the current script:
set theCount to minimumValue(12,105)
Inside a Tell statement, use the words of me or my to indicate that the target
of the command is the current script and not the default target of the Tell
statement. For example, the following sample script shows how to call the
minimumValue subroutine from within a Tell statement:
tell application "Scriptable Text Editor"
set theCount to my minimumValue(12,105)
get word theCount of front document
end tell
Without the word my before the minimumValue command, AppleScript
sends the minimumValue command to the Scriptable Text Editor, resulting
in an error.
Chapter 8, “Handlers,” describes the syntax for defining and invoking
subroutines such as minimumValue in more detail.
Note
You can also define subroutines in script objects. The target
of a user-defined command whose subroutine is defined in
a script object is the script object. For information about
defining and invoking subroutines in script objects, see
Chapter 9, “Script Objects.” ◆