You define an AppleScript script object using the basic syntax that Example 9-1 illustrates. 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. You could add other methods to the Collection object, such as a method that deletes a property/value pair from the "col" property (there's a programming challenge). You can also define this script object as a separate compiled script, without including the script Collection...end script syntax. Give the saved script the filename Collection. Example 9-3 does the same thing as Example 9-2, except that Example 9-3 gains access to the external script object (which has a filename of Collection) by using the load script scripting addition. You call the methods of AppleScript objects using the tell statement, assuming that the script object referred to by the objCol variable has a getName method: tell objCol to getName( ) See Chapter 7, for details on the tell statement. You can also use the possessive form to call an object's method, as in objCol's getName( ) or, use the of keyword getName( ) of objCol AppleScript doesn't use the "dot" syntax of other languages such as Java or JavaScript (i.e., you cannot use the syntax objCol.getName( )). You create several copies of the same script object by using the copy keyword, as opposed to the set keyword. For example, let's say you defined the Collection script object in the same script as the one in which you will use this object to do something (such as create instances of the object in memory). The code fragment in Example 9-4 creates separate instances of the Collection object in the variables cObj1 and cObj2. In the case of cObj2, if you used the code set cObj2 to Collection instead, then cObj2 and cObj1 would point or refer to the same Collection object. Using the copy syntax allows the scripter to create separate objects. You can create a child script object from a parent object by giving the child object a parent property. Example 9-5 creates a child object from the Collection object that I defined in Example 9-2. This new object inherits all of the properties and methods of the parent; you do not have to include those properties or methods in your definition of the child object. If you define a method with the same name as your parent's method, AppleScript overrides the parent method and calls the child's method. In this example, the child object defines a name property and getName method; only the child object will have this property and method, not its parent Collection object. The example also demonstrates the use of the continue statement inside of a method that you are overriding. If you use the continue statement followed by the name of the parent's method, then AppleScript calls the parent method. For Java programmers, if you were overriding a method called setCol, then the AppleScript continue statement is like calling the following inside of the overridden method: super.setCol( ) (* in Java, calling the base-class method inside a derived-class method that is overriding the parent method *) You use the continue statement when you want to add new functionality to a parent object's method and still have the parent method execute. In other words, you can also override a parent object's method by completely changing the method's behavior and never calling the parent method. You can intercept the calling of a scripting addition by using a script object and the continue statement. In other words, you can override some osax commands (you must test them first however), just as a child object might override a parent object's methods. The script object in Example 9-6 has a delay method that displays a dialog box asking the user if they really want to delay the script. If the user dismisses the dialog box with the Okay button, then the following code calls the delay scripting addition (whose integer argument determines how many seconds the script will pause): continue delay 10. |