令状ing Scripts in BeanShell and Java
Contents
Hello World in BeanShell
Here's a BeanShell script that 挿入するs Hello World at the start of an OpenOffice.org Writer 文書:
輸入する com.sun.星/主役にする.でっちあげる,人を罪に陥れる.XModel; 輸入する com.sun.星/主役にする.text.*; 輸入する com.sun.星/主役にする.uno.UnoRuntime; 輸入する 草案s.com.sun.星/主役にする.script.枠組み.XScriptContext; model = 状況.getDocument(); textdoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, model); oText = textdoc.getText(); oCursor = oText.createTextCursor(); oText.insertString(oCursor, "Hello World", 誤った)最高の,を越す
Trying out your BeanShell script
Trying out your Hello World BeanShell script is 平易な:- Start OpenOffice.org and open a new Writer 文書.
- Select the 道具s/Scripting 追加する-on's/Interactive BeanShell Scripting... menu item.
- Paste your Hello World code from above into the window that pops up and click on the eval button.
- You should see Hello World appear at the start of the Writer 文書.
You can 修正する the code 直接/まっすぐに in the evaluation window and click eval again to 実験(する) it. If you are new to the OpenOffice.org API this is a 広大な/多数の/重要な way to 実験 with it.
公式文書,認める: The Interactive BeanShell window does not 報告(する)/憶測 when an error occurs while 評価するing your code, so it may fail silently. The best way to trace the 死刑執行 of your code is to 令状 debug 声明s into your 文書 from your script, for example:
model = 状況.getDocument(); textdoc = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, model); oText = textdoc.getText(); oCursor = oText.createTextCursor(); oText.insertString(oCursor, "DEBUG: start", 誤った); // do something with the API oText.insertString(oCursor, "DEBUG: did something", 誤った);
When you are happy with your BeanShell script, you can create a Script 小包 which can be (軍隊を)展開する,配備するd to OpenOffice.org 取り付け・設備s or 文書s for use by others. This can be done using NetBeans or from the 命令(する) line.
Hello World in Java
Here's the Hello World script in Java:
輸入する com.sun.星/主役にする.でっちあげる,人を罪に陥れる.XModel;
輸入する com.sun.星/主役にする.text.*;
輸入する com.sun.星/主役にする.uno.UnoRuntime;
輸入する 草案s.com.sun.星/主役にする.script.枠組み.XScriptContext;
public class MyClass {
// The script method must be public
// It can either be static or 非,不,無-static
public 無効の showForm(XScriptContext xSc) {
// getting the text 文書 反対する
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", 誤った);
}
}
Office scripts in Java need to be 収集するd ーするために 遂行する/発効させる them. See the Developing Scripts in NetBeans and Developing Scripts on the 命令(する) line guides for 指示/教授/教育s on how to 収集する and (軍隊を)展開する,配備する Office scripts in Java.
令状ing Office scripts and the XScriptContext type
The XScriptContext type is used to 得る the the 文書 状況, desktop and 構成要素 factory from an Office script. Any public Java method which 受託するs XScriptContext as it's first parameter can be 遂行する/発効させるd as an Office script. For BeanShell scripts, an instance of XScriptContext is 利用できる in a 全世界の variable called "状況" which can be used by the script.The に引き続いて accessor methods are 利用できる on the XScriptContext type:
- 現在の 文書 - 接近 the 文書 状況 against which
the script was invoked
<XScriptContext Instance>.getDocument()
returns ::com::sun::星/主役にする::でっちあげる,人を罪に陥れる::XModel - Office Desktop - 接近 the desktop of the running Office
<XScriptContext Instance>.getDesktop()
returns ::com::sun::星/主役にする::でっちあげる,人を罪に陥れる::XDesktop - 構成要素 Factory - 接近 a ComponentContext factory to create
other UNO 構成要素s as 要求するd
<XScriptContext Instance>.getComponentContext()
returns ::com::sun::星/主役にする::uno::XComponentContext
輸入する 草案s.com.sun.星/主役にする.script.枠組み.XScriptContext;
Tips on 令状ing Office scripts
業績/成果: 現在/一般に scripts are 存在 負担d by the Scripting 枠組み each time they are run. As such it is important to keep the size of your scripts and any 扶養家族 jar とじ込み/提出するs they are using reasonably small. In 未来 解放(する)s this script 負担ing will be optimised by changing the point at which the scripts are 負担d by OpenOffice.org and using さまざまな (武器などの)隠匿場所ing 計画/陰謀s once they are 負担d. However, the 初期の 負担 will always be 影響d by the script and it's 扶養家族 jar/class とじ込み/提出する sizes.
Threading: Scripts are run synchronously by the Scripting 枠組み. If you wish to 成し遂げる any background 仕事 or 供給する some 使用者 interaction 経由で a dialog for instance, then it is your 責任/義務 to spawn a thread in the running script which can manage this 過程 or interaction and let the script return 敏速に. Within this running thread you should follow the normal UNO 構成要素 threading 指導基準s to 確実にする that they do not 行き詰まる OpenOffice.org through 不適切な use of the UNO API.
小包 Descriptor DTD and 見本 XML
Each script must 含む/封じ込める a 小包-descriptor.xml とじ込み/提出する which 供給するs all the necessary metadata for the script. The DTD for the 小包-descriptor.xml follows<?xml 見解/翻訳/版="1.0" encoding="UTF-8"?> <!-- DTD for 小包 Meta data for use in the OpenOffice.org Scripting 枠組み 事業/計画(する) --> <!ELEMENT logicalname EMPTY> <!ELEMENT description (#PCDATA)> <!ELEMENT displayname EMPTY> <!ELEMENT 地元の (displayname?, description?)> <!ELEMENT functionname EMPTY> <!ELEMENT 支え(る) EMPTY> <!ELEMENT languagedepprops (支え(る)+)> <!ELEMENT とじ込み/提出する (支え(る)*)> <!ELEMENT fileset (とじ込み/提出する+)> <!ELEMENT script (地元の+, functionname, logicalname, languagedepprops*, fileset*)> <!ELEMENT 小包 (script+)> <!ATTLIST logicalname value CDATA #REQUIRED > <!ATTLIST displayname value CDATA #REQUIRED > <!ATTLIST 地元の lang CDATA #REQUIRED > <!ATTLIST functionname value CDATA #REQUIRED > <!ATTLIST logicalname value CDATA #REQUIRED > <!ATTLIST 支え(る) 指名する CDATA #REQUIRED value CDATA #REQUIRED > <!ATTLIST とじ込み/提出する 指名する CDATA #REQUIRED > <!ATTLIST fileset 指名する CDATA #IMPLIED > <!ATTLIST script language CDATA #REQUIRED > <!ATTLIST 小包 language CDATA #REQUIRED >The に引き続いて is an example of a 小包-descriptor.xml とじ込み/提出する that defines a script, 器具/実施するd in Java. The languagedepprops element is used to 延長する the JVM's classpath.
<?xml 見解/翻訳/版="1.0" encoding="UTF-8"?> <!--見本 Meta Data for use with the Scripting 枠組み 事業/計画(する) in OpenOffice.org --> <!DOCTYPE 小包 SYSTEM "小包.dtd"> <小包 language="Java"> <script language="Java"> <地元の lang="english"> <displayname value="Memory.usage"/> <description> 陳列する,発揮するs the memory 現在の memory usage </description> </地元の> <functionname value="memoryUtils.memoryUsage"/> <logicalname value="MemoryUtils.MemUsage"/> <languagedepprops> <支え(る) 指名する="classpath" value="/選ぶ/foo.jar:/usr/java/src.jar"/> </languagedepprops> <fileset> <とじ込み/提出する 指名する="mems.txt"> <支え(る) 指名する="type" value="資源"/> </とじ込み/提出する> </fileset> </script> </小包>最高の,を越す
Last 修正するd: Tue 損なう 12 11:40:28 GMT 2003