Technote 1162Introduction to MRJ Scripting with AppleScript for JavaCompiled by Michael Hopkins |
CONTENTS How MRJ Scripting Works | With
the release of MRJ 2.1, AppleScript support in Java applets and applications is now a reality. This Technote covers the technical information you will need to take full advantage of AppleScript in your Java application. Apple Applet Runner and the MRJShellLib (inside of MRJLib) support the Macintosh Open Scripting Architecture. As a result, the Apple
Applet Runner, the applets it runs, and applications created with JBindery can be scripted by AppleScript or any other OSA scripting language. The only additional work required (for scripting of java-based applications) is the inclusion of the scripting resources This document provides a brief overview of MRJ Scripting. It assumes that you are familiar with AppleScript and the OSA Architecture. As more information about MRJ Scripting becomes available, it will be posted on the MRJ Developer Page. |
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 Objects
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 |
SummaryIncorporating AppleScript into your Java application allows increased
automation support. Adding basic scriptability is very simple. You need only to add a pre-generated |
Thanks to Ed Lai, Levi Brown, and the Apple MRJ Engineering Team.
Mention of third-party sites and third-party products is for informational purposes only and constitutes neither an endorsement nor a recommendation. Apple assumes no responsibility with regard to the selection, performance, or use of these vendors or products. |