ADC Home > Reference Library > Technical Notes > Legacy Documents > Java >
Important: This document is part of the Legacy section of the ADC Reference Library. This information should not be used for new development.
Current information on this Reference Library topic can be found here:
|
How MRJ Scripting WorksMRJ Scripting works by exporting public Java class, property, and method names as OSA scripting terminology so that they are visible to scripters. When a script runs, MRJ scripting translates the Apple Event data types and events into Java data types and method calls. A scripter can thus set and get data in property fields of Java objects, and invoke Java methods on those objects.
The OSA scripting terminology is generated dynamically by the Applet Runner or Java application. When you compile a script that targets
the Apple Applet Runner, the Script Editor (or any other OSA development
environment) fetches its dynamic terminology. That terminology includes AWT Component objects for each open applet. For example, the sample applet "Lightweight Gauge" has a formal applet name of
Because the
Methods and properties of Java objects are exposed to the scripter in this manner. Occasionally some Java object, method, or property
names will conflict with AppleScript reserved word namespace, or be illegal in AppleScript (for example, an item that contains periods like user.name). When this happens, the Java names will be published enclosed in AppleScript's literal quotation marks, the vertical bars ( | ). For example, a Java Applet parameter named "columns" can be written as In an applet that uses AWT, the entire AWT component hierarchy is exported for scripting, so every function of the applet that can be driven by the user interface can also be driven by scripting. To script an applet, you do not have to know about its internal methods; you can script its buttons, menus, and display objects directly. This is different from normal Macintosh application scripting, where you normally script the semantic objects of the application. The principal advantage to the MRJ's scripting method is that all aspects of the applet's operation are scriptable, without any special assistance from the applet. The principal disadvantage is that internal or semantic information stored in the applet can only be accessed by pulling the "puppet strings" of the user interface by scripting the AWT components. Thus, developers should include semantic objects as properties of the top-level Component in their Java applets or applications. Then you can script Java applets or applications in the preferred way of Macintosh scripting.
Scripting the Applet Runner or Java ApplicationThe Applet Runner is a scriptable application that supports the Required and Standard suites of events, as well as a small number of custom events. Java applications should also have a dictionary that supports these events. The supported events are:
See the Applet Runner dictionary for the full parameters and results of these events. Applet Runner Application PropertiesLike most scriptable applications, the Applet Runner application or Java applications support global properties that control its operation. All system properties of the Java engine are exposed as application properties of the Applet Runner or Java application. Remember that because most system properties are illegal as AppleScript identifiers, they are enclosed in vertical bars and are case sensitive:
If a method returns a Java object, it is normally returned as the opaque class of the Java object. With MRJ Scripting provides automatic contextual menu support for Java Java Objects as AppleScript ObjectsWhen an applet is opened in the Applet Runner, it is available as an implicit top-level element of the application (much like desktop files are implicit top-level objects in the Finder). To experiment with this, open the "Lightweight Gauge" applet in the Applet Runner and execute the following script:
The Java objects that make up this applet are contained by the
(Note that in the applet runner, the window is a container of the Applet and is not the Applet object itself. The Applet Runner's Window class is useful only for manipulating the windows the Applet Runner displays. You can also address the applet directly.)
Discovering the correct name of a component through trial and error can be difficult. MRJ Scripting assists you in determining object
specifiers (when the Following AppleScript conventions, the contents of containers are referred to using 1-based indices, even though the standard for Java is zero-based indices. So the first component in a container is component 1, not component 0.
Most properties of the Java object are available as
properties of the AppleScript object, and the AppleScript It can be set accordingly with the AppleScript Java Methods as AppleScript CommandsMost method calls on a Java object are available as AppleScript commands. In the dictionary displayed by the Script Editor, each Java class is listed in its own suite with an AppleScript class representing the Java class and a list of commands in that suite that correspond to the methods.
The target or direct object of a command must be the Java object that supports the corresponding method. You can use the syntax of any of the following AppleScript examples to send a command to a target object:
Because AppleScript supports named parameters but not ordered
parameters, and Java supports ordered parameters but typically not named
parameter information via Java reflection, the parameters are usually passed in the parameter-named parameters. This is an ordered list containing the values that would appear between the parenthesis in the Java method invocation. Remember, the parameters must be supplied in the order they are expected by the Java method; for your convenience, this is listed in the comment line of the command's dictionary entry. For example, an invocation of the (Because parameter lists are passed directly to Java methods without interpretation, index parameters are zero-based, not 1-based). Java classes that implement the BeanInfo interface provides additional information for scripting. MRJ scripting can then look for Java Parameter Descripter objects to provide named parameters in the dictionary, and you can use these named parameters directly in AppleScript. As more JavaBeans are created, this will become more common, but currently very little Java code provides this information.
Most methods take scalar parameters (discrete types such as Scripting Java AppletsFor applets, the applet tag is provided to AppleScript as a property of the Applet object, even though it is not really a field of the
Java Applet class. Furthermore, you can get and set the Applet tag to change runtime parameters. Setting the Applet tag will result in a relaunch of the Applet with the new Applet tag. Here is an example
of changing the
Creating a Scriptable Java ApplicationTo script a Java application, the Java application file needs to have Automatically generated terminologies may not be very user friendly. Their comments are simply the Java object's name, and they expose most properties and methods to the scripter. You may want to use the automatically generated terminology as a starting point, then change the comments and delete the properties and methods that are not useful to the scripter.
Since the Generating an 'aete' for a Java applicationScriptable applications must provide a terminology resource (stored in resource The automatically generated terminology is a combination of a static basic terminology and dynamic terminology created by your scriptable Java application. The file "MRJ Scripting aete" includes the basic terminologies and the AWT component terminologies. MRJ scripting automatically generates additional terminologies for all components in open windows of Java applications; in this way MRJ generates all basic terminology needed to make an MRJ application scriptable. To include the basic resources in a Java application, use JBindery or ResEdit.
Once the basic
If it is not practical to open all the windows manually, or if there are scriptable classes that are not AWT components, you can use scripting itself to ensure terminologies will be generated for those classes. In the basic dictionary there is a suite called the "Developer Suite," which contains commands and classes that designed to assist the developer, not the end user. In this suite, the
Once you have touched (manually or by scripting) all the classes you wish to expose, the next step is to retrieve the dynamic
terminologies. You do this with the same
In our terminology we also enhance the The result of the
When installing the "Add Resource" scripting addition, increase the partition size of the Script Editor to about 1500K, and enter and
run this script. Then you can find the resource in the resulting
"HD:TestRes" file and edit it with your favorite
When cleaning up the terminology for the scripter, you should delete all the events and properties that are not relevant. You may want to delete non-relevant classes well; however, if dynamic terminology is on, then
the terminologies of classes you have removed will show up again dynamically. Currently, to get around the problem, you can delete all events and properties of those classes but leave the classes around. Since the dynamic terminology generation does not try to override the classes already in the You should never change the four-character codes in the aete. However, you are encouraged to change the names of events, parameters, classes, and properties if they are generated from programming names and not the display names. You should also add comments to inform the scripter what the individual classes are and how they are used.
After you have edited the
Dynamic terminology is controlled by the
You are strongly encouraged to edit and trim down your Changes in MRJ 2.2MRJ 2.2 has made some significant changes to the scripting model used by the MRJ. These changes lie in two main areas: changes to the terminology, and improved support for alternative containment hierarchies. These changes are documented in About MRJ Scripting which is part of the MRJ SDK 2.2. Terminology Changes There are some changes in the terminology to avoid conflicts with built-in AppleScript key words:
Java array support has been improved. In previous MRJ versions, it was possible to get scripting errors when referring to the index of an array item. In MRJ 2.2, these type of references are now possible:
This operation is now possible and will return the value {1, 4, 3} where previously, it would have caused an error. Additionally, it is now possible to manipulate Java Vector objects just like an AppleScript Array. For example, you can now do the following:
This operation will return the result "gamma". In previous versions of the MRJ, the only way to manipulate Vector objects was via the class methods. The "make" event of the core suite is now supported. It is analogous to calling the constructor of a class directly. For example: Corresponds to calling "new Frame( )" from Java. You can even do more complex operations such as:
This is equivalent to the following:
Finally, MRJ 2.2 adds support for object references. For example, if you want to store an object reference for later use, you can store it in the "object collection" property of the Java application. For example:
This saves a reference to the new xyz object under the variable name "alpha", If you need to retrieve that reference later, you may refer to it as: Note that if AppleScript is holding a reference to a particular object, the garbage collector cannot free that memory even if the object is no longer in use. You must explicitly remove the reference from the object collection as follows: Support for Alternative Containment Hierarchies In the MRJ, typically objects must be referenced from AppleScript in terms of the visual containment hierarchy. Thus, if a button is in a panel in a window, you must access the button as follows: This can be a nuisance if you have a deep containment hierarchy such as in the case of swing. In MRJ 2.2, a developer may specify an alternate hierarchy by implementing the com.apple.scripting.AlternateHierarchy interface. This bypasses the traditional visual containment hierarchy. For example:
This allows the developer to write the following script: regardless of how many layers deep button 2 is. However, it is the responsibility of the developer to keep track of items in the custom hierarchy and writing the retrieval method. SummaryIncorporating AppleScript into your Java application allows increased
automation support. Adding basic scriptability is very simple. You need only to add a pre-generated
ReferencesAppleScript Language Guide: English Dialect MRJ SDK 2.2 (About MRJ Scripting) Downloadables
|
|