Name

Script Objects

Synopsis

You define an AppleScript script object using the basic syntax that Example 9-1 illustrates.

Example 9-1. Syntax for Creating a Script Object
script MyScript
 (* define properties, methods, variables here *)
end script

Use the syntax script...end script to define a script object within another script. For example, a single script could involve its own properties, subroutines, run handler, and one or more script objects. Used in this manner, a script object can be like a “type” that you define. Example 9-2 defines a Collection type within another script. A Collection involves one to several property/value pairs and methods that can return Collection values as well as add a new property/value pair to the Collection. In this case, the Collection object or type is defined at the top of the script and then demonstrated beneath the script-object definition. The example Collection involves the names of some planets followed by their diameters in kilometers.

Example 9-2. A Collection Script Object
script Collection property col : {} (* an empty list type until it is initialized as a record *) (* return the collection *) on getCol( ) return col end getCol (* set the collection value(s) *) on setCol(rec) if (class of rec is record) then set col to rec -- collection object's col property is set to rec else return end if end setCol (* add a member to the collection *) on add(mem) if (class of mem is record) then set col to col & mem else return end if end add (* get ...

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.