Language

The Free and Open Productivity Suite
Released: Apache OpenOffice 4.1.15

General Developer Guidelines

Writing Office scripts and the XScriptContext type

All Java script methods must pass in XScriptContext as their first parameter. The developer can then use accessor functions on the scripting context to get at the document context, desktop and component factory as follows: The Script must import the XScriptContext interface, using the following import directive:
    import drafts.com.sun.star.script.framework.XScriptContext;
Example usage in a Java script method to access a Writer document and insert some text:

    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);
        }
    }

Tips on writing Office scripts

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

How to Bind Scripts

Binding scripts to Menu items Binding scripts to Shortcut Keys
  • Finally click on the OK button to save your new script shortcut key.
  • Restart OpenOffice.org and open a document
  • Pressing your new script shortcut key will invoke the Java script. Note: Menu items and Shortcut keys are not updated on the fly, hence the need to restart, this will be addressed in a future release.
    Last Modified: Wed Jan 15 16:25:01 GMT 2003
  • Apache Software Foundation

    Copyright & License | Privacy | Contact Us | Donate | Thanks

    Apache, OpenOffice, OpenOffice.org and the seagull logo are registered trademarks of The Apache Software Foundation. The Apache feather logo is a trademark of The Apache Software Foundation. Other names appearing on the site may be trademarks of their respective owners.