Language Guide
CHAPTER 9
Script Objects
266 About Script Objects
Here is a simple script object definition:
script John
property HowManyTimes : 0
to sayHello to someone
set HowManyTimes to HowManyTimes + 1
return "Hello " & someone
end sayHello
end script
It defines a script object that can handle the sayHello command. It assigns the
script object to the variable John. The definition includes a handler for the
sayHello command. It also includes a property, called HowManyTimes, that
indicates how many times the sayHello command has been called.
A handler within a script object definition follows the same syntax rules as a
subroutine definition. Unlike a subroutine definition, however, you can group a
handler within a script object definition with properties whose values are
related to the handler’s actions.
After you define a script object, you initialize it by running the script that
contains the script object definition. You can then use a Tell statement to
send commands to the script object. For example, the following statement
sends the sayHello command the script object defined above.
tell John to sayHello to "Herb"
The result is "Hello Herb".
You can manipulate the properties of script objects in the same way you
manipulate the properties of system and application objects. Use the Get
command to get the value of a property and the Set or Copy command to
change the value of a property.
The following statement uses a Get command to get the value of the
HowManyTimes property of script object John.
get HowManyTimes of John
if the result > 10
return "John, aren't you tired of saying hello?"
end if