On 2010-04-06 10:22, Thomas Krumbein wrote:
Hey,
in one of my basic-macros I have to call a python script and work
afterwards with the results - i.e. the content of a created textfile.
At the moment I do this using the shell service. But there are two
things, which I do not like:
- when calling the shellservice a console-window will pop in front for a
very short moment.
- the Python script create a text-file, then I read the text file.
Because the Basic-script do not stop on the shellservice call, I have to
use a wait() statemet to be sure, the file is created.
Is there - maybe - another way to call a python script in basic? I have
to call the python script with parameters and - if possible - to get a
flag or something back, so that I now, python script is done.
Hi Thomas.
Try this. Create a python file called test.py containing
def testMacro():
return "Testing from Python"
and save it in your user configuration under .../scripts/python/. On my
Windows XP machine I have saved test.py in "C:\Documents and
Settings\<user name>\Application
Data\OpenOffice.org\3\user\Scripts\python\" - I had to create the
\python\ directory myself; \Scripts\ was already there.
Check that the Python macro is seen by OOo: Open "Tools -> Macros ->
Organize Macros -> Python..." and check that you now have a library
called "test" with a macro in it called "testMacro".
From a StarBasic macro you can now call the macro by-URL with
Sub Main
Dim MasterScriptProviderFactory as Object
Dim MasterScriptProvider as Object
Dim Script as Object
Dim Url as String
Dim PythonResult as String
Url =
"vnd.sun.star.script:test.py$testMacro?language=Python&location=user"
MasterScriptProviderFactory =
createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
MasterScriptProvider =
MasterScriptProviderFactory.createScriptProvider("")
Script = MasterScriptProvider.getScript(url)
PythonResult = Script.invoke(Array(),Array(),Array())
MsgBox PythonResult
End Sub
There might be a way to load the Python library directly instead of
by-URL so you could call the macro in a more natural way, but I didn't
figure out how to do it. Perhaps others can help.
Cheers
-- Jan Holst Jensen
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]