Despite the fact that the old syntax with protocol macro: works with Basic alone and Basic can not handle floating point numbers, this works for me:
Code: Select all
REM ***** BASIC *****
Sub test_Args(optional strArg$, optional intArg%,optional dblArg as double, optional dateArg as date)
if (vartype(strArg)=10)then strArg = "<Missing>"
if (vartype(intArg)=10)then intArg = 0
if (vartype(dblArg)=10)then dblArg = Pi()
if (vartype(dateArg)=10)then dateArg = now()
Print strArg , intArg, dblArg, cDateToISO(dateArg)
End Sub
called without arguments it yields default values <Missing> 0 3.14159265358979 20080729
Code: Select all
soffice "macro:///Library3.Module1.test_Args(arg1,123,4.567,2000-12-31)"
yields: arg1 123 4 2000-12-31
floating point 4 should be 4.567. Could be same issue as with cDbl("1.2345") and cSng("1.2345") or with comma-locale: cDbl("1,2345") and cSng("1,2345")
In any case it is easy to write Python or Java with the usual argument handlers, load or connect to a listening office instance (local or remote), instanciate the ServiceProvider and use the office-API "from outside".
A Python snippet which can connect to a running office by means of given arguments or using a default connection (socket, port 2002). Then it gets the active document and passes it over to the XrayTool which is an object inspector written in StarBasic.
Code: Select all
import uno, sys, getopt
class Office:
'''Frequently used methods in office context'''
def __init__(self, ctx=uno.getComponentContext()):
self.ctx = ctx
self.smgr = self.ctx.ServiceManager
def createUnoService(self, service):
return self.smgr.createInstance(service)
def getDesktop(self):
return self.smgr.createInstanceWithContext("com.sun.star.frame.Desktop",self.ctx)
def getCurrentComponent(self):
return self.getDesktop().getCurrentComponent()
def getCurrentFrame(self):
return self.getDesktop().getCurrentFrame()
def getCurrentController(self):
return self.getCurrentFrame().getController()
def callXray(self, obj=None):
"""Macro to call Basic XRay by Bernard Marcelly from Python.
If no object is given, the current document's selection is used"""
sURL = "vnd.sun.star.script:XRayTool._Main.Xray?language=Basic&location=application"
if not obj:
obj = self.getCurrentController().getSelection()
oMSPF = self.createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
oMSP = oMSPF.createScriptProvider('')
oScript = oMSP.getScript(sURL)
oScript.invoke((obj,), (), ())
oMSPF = self.createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
oMSP = oMSPF.createScriptProvider('')
oScript = oMSP.getScript(sURL)
x = oScript.invoke((thisComponent,), (), ())
def getConnection():
defaultConnect = "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext"
strConnect = defaultConnect
try:
opts, args = getopt.getopt(sys.argv[1:], 'a:', ['accept=',])
except getopt.GetoptError, e:
pass
else:
if opts:
optConnect = opts[0][1]
strConnect = 'uno:'+ optConnect +'StarOffice.ComponentContext'
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
try:
ctx = resolver.resolve(strConnect)
except NoConnectException, e:
raise e
except ConnectionSetupException, e:
raise e
except IllegalArgumentException, e:
raise e
else:
return ctx
ctx = getConnection()
o = Office(ctx)
frame = o.getCurrentFrame()
ThisComponent = o.getCurrentComponent()
o.callXray(ThisComponent)