Difference between revisions of "Documentation/DevGuide/Scripting/Writing Macros"
(More information on scripts, reorganization of the page) |
m (→Compiling and Deploying Java macros) |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 8: | Line 8: | ||
__NOTOC__ | __NOTOC__ | ||
=== The HelloWorld macro === | === The HelloWorld macro === | ||
− | Here is a comparison of HelloWorld macros in the different script languages available in {{ | + | Here is a comparison of HelloWorld macros in the different script languages available in {{AOo}}. |
==== Basic ==== | ==== Basic ==== | ||
− | + | <syntaxhighlight lang="oobas"> | |
Sub writeHelloWorld() | Sub writeHelloWorld() | ||
Dim oDoc As Object, xText As Object, xTextRange As Object | Dim oDoc As Object, xText As Object, xTextRange As Object | ||
Line 26: | Line 26: | ||
xTextRange.setString( "Hello World (in Basic)" ) | xTextRange.setString( "Hello World (in Basic)" ) | ||
End Sub | End Sub | ||
− | + | </syntaxhighlight> | |
Basic interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern: | Basic interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern: | ||
− | + | <syntaxhighlight lang="idl"> | |
SomeType getSomeProperty() | SomeType getSomeProperty() | ||
void setSomeProperty(SomeType aValue) | void setSomeProperty(SomeType aValue) | ||
− | + | </syntaxhighlight> | |
Using these facilities some lines can be simplified: | Using these facilities some lines can be simplified: | ||
− | + | <syntaxhighlight lang="oobas"> | |
xText = oDoc.Text | xText = oDoc.Text | ||
xTextRange = xText.End | xTextRange = xText.End | ||
xTextRange.String = "Hello World (in Basic)" | xTextRange.String = "Hello World (in Basic)" | ||
− | + | </syntaxhighlight> | |
Service properties can be directly accessed in Basic, it is not necessary to use <code>getPropertyValue</code> or <code>setPropertyValue</code> methods. | Service properties can be directly accessed in Basic, it is not necessary to use <code>getPropertyValue</code> or <code>setPropertyValue</code> methods. | ||
Line 43: | Line 43: | ||
==== BeanShell ==== | ==== BeanShell ==== | ||
As BeanShell accepts typeless variables, the code is simplified compared to Java. | As BeanShell accepts typeless variables, the code is simplified compared to Java. | ||
− | + | <syntaxhighlight lang="java"> | |
import com.sun.star.uno.UnoRuntime; | import com.sun.star.uno.UnoRuntime; | ||
import com.sun.star.text.XTextDocument; | import com.sun.star.text.XTextDocument; | ||
Line 68: | Line 68: | ||
xTextRange.setString( "Hello World (in BeanShell)" ); | xTextRange.setString( "Hello World (in BeanShell)" ); | ||
return 0; | return 0; | ||
− | + | </syntaxhighlight> | |
BeanShell interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern: | BeanShell interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern: | ||
− | + | <syntaxhighlight lang="idl"> | |
SomeType getSomeProperty() | SomeType getSomeProperty() | ||
void setSomeProperty(SomeType aValue) | void setSomeProperty(SomeType aValue) | ||
− | + | </syntaxhighlight> | |
Using these facilities some lines can be simplified: | Using these facilities some lines can be simplified: | ||
− | + | <syntaxhighlight lang="java"> | |
xText = xTextDoc.Text; | xText = xTextDoc.Text; | ||
xTextRange = xText.End; | xTextRange = xText.End; | ||
xTextRange.String = "Hello World (in BeanShell)"; | xTextRange.String = "Hello World (in BeanShell)"; | ||
− | + | </syntaxhighlight> | |
Service properties are only accessed with <code>getPropertyValue</code> or <code>setPropertyValue</code>. | Service properties are only accessed with <code>getPropertyValue</code> or <code>setPropertyValue</code>. | ||
Line 87: | Line 87: | ||
==== JavaScript ==== | ==== JavaScript ==== | ||
As JavaScript accepts typeless variables, the code is simplified compared to Java. | As JavaScript accepts typeless variables, the code is simplified compared to Java. | ||
− | + | <syntaxhighlight lang="javascript"> | |
importClass(Packages.com.sun.star.uno.UnoRuntime) | importClass(Packages.com.sun.star.uno.UnoRuntime) | ||
importClass(Packages.com.sun.star.text.XTextDocument) | importClass(Packages.com.sun.star.text.XTextDocument) | ||
Line 111: | Line 111: | ||
pv.setPropertyValue("CharPosture", Packages.com.sun.star.awt.FontSlant.ITALIC) | pv.setPropertyValue("CharPosture", Packages.com.sun.star.awt.FontSlant.ITALIC) | ||
xTextRange.setString( "Hello World (in JavaScript)" ) | xTextRange.setString( "Hello World (in JavaScript)" ) | ||
− | + | </syntaxhighlight> | |
JavaScript does not accept the simplifications seen in Basic and BeanShell. | JavaScript does not accept the simplifications seen in Basic and BeanShell. | ||
Line 118: | Line 118: | ||
==== Java ==== | ==== Java ==== | ||
− | Other sections of this document provide numerous Java examples. Here is the HelloWorld provided in {{ | + | Other sections of this document provide numerous Java examples. Here is the HelloWorld provided in {{AOo}} |
− | + | <syntaxhighlight lang="java"> | |
import com.sun.star.uno.UnoRuntime; | import com.sun.star.uno.UnoRuntime; | ||
import com.sun.star.frame.XModel; | import com.sun.star.frame.XModel; | ||
Line 141: | Line 141: | ||
} | } | ||
} | } | ||
− | + | </syntaxhighlight> | |
===== Compiling and Deploying Java macros ===== | ===== Compiling and Deploying Java macros ===== | ||
− | Because Java is a compiled language it is not possible to execute Java source code as a macro directly from within {{ | + | Because Java is a compiled language it is not possible to execute Java source code as a macro directly from within {{AOo}}. The code must first be compiled and then deployed within a {{PRODUCTNAME}} installation or document. The following steps show how to create a Java macro using the HelloWorld example code: |
* Create a ''HelloWorld'' directory for your macro | * Create a ''HelloWorld'' directory for your macro | ||
* Create a ''HelloWorld.java'' file using the HelloWorld source code | * Create a ''HelloWorld.java'' file using the HelloWorld source code | ||
− | * Compile the ''HelloWorld.java'' file. The following jar files from the ''program/classes'' directory of a {{ | + | * Compile the ''HelloWorld.java'' file. The following jar files from the ''program/classes'' directory of a {{AOo}} installation must be in the classpath: ''ridl.jar'', ''unoil.jar'', ''jurt.jar'' |
* Create a ''HelloWorld.jar'' file containing the ''HelloWorld.class'' file | * Create a ''HelloWorld.jar'' file containing the ''HelloWorld.class'' file | ||
* Create a ''parcel-descriptor.xml'' file for your macro | * Create a ''parcel-descriptor.xml'' file for your macro | ||
− | + | <syntaxhighlight lang="xml"> | |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
Line 170: | Line 170: | ||
</script> | </script> | ||
</parcel> | </parcel> | ||
− | + | </syntaxhighlight> | |
The ''parcel-descriptor.xml'' file is used by the Scripting Framework to find macros. The functionname element indicates the name of the Java method which should be executed as a macro. The classpath element can be used to indicate any jar or class files which are used by the macro. If the classpath element is not included, then the directory in which the ''parcel-desciptor.xml'' file is found and any jar files in that directory will be used as the classpath. The necessary Java UNO classes are available automatically. | The ''parcel-descriptor.xml'' file is used by the Scripting Framework to find macros. The functionname element indicates the name of the Java method which should be executed as a macro. The classpath element can be used to indicate any jar or class files which are used by the macro. If the classpath element is not included, then the directory in which the ''parcel-desciptor.xml'' file is found and any jar files in that directory will be used as the classpath. The necessary Java UNO classes are available automatically. | ||
− | * Copy the HelloWorld directory into the ''share/Scripts/java'' directory of | + | * Copy the HelloWorld directory into the ''share/Scripts/java'' (or ''basis/share/Scripts/java'') directory of an {{AOo}} installation or into the ''user/Scripts/java'' directory of a user installation. If you want to deploy the macro to a document you need to place it in a ''Scripts/java'' directory within the document zip file. |
− | * If {{ | + | * If {{AOo}} is running, you will need to restart it in order for the macro to appear in the Macro Selector dialog. |
− | {{ | + | {{Note|The ''parcel-descriptor.xml'' file is also used to detect BeanShell and JavaScript macros. It is created automatically when creating macros using the Organizer dialogs for BeanShell and JavaScript.}} |
+ | |||
+ | If these files are embedded in document file, they have to be registered in META-INF/manifest.xml: | ||
+ | <syntaxhighlight lang="xml"> | ||
+ | <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="Scripts/java/Test/parcel-descriptor.xml"/> | ||
+ | <manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/java/Test/test.jar"/> | ||
+ | <manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/java/Test/"/> | ||
+ | <manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/java/"/> | ||
+ | <manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/"/> | ||
+ | </syntaxhighlight> | ||
==== Python ==== | ==== Python ==== | ||
A Python module may contain several scripts. | A Python module may contain several scripts. | ||
− | < | + | <syntaxhighlight lang="python"> |
def HelloPython( ): | def HelloPython( ): | ||
import uno | import uno | ||
Line 199: | Line 208: | ||
xTextRange.setString( "Hello World (in Python)" ) | xTextRange.setString( "Hello World (in Python)" ) | ||
return None | return None | ||
− | </ | + | </syntaxhighlight> |
Python interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern: | Python interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern: | ||
− | + | <syntaxhighlight lang="idl"> | |
SomeType getSomeProperty() | SomeType getSomeProperty() | ||
void setSomeProperty(SomeType aValue) | void setSomeProperty(SomeType aValue) | ||
− | + | </syntaxhighlight> | |
Using these facilities some lines can be simplified: | Using these facilities some lines can be simplified: | ||
− | < | + | <syntaxhighlight lang="python"> |
xText = oDoc.Text | xText = oDoc.Text | ||
xTextRange = xText.End | xTextRange = xText.End | ||
xTextRange.String = "Hello World (in Python)" | xTextRange.String = "Hello World (in Python)" | ||
− | </ | + | </syntaxhighlight> |
Service properties can be directly accessed in Python, no need to use <code>getPropertyValue</code> or <code>setPropertyValue</code>. | Service properties can be directly accessed in Python, no need to use <code>getPropertyValue</code> or <code>setPropertyValue</code>. | ||
− | === Features of script languages supported by {{ | + | === Features of script languages supported by {{AOo}} === |
{|border="1" cellpadding=4 style="border-collapse:collapse;" | {|border="1" cellpadding=4 style="border-collapse:collapse;" | ||
Line 296: | Line 305: | ||
− | === Using the {{ | + | === Using the {{AOo}} API from macros === |
− | BeanShell, JavaScript, Java, Python macros are supplied with a variable of type <idl>com.sun.star.script.provider.XScriptContext</idl> which can be used to access the {{ | + | BeanShell, JavaScript, Java, Python macros are supplied with a variable of type <idl>com.sun.star.script.provider.XScriptContext</idl> which can be used to access the {{AOo}} API. This interface has three methods: |
* <idl>com.sun.star.frame.XModel</idl> <code>getDocument( )</code> | * <idl>com.sun.star.frame.XModel</idl> <code>getDocument( )</code> | ||
Line 337: | Line 346: | ||
Each of the arguments in the <code>Object[]</code> are of the UNO type Any. For more information on how the Any type is used in Java see [[Documentation/DevGuide/ProUNO/Java/Type Mappings|Type Mappings]]. | Each of the arguments in the <code>Object[]</code> are of the UNO type Any. For more information on how the Any type is used in Java see [[Documentation/DevGuide/ProUNO/Java/Type Mappings|Type Mappings]]. | ||
− | The ButtonPressHandler macros in the Highlight library of a {{ | + | The ButtonPressHandler macros in the Highlight library of a {{AOo}} installation show how a macro can handle arguments. |
* '''Python''': The arguments are passed as parameters of the called function. | * '''Python''': The arguments are passed as parameters of the called function. | ||
Line 359: | Line 368: | ||
The following code shows how to create a dialog from a Java macro: | The following code shows how to create a dialog from a Java macro: | ||
− | + | <syntaxhighlight lang="java"> | |
public XDialog getDialog(XScriptContext context) | public XDialog getDialog(XScriptContext context) | ||
{ | { | ||
Line 390: | Line 399: | ||
return theDialog; | return theDialog; | ||
} | } | ||
− | + | </syntaxhighlight> | |
=== Calling a macro with the Scripting Framework === | === Calling a macro with the Scripting Framework === | ||
Line 397: | Line 406: | ||
The identity and location of the called script is contained in an URI, which follows a [[Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification|particular syntax]]. | The identity and location of the called script is contained in an URI, which follows a [[Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification|particular syntax]]. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Latest revision as of 15:11, 14 January 2024
The HelloWorld macro
Here is a comparison of HelloWorld macros in the different script languages available in Apache OpenOffice.
Basic
Sub writeHelloWorld() Dim oDoc As Object, xText As Object, xTextRange As Object oDoc = ThisComponent xText = oDoc.getText() xTextRange = xText.getEnd() xTextRange.CharBackColor = 1234567 xTextRange.CharHeight = 16.0 ' CharUnderline receives a group constant xTextRange.CharUnderline = com.sun.star.awt.FontUnderline.WAVE ' CharPosture receives an enum xTextRange.CharPosture = com.sun.star.awt.FontSlant.ITALIC xTextRange.setString( "Hello World (in Basic)" ) End Sub
Basic interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern:
SomeType getSomeProperty() void setSomeProperty(SomeType aValue)
Using these facilities some lines can be simplified:
xText = oDoc.Text xTextRange = xText.End xTextRange.String = "Hello World (in Basic)"
Service properties can be directly accessed in Basic, it is not necessary to use getPropertyValue
or setPropertyValue
methods.
BeanShell
As BeanShell accepts typeless variables, the code is simplified compared to Java.
import com.sun.star.uno.UnoRuntime; import com.sun.star.text.XTextDocument; import com.sun.star.text.XText; import com.sun.star.text.XTextRange; import com.sun.star.beans.XPropertySet; import com.sun.star.awt.FontSlant; import com.sun.star.awt.FontUnderline; oDoc = XSCRIPTCONTEXT.getDocument(); xTextDoc = UnoRuntime.queryInterface(XTextDocument.class,oDoc); xText = xTextDoc.getText(); xTextRange = xText.getEnd(); pv = UnoRuntime.queryInterface(XPropertySet.class, xTextRange); pv.setPropertyValue("CharBackColor", 1234567); pv.setPropertyValue("CharHeight", 16.0); // CharUnderline receives a group constant pv.setPropertyValue("CharUnderline", com.sun.star.awt.FontUnderline.WAVE); // CharPosture receives an enum pv.setPropertyValue("CharPosture", com.sun.star.awt.FontSlant.ITALIC) ; xTextRange.setString( "Hello World (in BeanShell)" ); return 0;
BeanShell interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern:
SomeType getSomeProperty() void setSomeProperty(SomeType aValue)
Using these facilities some lines can be simplified:
xText = xTextDoc.Text; xTextRange = xText.End; xTextRange.String = "Hello World (in BeanShell)";
Service properties are only accessed with getPropertyValue
or setPropertyValue
.
JavaScript
As JavaScript accepts typeless variables, the code is simplified compared to Java.
importClass(Packages.com.sun.star.uno.UnoRuntime) importClass(Packages.com.sun.star.text.XTextDocument) importClass(Packages.com.sun.star.text.XText) importClass(Packages.com.sun.star.text.XTextRange) importClass(Packages.com.sun.star.beans.XPropertySet) importClass(Packages.com.sun.star.awt.FontSlant) importClass(Packages.com.sun.star.awt.FontUnderline) oDoc = XSCRIPTCONTEXT.getDocument() xTextDoc = UnoRuntime.queryInterface(XTextDocument,oDoc) xText = xTextDoc.getText() xTextRange = xText.getEnd() pv = UnoRuntime.queryInterface(XPropertySet, xTextRange) pv.setPropertyValue("CharHeight", 16.0) // Double // CharBackColor receives an Integer pv.setPropertyValue("CharBackColor", new java.lang.Integer(1234567)) // CharUnderline receives a group constant pv.setPropertyValue("CharUnderline", new java.lang.Short(Packages.com.sun.star.awt.FontUnderline.WAVE)) // CharPosture receives an enum pv.setPropertyValue("CharPosture", Packages.com.sun.star.awt.FontSlant.ITALIC) xTextRange.setString( "Hello World (in JavaScript)" )
JavaScript does not accept the simplifications seen in Basic and BeanShell.
Setting an integer value to a property requires to use a java class.
Java
Other sections of this document provide numerous Java examples. Here is the HelloWorld provided in Apache OpenOffice
import com.sun.star.uno.UnoRuntime; import com.sun.star.frame.XModel; import com.sun.star.text.XTextDocument; import com.sun.star.text.XTextRange; import com.sun.star.text.XText; import com.sun.star.script.provider.XScriptContext; public class HelloWorld { public static void printHW(XScriptContext xScriptContext) { XModel xDocModel = xScriptContext.getDocument(); // getting the text document object XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface( XTextDocument.class, xDocModel); XText xText = xtextdocument.getText(); XTextRange xTextRange = xText.getEnd(); xTextRange.setString( "Hello World (in Java)" ); } }
Compiling and Deploying Java macros
Because Java is a compiled language it is not possible to execute Java source code as a macro directly from within Apache OpenOffice. The code must first be compiled and then deployed within a OpenOffice.org installation or document. The following steps show how to create a Java macro using the HelloWorld example code:
- Create a HelloWorld directory for your macro
- Create a HelloWorld.java file using the HelloWorld source code
- Compile the HelloWorld.java file. The following jar files from the program/classes directory of a Apache OpenOffice installation must be in the classpath: ridl.jar, unoil.jar, jurt.jar
- Create a HelloWorld.jar file containing the HelloWorld.class file
- Create a parcel-descriptor.xml file for your macro
<?xml version="1.0" encoding="UTF-8"?> <parcel language="Java" xmlns:parcel="scripting.dtd"> <script language="Java"> <locale lang="en"> <displayname value="HelloWorld"/> <description> Prints "Hello World". </description> </locale> <functionname value="HelloWorld.printHW"/> <logicalname value="HelloWorld.printHW"/> <languagedepprops> <prop name="classpath" value="HelloWorld.jar"/> </languagedepprops> </script> </parcel>
The parcel-descriptor.xml file is used by the Scripting Framework to find macros. The functionname element indicates the name of the Java method which should be executed as a macro. The classpath element can be used to indicate any jar or class files which are used by the macro. If the classpath element is not included, then the directory in which the parcel-desciptor.xml file is found and any jar files in that directory will be used as the classpath. The necessary Java UNO classes are available automatically.
- Copy the HelloWorld directory into the share/Scripts/java (or basis/share/Scripts/java) directory of an Apache OpenOffice installation or into the user/Scripts/java directory of a user installation. If you want to deploy the macro to a document you need to place it in a Scripts/java directory within the document zip file.
- If Apache OpenOffice is running, you will need to restart it in order for the macro to appear in the Macro Selector dialog.
The parcel-descriptor.xml file is also used to detect BeanShell and JavaScript macros. It is created automatically when creating macros using the Organizer dialogs for BeanShell and JavaScript. |
If these files are embedded in document file, they have to be registered in META-INF/manifest.xml:
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="Scripts/java/Test/parcel-descriptor.xml"/> <manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/java/Test/test.jar"/> <manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/java/Test/"/> <manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/java/"/> <manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/"/>
Python
A Python module may contain several scripts.
def HelloPython( ): import uno def HelloPython( ): oDoc = XSCRIPTCONTEXT.getDocument() xText = oDoc.getText() xTextRange = xText.getEnd() xTextRange.CharHeight = 16.0 xTextRange.CharBackColor = 1234567 # CharUnderline receives a group constant xTextRange.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.WAVE") # CharPosture receives an enum xTextRange.CharPosture = uno.getConstantByName("com.sun.star.awt.FontSlant.ITALIC") xTextRange.setString( "Hello World (in Python)" ) return None
Python interprets pairs of get and set methods at UNO objects as object properties if they follow this pattern:
SomeType getSomeProperty() void setSomeProperty(SomeType aValue)
Using these facilities some lines can be simplified:
xText = oDoc.Text xTextRange = xText.End xTextRange.String = "Hello World (in Python)"
Service properties can be directly accessed in Python, no need to use getPropertyValue
or setPropertyValue
.
Features of script languages supported by Apache OpenOffice
Language feature | Basic | BeanShell | JavaScript | Java macro | Python |
---|---|---|---|---|---|
Interpreted | Yes | Yes | Yes | No | Yes |
Integrated editor | Yes, colored | Yes | Yes | No | No |
Integrated debugger | Yes | No | Yes | No | No |
Script organizer | Yes | Yes | Yes | No | No |
Script encryption | Possible | No | No | No | No |
Typeless variables | Yes: Variant | Yes | Yes | No | Yes |
Getter/Setter | Yes | Yes | No | No | Yes |
Direct property access | Yes | No | No | No | Yes |
Function for Calc formula | Yes | No | No | No | No |
UNO component creation | No | No | No | Yes | Yes |
Using the Apache OpenOffice API from macros
BeanShell, JavaScript, Java, Python macros are supplied with a variable of type com.sun.star.script.provider.XScriptContext which can be used to access the Apache OpenOffice API. This interface has three methods:
- com.sun.star.frame.XModel
getDocument( )
- Returns the
XModel
interface of the document for which the macro was invoked (see Using the Component Framework)
- com.sun.star.frame.XDesktop
getDesktop( )
- Returns the
XDesktop
interface for the application which can be used to access open document, and load documents (see Using the Desktop)
- com.sun.star.uno.XComponentContext
getComponentContext( )
- Returns the
XComponentContext
interface which is used to create instances of services (see Service Manager and Component Context)
Depending on the language the macro accesses XScriptContext
in different ways:
- BeanShell: Using the global variable
XSCRIPTCONTEXT
oDoc = XSCRIPTCONTEXT.getDocument();
- JavaScript: Using the global variable
XSCRIPTCONTEXT
oDoc = XSCRIPTCONTEXT.getDocument();
- Java: The first parameter passed to the macro method is always of type
XScriptContext
Xmodel xDocModel = xScriptContext.getDocument();
- Python: Using the global variable
XSCRIPTCONTEXT
oDoc = XSCRIPTCONTEXT.getDocument()
Handling arguments passed to macros
In certain cases arguments may be passed to macros, for example, when a macro is assigned to a button in a document. In this case the arguments are passed to the macro as follows:
- BeanShell: In the global
Object[]
variableARGUMENTS
event = (ActionEvent) ARGUMENTS[0];
- JavaScript: In the global
Object[]
variableARGUMENTS
event = ARGUMENTS[0];
- Java: The arguments are passed as an
Object[]
in the second parameter to the macro method
public void handleButtonPress( XScriptContext xScriptContext, Object[] args)
Each of the arguments in the Object[]
are of the UNO type Any. For more information on how the Any type is used in Java see Type Mappings.
The ButtonPressHandler macros in the Highlight library of a Apache OpenOffice installation show how a macro can handle arguments.
- Python: The arguments are passed as parameters of the called function.
Creating dialogs from macros
Dialogs which have been built in the Dialog Editor can be loaded by macros using the com.sun.star.awt.XDialogProvider API. The method createDialog()
from interface XDialogProvider
uses a string as a parameter. This string is the URL to the dialog. It is formed as follows:
vnd.sun.star.script:DIALOGREF?location=[application|document]
where DIALOGREF
is the name of the dialog that you want to create, and location is either application or document depending on where the dialog is stored.
For example if you wanted to load dialog called MyDialog, which is in a Dialog Library called MyDialogLibrary in the OpenOffice.org dialogs area of your installation then the URL would be:
vnd.sun.star.script:MyDialogLibrary.MyDialog?location=application
If you wanted to load a dialog called MyDocumentDialog which in a library called MyDocumentLibrary which is located in a document then the URL would be:
vnd.sun.star.script:MyDocumentLibrary.MyDocumentDialog?location=document
The following code shows how to create a dialog from a Java macro:
public XDialog getDialog(XScriptContext context) { XDialog theDialog; // We must pass the XModel of the current document when creating a DialogProvider object Object[] args = { context.getDocument() }; Object obj; try { obj = xmcf.createInstanceWithArgumentsAndContext( "com.sun.star.awt.DialogProvider", args, context.getComponentContext()); } catch (com.sun.star.uno.Exception e) { System.err.println("Error getting DialogProvider object"); return null; } XDialogProvider xDialogProvider = (XDialogProvider) UnoRuntime.queryInterface(XDialogProvider.class, obj); // Got DialogProvider, now get dialog try { theDialog = xDialogProvider.createDialog( "vnd.sun.star.script:MyDialogLibrary.MyDialog?location=application"); } catch (java.lang.Exception e) { System.err.println("Got exception on first creating dialog: " + e.getMessage()); } return theDialog; }
Calling a macro with the Scripting Framework
The service com.sun.star.script.provider.ScriptProvider exports interface XScriptProvider which offers method getScript( )
. This method returns an interface XScript which offers the method invoke( )
which will call the script with parameters if necessary.
The identity and location of the called script is contained in an URI, which follows a particular syntax.
Content on this page is licensed under the Public Documentation License (PDL). |