* スクリプティングフレームワークメモ [#n42bad03] #contents ** コマンドライン引数 [#tf1fd21e] コマンドラインから 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" ** ドキュメントにスクリプトを保存する [#j8db162d] スクリプトプロバイダはマクロの実行方法は提供してくれますがマクロの編集方法は提供してくれません。ファイル外に保存されたマクロのファイルは Scripts/dir 以下にあるのでそこを調べれば編集できます。 また、現在の python スクリプトプロバイダはドキュメント内へのスクリプトファイル保存をサポートしていません。 ドキュメント内へのファイルのスクリプト書き込みは次のようにします。 #code(basic){{ sub WriteDocumentScripts ' to get internal identifier of the document model oTddcf = CreateUnoService( _ "com.sun.star.frame.TransientDocumentsDocumentContentFactory" ) oContent = oTddcf.createDocumentContent(ThisComponent) oId = oContent.getIdentifier() sId = oId.getContentIdentifier() ' vnd.sun.star.tdoc:/ID 'msgbox sId oSfa = CreateUnoService("com.sun.star.ucb.SimpleFileAccess") ' check Scripts dir 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 ' get python directory 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 & "/" ' write script file sScriptPath = sPythonDir & "hello.py" If oSfa.exists(sScriptPath) Then oSfa.kill(sScriptPath) End If 'oIn = oSfa.openFileWrite(sScriptPath) 'oIn.closeInput() ' write script file oPipe = CreateUnoService("com.sun.star.io.Pipe") oTextOut = CreateUnoService("com.sun.star.io.TextOutputStream") oTextOut.setOutputStream(oPipe) ' script slf = chr(10) sScript = "def hello():" & slf & _ " print('hello2')" oTextOut.writeString(sScript) oTextOut.flush() oTextOut.closeOutput() oSfa.writeFile(sScriptPath, oPipe) oPipe.closeInput() end sub }} |