Writing Scripts in BeanShell and Java

Contents

Hello World in BeanShell

Here's a BeanShell script that inserts Hello World at the start of an OpenOffice.org Writer document:

    import com.sun.star.frame.XModel;
    import com.sun.star.text.*;
    import com.sun.star.uno.UnoRuntime;
    import drafts.com.sun.star.script.framework.XScriptContext;

    model = context.getDocument(); 
    textdoc = (XTextDocument)
        UnoRuntime.queryInterface(XTextDocument.class, model);

    oText = textdoc.getText(); 
    oCursor = oText.createTextCursor(); 
    oText.insertString(oCursor, "Hello World", false)
Top

Trying out your BeanShell script

Trying 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. If you are new to the OpenOffice.org API this is a great way to experiment with it.

Note: The Interactive BeanShell window does not report when an error occurs while evaluating your code, so it may fail silently. The best way to trace the execution of your code is to write debug statements into your document from your script, for example:

model = context.getDocument();
textdoc = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, model);
oText = textdoc.getText();
oCursor = oText.createTextCursor();

oText.insertString(oCursor, "DEBUG: start", false);

// do something with the API

oText.insertString(oCursor, "DEBUG: did something", false);

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

Hello World in Java

Here's the Hello World script in Java:

    import com.sun.star.frame.XModel;
    import com.sun.star.text.*;
    import com.sun.star.uno.UnoRuntime;
    import drafts.com.sun.star.script.framework.XScriptContext;

    public class MyClass {

        // The script method must be public
        // It can either be static or non-static

        public void showForm(XScriptContext xSc) {

            // getting the text document object
            XModel xmodel = xSc.getDocument();

            XTextDocument xtextdoc = (XTextDocument)
                UnoRuntime.queryInterface(XTextDocument.class, xmodel);
            XText xtext = xtextdoc.getText();
            XTextCursor xtextcursor = xtext.createTextCursor();

            xtext.insertString(xtextcursor, "Hello World", false);
        }
    }

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.

Top

Writing Office scripts and the XScriptContext type

The 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:

The Java or BeanShell script must import the XScriptContext interface, using the following import directive:
    import drafts.com.sun.star.script.framework.XScriptContext;

Top

Tips on writing Office scripts

Top

Parcel Descriptor DTD and sample XML

Each 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"?>
<!-- DTD for Parcel Meta data for use in the OpenOffice.org Scripting Framework Project -->
<!ELEMENT logicalname EMPTY>
<!ELEMENT description (#PCDATA)>
<!ELEMENT displayname EMPTY>
<!ELEMENT locale (displayname?, description?)>
<!ELEMENT functionname EMPTY>
<!ELEMENT prop EMPTY>
<!ELEMENT languagedepprops (prop+)>
<!ELEMENT file (prop*)>
<!ELEMENT fileset (file+)>
<!ELEMENT script (locale+, functionname, logicalname, languagedepprops*, fileset*)>
<!ELEMENT parcel (script+)>
<!ATTLIST logicalname
	value CDATA #REQUIRED
>
<!ATTLIST displayname
	value CDATA #REQUIRED
>
<!ATTLIST locale
	lang CDATA #REQUIRED
>
<!ATTLIST functionname
	value CDATA #REQUIRED
>
<!ATTLIST logicalname
	value CDATA #REQUIRED
>
<!ATTLIST prop
	name CDATA #REQUIRED
	value CDATA #REQUIRED
>
<!ATTLIST file
	name CDATA #REQUIRED
>
<!ATTLIST fileset
	name CDATA #IMPLIED
>
<!ATTLIST script
	language CDATA #REQUIRED
>
<!ATTLIST parcel
	language CDATA #REQUIRED
>
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"?>
<!--Sample Meta Data for use with the Scripting Framework Project in OpenOffice.org -->
<!DOCTYPE parcel SYSTEM "parcel.dtd">
<parcel language="Java">
	<script language="Java">
		<locale lang="english">
			<displayname value="Memory.usage"/>
			<description>
				Displays the memory current memory usage
			</description>
		</locale>
		<functionname value="memoryUtils.memoryUsage"/>
		<logicalname value="MemoryUtils.MemUsage"/>
		<languagedepprops>
			<prop name="classpath" value="/opt/foo.jar:/usr/java/src.jar"/>
		</languagedepprops>
		<fileset>
			<file name="mems.txt">
				<prop name="type" value="resource"/>
			</file>
		</fileset>
	</script>
</parcel>
Top
Last Modified: Tue Mar 12 11:40:28 GMT 2003