Can't execute macro from command line

Creating a macro - Writing a Script - Using the API
Post Reply
cy6erGn0m
Posts: 9
Joined: Sun Jul 27, 2008 12:16 pm

Can't execute macro from command line

Post 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?
OOo 2.4.X on Mandriva + Solaris 10
User avatar
Villeroy
Volunteer
Posts: 30874
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Can't execute macro from command line

Post 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
cy6erGn0m
Posts: 9
Joined: Sun Jul 27, 2008 12:16 pm

Re: Can't execute macro from command line

Post by cy6erGn0m »

soffice "macro:///MyMacros.Library1.Module1.Macro1()"

nothing hapened..
OOo 2.4.X on Mandriva + Solaris 10
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand
Contact:

Re: Can't execute macro from command line

Post 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?
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
User avatar
Villeroy
Volunteer
Posts: 30874
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Can't execute macro from command line

Post 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. 
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
cy6erGn0m
Posts: 9
Joined: Sun Jul 27, 2008 12:16 pm

Re: Can't execute macro from command line

Post 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..
OOo 2.4.X on Mandriva + Solaris 10
User avatar
Villeroy
Volunteer
Posts: 30874
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Can't execute macro from command line

Post 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".
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Can't execute macro from command line

Post 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"
User avatar
Villeroy
Volunteer
Posts: 30874
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Can't execute macro from command line

Post 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".
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
cy6erGn0m
Posts: 9
Joined: Sun Jul 27, 2008 12:16 pm

Re: Can't execute macro from command line

Post 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?
OOo 2.4.X on Mandriva + Solaris 10
User avatar
Villeroy
Volunteer
Posts: 30874
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Can't execute macro from command line

Post 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)
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
cy6erGn0m
Posts: 9
Joined: Sun Jul 27, 2008 12:16 pm

Re: Can't execute macro from command line

Post 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.
OOo 2.4.X on Mandriva + Solaris 10
cy6erGn0m
Posts: 9
Joined: Sun Jul 27, 2008 12:16 pm

Re: Can't execute macro from command line

Post by cy6erGn0m »

Is really no way to pass arguments to beanshell? I am in panic! Please, help me!
OOo 2.4.X on Mandriva + Solaris 10
User avatar
Villeroy
Volunteer
Posts: 30874
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Can't execute macro from command line

Post 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
cy6erGn0m
Posts: 9
Joined: Sun Jul 27, 2008 12:16 pm

Re: Can't execute macro from command line

Post by cy6erGn0m »

Yes, it helps. Thank you!
OOo 2.4.X on Mandriva + Solaris 10
Post Reply