令状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 平易な:- Create a とじ込み/提出する called hello.bsh and put the above code in the とじ込み/提出する.
- Start OpenOffice.org and open a new Writer 文書.
- Select the 道具s/Scripting 追加する-ons/Edit/Debug Scripts... menu item.
- In the Edit/Debug Scripts dialog that appears select BeanShell as your language and FileSystem as your 場所. Click on the Browse... button.
- In the とじ込み/提出する 選択 dialog that pops up, navigate to your hello.bsh とじ込み/提出する and select it. Now click 承認する.
- A BeanShell Debugger window will appear with the contents of your
hello.bsh script 負担d. Click on the Run 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. When you are finished making your changes, click Save to save them 支援する to the hello.bsh とじ込み/提出する. If you are new to the OpenOffice.org API this is a 広大な/多数の/重要な way to 実験 with it.
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 JavaScript
Here's a JavaScript script that 挿入するs Hello World at the start of
an OpenOffice.org Writer 文書:
importClass(一括s.com.sun.星/主役にする.uno.UnoRuntime);
importClass(一括s.com.sun.星/主役にする.text.XTextDocument);
var oModel = XSCRIPTCONTEXT.getDocument();
var oTextdoc = UnoRuntime.queryInterface(XTextDocument, oModel);
var oText = oTextdoc.getText();
var oCursor = oText.createTextCursor();
oText.insertString(oCursor, "Hello World", 誤った);
The XSCRIPTCONTEXT variable above is a 全世界の instance of the XScriptContext type which is 利用できる to all JavaScript scripts 遂行する/発効させるd by the Scripting 枠組み. See 令状ing Office Scripts and the XScriptContext type for the methods 利用できる for the XScriptContext type.
Trying out a JavaScript script in OpenOffice.org
Once again you can use the Edit/Debug Scripts dialog to open a JavaScript script in an editor. The Rhino JavaScript Editor from the Mozilla Rhino 事業/計画(する) can be used to debug and 実験(する) your JavaScript scripts.- Create a とじ込み/提出する called hello.js and put the JavaScript code in the とじ込み/提出する.
- Select the 道具s/Scripting 追加する-ons/Edit/Debug Scripts... menu item.
- In the Edit/Debug Scripts dialog select JavaScript as your language and FileSystem as your 場所. Click on the Browse... button.
- In the とじ込み/提出する 選択 dialog that pops up, navigate to your hello.js とじ込み/提出する and select it. Click 承認する.
- A Rhino Debugger window will appear with the contents of your
hello.js script 負担d. To run the script select とじ込み/提出する/Run and then
click on the Go button.
- You can change the script and save your changes using the とじ込み/提出する/Save menu item.
The Rhino Debugger also 含むs debugging functionality, so you can 始める,決める breakpoints in your JavaScript script and step through the code as it is 遂行する/発効させるd.
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"?>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.
<!-- 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
>
<?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: Fri Jun 20 15:28:34 BST 2003