This article is old and is being consolidated into the book.
Please refer to the corresponding chapter(s) therein.
If the chapters or sections are not completed yet, you can use this article.
Refer to the examples as they are tested against the latest code.
COM Scripting with JudoScriptBy James Jianbo Huang December 2002 non-printer versionAbstract Microsoft's COM and Java are the two most popular object models. JudoScript has native
support for both. In JudoScript, ActiveX controls are obtained via the operator
Currently ActiveX event handling is not supported due to the limitation of the underlying JCom package, and out-bound parameters are not supported in a more meaningful way. These situations may improve in the later versions.
1. Introduction to COM and ActiveX
Microsoft's Component Object Model (COM) and Java's object model are the two most important object models to date. JudoScript, as a language designed for Java scripting, has also native support for COM/ActiveX scripting. JudoScript seamlessly bridges these two powerful object models (without the Microsoft JVM). In the early days of Windows, most programs are monolithic executables. DLLs provide limited flexibility and reusability. It was the "crazy" idea of embedding spreadsheets in word documents and vice versa that prompted the concept of OLE, which eventually led to the ubiquitous COM on Windows platforms. Today, most of the major Windows software are presented in the form of ActiveX (what a funky name!), making them readily scriptable with capable scripting languages like Visual Basic and JudoScript. All COM objects are registered in the Windows registry when the software is installed. Windows have APIs to obtain such objects via GUIDs (Globally Unique IDentifiers). COM objects implements various interfaces; each interface is identified by a GUID and is also registered on the system. Every single COM object implements an interface called IUnknown, which is not useful in itself but allows clients to obtain other interfaces that it implements. Hence, in order to use a COM object, you will need to know its GUID and its interfaces. There are ways to describe and inspect those interfaces offline, but this is not generally available at runtime and type matching is challenging if possible at all. What this means is, general COM objects are not as easily scriptable with scripting languages. In order to make COM objects scriptable, Microsoft has defined a special interface called
2. ActiveX Scripting with JudoScript
To get hold of an ActiveX control in JudoScript, use the
Microsoft Windows has aliases for ActiveX identifiers. In the above example, we
instantiates a "ScriptControl", set its
This program brings up the Internet Explorer, prints out the path for the executable (line 6), visits Yahoo! and Google respectively for 5 seconds, then closes the brower. Like calling methods in Java, you can cast values to Java primitive types. For
ActiveX invocation, there is one more type, Parameterized Property Getting
One of the main reasons to script ActiveX controls is to script Microsoft Office products such as Word and Excel. The following is an example that uses Excel.
It launches Excel, creates a new workbook, sets a value at cell "A1" and sets a
formula at cell "A2", then prints out the values at "A1" and "A2". On lines 8 and
9, it uses parameterized property get; its syntax is the same as the
multi-dimensional array access operator. Similarly, a simple property access for
ActiveX controls can be conceived as a single-dimensional array access. (It would
be preferrable to use a syntax like
The following is an example that uses Microsoft Word. It opens a Word document and prints out its words and tables. Using this program as a template, you can do a lot more with Word; you will need to know the Word's DOM, which is not the topic of this article; this information is available in the help files of the Word product, or any books about VBA for Word.
IEnumVARIANT Interface
One of the COM interface, IEnumVARIANT, represents a collection of values. It can be returned by method calls. JudoScript directly converts an IEnumVARIANT into an array.
3. Setting Up and Java-COM-Bridge APIs
At this writing, JudoScript uses the JCom
package for its ActiveX support. Java classes of JCom are already included in the
JudoScript. JCom's JNI DLL, jcom.dll, must be put in the JCom package is a straightforward Java-COM bridge. Its API is concise and elegant, and maps Java and COM data types well. However, JudoScript has a wish-list from a Java-COM bridge provider that would support by-reference parameters and event handling. So JudoScript has not ruled out the possibility of creating its own Java-COM implementation. For now, if scripting Microsoft Word and Excel is functioning well, it is deemed good enough, unless users strongly request the aforementioned features.
4. Summary
JudoScript bridges the two most powerful object models -- COM/ActiveX and Java. In JudoScript,
scripting ActiveX controls is no different than scripting any other objects, including
Java objects; the only difference is the way to obtain an object: ActiveX controls are
obtained via the Currently, JudoScript does not handle ActiveX event handling, and by-reference parameters are not explicitly supported, that is, the out-bound parameters are not supported. This, however, may change in the future releases.
5. Code Listings
|