|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
MacOS.AEClass objectThe MacOS.AEClass object represents a reference to an object within another application. While MacOS.AEClass has a series of standard properties, it also provides JavaScript properties and methods corresponding to the properties and AppleEvents defined for the object in the application's dictionary ('aete').
Getting PropertiesTo read the value of an application property, use the standard JavaScript dot (.) notation to name the property you want to read. For instance, the following example reads the name property from the MacOS Finder's application object.
NOTE: application dictionaries may define names containing spaces. JavaScript OSA translates all spaces in dictionary names to underscores (_). If you want to read the "default value" of an object, use the MacOS.AEClass.valueOf() method.
Specifying a Desired TypeThe Get AppleEvent allows you to optionally specify a desired type when reading a property. In AppleScript this is done using the as parameter of the get comment (e.g. get contents of document 1 as styled text). JavaScript OSA supports this by providing a xxxAs(requestedType) method for each property where xxx is the property name. When reading the default value of an object, you can specify a desired type as a parameter to the valueOf() function. The desired type may be expressed using one of the symbolic names in the _types object or as a 4-character string containing the 4-character AppleEvent data type code (e.g. ..._types.integer or "long").
Setting PropertiesTo modify the value an application property, use a standard JavaScript assignment statement:
To set an object's "default value", use the JavaScript assignment operator (=):
Accessing Elements (collections)The MacOS.AEClass also provides properties named after each element of a class which return MacOS.AEColl objects. These properties allow you to navigate through an application's containment network and locate other objects. Element names can be either the singular (e.g. the Finder's "file" element) or plural (e.g. the Finder's "files" element) in cases where the application's dictionary defines a plural form of the class. For instance, in the MacOS Finder, the collection of files on the desktop is represented by the file property. The following sample counts the number of files on the desktop. For further details, please refer to MacOS.AEColl.
Sending AppleEventsThe MacOS.AEClass provides a function named after each AppleEvent defined for the object represented by the MacOS.AEClass instance in the target application's dictionary. When any of these functions are called, the corresponding AppleEvent is sent to the target object within the target application. Two methods of sending AppleEvents are provided. The first uses the standard positional JavaScript function call and the second allows you to specify the AppleEvent parameters by name. Positional ParametersWhen the positional form is used (which is the default), parameters appearing in the JavaScript function call are added to the AppleEvent in the order that they appear in the Application's dictionary definition for the AppleEvent. If you want to omit a parameter from the AppleEvent, specify Null as its value in the function's parameter list. For example, the Finder's Move command is defined as follows:
The following example shows the MacOS Finder's Move AppleEvent being invoked with positional parameters in JavaScript.
Parameter 1 (folder["untitled folder"]) is sent as the "to" parameter and parameter 2 (false) is sent as the "replacing") parameter. The direct object (sometimes called the subject, target or reference) parameter is supplied by default by the MacOS.AEClass object. If you want to override this parameter, you must use Named Parameters and specify the parameter as "ae_----". The MacOS.AEApp class, which is a special form of the MacOS.AEClass class representing the application's application object requires that the direct object parameter, if it is included in the AppleEvents definition in the application's dictionary, be included as the first parameter in AppleEvent function calls. This is because AppleEvents invoked on the application object typically do not include a direct object parameter, or if they do, the direct object parameter may be something other than an object reference. Named ParametersTo send an AppleEvent using named parameters, prefix the AppleEvent name with an underscore (_). When the named form is used, parameters are placed into a JavaScript object as properties. Property names must correspond to the parameter names appearing in the AppleEvent's definition in the application dictionary. The order of properties within the object definition is not important.
The MacOS.AEApp class, which is a special form of the MacOS.AEClass class representing the application's application object requires that the direct object parameter, if it is included in the AppleEvents definition in the application's dictionary, be included as the first parameter in AppleEvent function calls. This is because AppleEvents invoked on the application object typically do not include a direct object parameter, or if they do, the direct object parameter may be something other than an object reference. Using _constantsSome properties and AppleEvent parameters accept enumerations as their values. JavaScript OSA provides the _constants property as a way of accessing application defined constants. Each constant in the application's dictionary is defined as a property of the _constants property.
The type of value returned for each property of the _constants object is an instance of MacOS.AEDesc containing the constant value. Using _typesSome properties and AppleEvent parameters accept data types as their values. JavaScript OSA provides the _types property as a way of accessing application data types. Each data type and class in the application's dictionary is defined as a property of the _types property.
The type of value returned for each property of the _types object is MacOS.AEDesc. Responding To ErrorsIf an AppleEvent results in an error, a JavaScript exception is thrown containing a MacOS.MacOSError object. This object contains the details of the error. You can catch errors using a JavaScript try - catch construct. The MacOS.MacOSError page contains an example illustrating how to catch exceptions. Issues & WorkaroundsJavaScript, unlike AppleScript, was not designed with AppleEvents in mind. Because of this, there are a few places where the syntax of the JavaScript language and common AppleEvent terms collide. The AppleEvent class property conflicts with the JavaScript class reserved word. Therefore, it is not possible to send an AppleEvent requesting the class of an object using normal JavaScript syntax.
JavaScript will flag the word class with an error. The workaround is to use JavaScriptÕs array syntax to specify the property name.
Similarly, the standard Delete AppleEvent collides with JavaScript's delete reserved word. As with the Class example above, use the array syntax to define the AppleEvent you want to send
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||