Page 1 of 1

Can't execute macro from command line

Posted: Sun Jul 27, 2008 8:22 pm
by cy6erGn0m
I wrote a simple script in BeanShell, add it to MyMacroses part, in Library1 with name Macro1 (Macro1.bsh).

I tried:

soffice "macro:///Library1.Macro1()"
soffice "macro://Library1.Macro1()"
soffice "macro:///Standard.Library1.Macro1()"
soffice "macro://Standard.Library1.Macro1()"

but nothing happens, there is no any messages in OpenOffice or console, script is not executed :(

What i doing wrong?

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 10:04 am
by Villeroy
Call Macro1 in Module1 of library Standard:

Code: Select all

soffice "macro://Standard.Module1.Macro1()"
Macros are in modules, modules are in libraries, libraries are in containers. Public libraries in "MyMacros" and "OOo Macros", document specific ones are in the respective document.

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 11:24 am
by cy6erGn0m
soffice "macro:///MyMacros.Library1.Module1.Macro1()"

nothing hapened..

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 11:31 am
by probe1
Call to soffice executable (as described by soffice -help from a shell) is

soffice [options] [documents...]

so you may do a

Code: Select all

soffice writer.odt "macro://Standard.Module1.Macro1()"
(you need to specify a document!)

Does this work?

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 11:40 am
by Villeroy
cy6erGn0m wrote:soffice "macro:///MyMacros.Library1.Module1.Macro1()"

nothing hapened..
"macro:///Library1.Module1.Macro1()"
Without document, the library is assumed to be in one of the public containers.
 Edit: In fact it seems to be impossible to call document macros, which is perfect for security reasons. 

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 1:29 pm
by cy6erGn0m
soffice test_macro.odt "macro://MyMacros.Library1.Module1.Macro2()"


Ok, it shows OO window, it shows alrert, i click Enable Macros .. and then nothing happens..

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 2:07 pm
by Villeroy
Leave out "MyMacros". There is no such container except in the GUI, where "My Macros" indicates libraries stored in the user's profile (with write access) whereas "OOo Macros" indicates libraries stored in the installation path (read-only with lots of usable example macros). Use macro:///library.module.routine only.
Correction to my previous statement: Both locations "My Macros" and "OOo Macros are merged into one library container as you may notice when you try to create a new library "Tools" in "My Macros". You'll get an error message about an already existing library "Tools". It's the one in "OOo Macros".

Code: Select all

soffice "macro:///Standard.Test.test_MessageBox"
calls routine "test_MessageBox" in module "Test", library "Standard".

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 5:25 pm
by hanya
BeanShell macros can be executed with this kind of url:

Code: Select all

soffice "vnd.sun.star.script:Library1.Macro1.bsh?language=BeanShell&location=user"

Re: Can't execute macro from command line

Posted: Mon Jul 28, 2008 8:38 pm
by Villeroy
hanya wrote:BeanShell macros can be executed with this kind of url:

Code: Select all

soffice "vnd.sun.star.script:Library1.Macro1.bsh?language=BeanShell&location=user"
Same with all other scripting languages including Basic:

Code: Select all

soffice "vnd.sun.star.script:Library1.Macro1?language=Basic&location=application"
Location=application reflects the merged container made of "My Macros" and OOo Macros".

Re: Can't execute macro from command line

Posted: Tue Jul 29, 2008 8:26 am
by cy6erGn0m
Yes,

Code: Select all

soffice "vnd.sun.star.script:Library1.Macro1.bsh?language=BeanShell&location=user"

This works good.. and only it. Any other ways does not.

But, how can i pass arguments to Macro1?

Re: Can't execute macro from command line

Posted: Tue Jul 29, 2008 12:24 pm
by Villeroy
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)

Re: Can't execute macro from command line

Posted: Tue Jul 29, 2008 2:37 pm
by cy6erGn0m

Code: Select all

soffice "macro:///Library3.Module1.test_Args(arg1,123,4.567,2000-12-31)"
This form does not work. OO ignores it and does not start macro and there is not any error messages. Works only second way. ("vnd.sun.star.script:Library1.Macro1.bsh?language=BeanShell&location=user"), but in second way i dont understand, how to pass arguments.

Re: Can't execute macro from command line

Posted: Tue Aug 12, 2008 3:45 pm
by cy6erGn0m
Is really no way to pass arguments to beanshell? I am in panic! Please, help me!

Re: Can't execute macro from command line

Posted: Tue Aug 12, 2008 4:03 pm
by Villeroy
cy6erGn0m wrote:Is really no way to pass arguments to beanshell? I am in panic! Please, help me!
Pass them to Basic using the old syntax (macro:///...) and call BeanShell from Basic.
Example (Basic>Python): http://www.oooforum.org/forum/viewtopic.phtml?t=59534
You may find better examples than my own one in that forum.

Re: Can't execute macro from command line

Posted: Tue Aug 19, 2008 12:38 pm
by cy6erGn0m
Yes, it helps. Thank you!