|
|
Writing Scripts in BeanShell and JavaContentsHello World in BeanShellHere's a BeanShell script that inserts Hello World at the start of an OpenOffice.org Writer document:
import com.sun.star.frame.XModel;Top Trying out your BeanShell scriptTrying out your Hello World BeanShell script is easy:
You can modify the code directly in the evaluation window and click eval again to test it. When you are finished making your changes, click Save to save them back to the hello.bsh file. If you are new to the OpenOffice.org API this is a great way to experiment with it. When you are happy with your BeanShell script, you can create a Script Parcel which can be deployed to OpenOffice.org installations or documents for use by others. This can be done using NetBeans or from the command line. Top
importClass(Packages.com.sun.star.uno.UnoRuntime); The XSCRIPTCONTEXT variable above is a global instance of the XScriptContext type which is available to all JavaScript scripts executed by the Scripting Framework. See Writing Office Scripts and the XScriptContext type for the methods available for the XScriptContext type. Trying out a JavaScript script in OpenOffice.orgOnce again you can use the Edit/Debug Scripts dialog to open a JavaScript script in an editor. The Rhino JavaScript Editor from the Mozilla Rhino project can be used to debug and test your JavaScript scripts.
The Rhino Debugger also includes debugging functionality, so you can set breakpoints in your JavaScript script and step through the code as it is executed. Hello World in JavaHere's the Hello World script in Java:
Office scripts in Java need to be compiled in order to execute them. See the Developing Scripts in NetBeans and Developing Scripts on the command line guides for instructions on how to compile and deploy Office scripts in Java. Writing Office scripts and the XScriptContext typeThe XScriptContext type is used to obtain the the document context, desktop and component factory from an Office script. Any public Java method which accepts XScriptContext as it's first parameter can be executed as an Office script. For BeanShell scripts, an instance of XScriptContext is available in a global variable called "context" which can be used by the script.The following accessor methods are available on the XScriptContext type:
import drafts.com.sun.star.script.framework.XScriptContext; Tips on writing Office scripts
Parcel Descriptor DTD and sample XMLEach script must contain a parcel-descriptor.xml file which provides all the necessary metadata for the script. The DTD for the parcel-descriptor.xml follows<?xml version="1.0" encoding="UTF-8"?>The following is an example of a parcel-descriptor.xml file that defines a script, implemented in Java. The languagedepprops element is used to extend the JVM's classpath. <?xml version="1.0" encoding="UTF-8"?>Top Last Modified: Fri Jun 20 15:28:34 BST 2003 |




