スクリプティングフレームワークメモ 
コマンドライン引数 
コマンドラインから soffice を起動してマクロを実行することができる。そのときにマクロに対して引数を与えるには…?
OOo Basic のマクロでは次のようにして引数を与えて実行できる。
soffice "macro:///Standard.Module1.Test(Arg1,Arg2)"
スクリプティングフレームワークが実装されてからはマクロの実行 URL が変更されて次のようなものが利用されている。
vnd.sun.star.script:Library1.Macro1.bsh?language=BeanShell&location=user
しかし、この形式の URL では独自の引数を渡すことができない。&arg1=hoge などとしてもスクリプトプロバイダ付近で削除されてしまうらしくマクロまで渡されない。
macro:/// の形式で実行できる Basic のマクロから実行したいスクリプティングフレームワークスクリプトのコードを実行することでなんとか引数を渡すことができる。
単純にマクロを引数を渡して invoke します。
以下のような形式の URI を指定すると Java、BeanShell、JavaScript で書かれてドキュメント内にあるマクロが実行できます。
~/opt/ooo-dev3/program/soffice ~/Desktop/js.odt \
"vnd.sun.star.script:Library1.Macro1.js?language=JavaScript&location=vnd.sun.star.tdoc:/1"
ドキュメントにスクリプトを保存する 
スクリプトプロバイダはマクロの実行方法は提供してくれますがマクロの編集方法は提供してくれません。ファイル外に保存されたマクロのファイルは Scripts/dir 以下にあるのでそこを調べれば編集できます。
また、現在の python スクリプトプロバイダはドキュメント内へのスクリプトファイル保存をサポートしていません。
ドキュメント内へのファイルのスクリプト書き込みは次のようにします。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| | sub WriteDocumentScripts
oTddcf = CreateUnoService( _
"com.sun.star.frame.TransientDocumentsDocumentContentFactory" )
oContent = oTddcf.createDocumentContent(ThisComponent)
oId = oContent.getIdentifier()
sId = oId.getContentIdentifier() oSfa = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
sScriptsDir = sId & "/Scripts"
If NOT oSfa.exists(sScriptsDir) AND NOT oSfa.isFolder(sScriptsDir) Then
oSfa.createFolder(sScriptsDir)
End If
If NOT oSfa.exists(sScriptsDir) Then
msgbox "failed to create Scripts directory."
Exit Sub
End If
sPythonDir = sScriptsDir & "/python"
If NOT oSfa.exists(sPythonDir) AND NOT oSfa.isFolder(sPythonDir) Then
oSfa.createFolder(sPythonDir)
End If
If NOT oSfa.exists(sPythonDir) Then
msgbox "failed to create python directory."
Exit Sub
End If
sPythonDir = sPythonDir & "/"
sScriptPath = sPythonDir & "hello.py"
If oSfa.exists(sScriptPath) Then
oSfa.kill(sScriptPath)
End If
oPipe = CreateUnoService("com.sun.star.io.Pipe")
oTextOut = CreateUnoService("com.sun.star.io.TextOutputStream")
oTextOut.setOutputStream(oPipe)
slf = chr(10)
sScript = "def hello():" & slf & _
" print('hello2')"
oTextOut.writeString(sScript)
oTextOut.flush()
oTextOut.closeOutput()
oSfa.writeFile(sScriptPath, oPipe)
oPipe.closeInput()
end sub
|