User-Defined Commands
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 theminimumValue
command is a subroutine that returns the smaller of two values. The target of theminimumValue
command in the following example is the current script:
set theCount to minimumValue(12,105)Inside a Tell statement, use the wordsof me
ormy
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 theminimumValue
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 tellWithout the wordmy
before theminimumValue
command, AppleScript
sends theminimumValue
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."
![]()