Language

The Free and Open Productivity Suite
Released: Apache OpenOffice 4.1.15

Writing Office Scripts in JavaScript

A prototype Script Runtime has been developed for JavaScript using the Rhino JavaScript Interpreter from the Mozilla project. This Runtime allows JavaScript scripts to be executed in OpenOffice.org. Unlike the BeanShell and Java Runtimes the JavaScript Runtime is still in development and is not yet fully functional, however it is suitable for experimenting with Office scripting in JavaScript.

Some known limitations of the JavaScript Runtime are:

Contents

Installation

Hello World in JavaScript

Here's a JavaScript script that inserts Hello World at the start of an OpenOffice.org Writer document:
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextDocument);

var oModel = ScriptContext.getDocument(); 
var oTextdoc = UnoRuntime.queryInterface(XTextDocument, oModel);
var oText = oTextdoc.getText(); 
var oCursor = oText.createTextCursor(); 

oText.insertString(oCursor, "Hello World", false);

The ScriptContext 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.

Invoking a JavaScript script in OpenOffice.org

You have two choices for making your Hello World script available for execution within OpenOffice.org:
  1. Using the command line tools

    You can use the CommandLineTools class available with the Scripting Framework to generate and deploy a Script Parcel. To do this with the Hello World script:

    • Create a directory called helloworld and create a file called helloworld.js in that directory using the code above
    • Setup the command line tools according to the instructions on the Command Line Tools page
    • To generate a script parcel run the command:

      java CommandLineTools -g -l Rhino=.js
      
    • To deploy the generated .sxp file:

      java CommandLineTools -d <path to helloworld.sxp> <path to your OpenOffice.org>/user/Scripts
      
  2. Manually deploying your script

    • Create a directory called <path to your OpenOffice.org>/user/Scripts/rhino/helloworld
    • Create a file helloworld.js in that directory with the Hello World code
    • Create a file called parcel-descriptor.xml in that directory with the following contents:

      <?xml version="1.0" encoding="UTF-8"?>
      
      <parcel language="Rhino" xmlns:parcel="scripting.dtd">
      
          <script language="Rhino">
              <locale lang="en">
                  <displayname value="HelloWorld"/>
                  <description>Insert Hello World in a document</description>
              </locale>
              <functionname value="helloworld.js"/>
              <logicalname value="HelloWorld.Rhino"/>
          </script>
      </parcel>
      

Once you have the JavaScript ScriptRuntime installed and the Hello World script deployed restart OpenOffice.org. See the Scripting Framework User Guide for instructions on how to bind your script to a menu item, key binding or event. To see your script set the Location to User and the Language to Rhino in the Assign dialogs.

If you want to change your Hello World script, just edit the helloworld.js file in a text editor, save it and rerun the script using your menu, key or event binding. You don't need to restart OpenOffice.org or rebind to the script as the Scripting Framework reloads the script each time it runs it.

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.