Page 1 of 1

[Solved] List of Modules and Macros

Posted: Fri Aug 03, 2012 12:52 am
by saleem145
Hello,

From python can I determine the list of basic modules that are part of the spreadsheet. Within each module can I can determine what macros are available??

Given a module and macro name (implemented in basic) can I call the macro from python.

Thanks,

Saleem

Re: List of Modules and Macros

Posted: Fri Aug 03, 2012 12:59 am
by saleem145
spreadsheet.BasicLibraries will give me the list of modules. Not sure how to get a list of macros and how to call them from python.

Re: List of Modules and Macros

Posted: Fri Aug 03, 2012 3:58 pm
by hanya
Basically both listing macros and executing one of them can be done through script provider which can be accessed on document model. But listing basic macro functions can not be done through PyUNO due to the problem on the component implementation of the node for basic method and pyuno in Python on AOO 3.4.

Re: List of Modules and Macros

Posted: Fri Aug 03, 2012 7:16 pm
by hanya
hanya wrote:But listing basic macro functions can not be done through PyUNO due to the problem on the component implementation of the node for basic method and pyuno in Python on AOO 3.4.
Issue: https://issues.apache.org/ooo/show_bug.cgi?id=120458

Re: List of Modules and Macros

Posted: Fri Aug 03, 2012 7:32 pm
by hanya
We need to use the workaround due to the problem above.

Code: Select all

def _get_document_uri(ctx, doc):
    """ Get document transient URI. """
    tddc = ctx.getServiceManager().createInstanceWithContext(
    "com.sun.star.frame.TransientDocumentsDocumentContentFactory", ctx)
    try:
        content = tddc.createDocumentContent(doc)
        id = content.getIdentifier()
        return id.getContentIdentifier()
    except:
        pass


def get_document_basic_macros_provider(ctx, doc):
    uri = _get_document_uri(ctx, doc)
    return create(ctx, "com.sun.star.script.provider.ScriptProviderForBasic", (uri,))


def create(ctx, name, args=None):
    smgr = ctx.getServiceManager()
    if args is None:
        return smgr.createInstanceWithContext(name, ctx)
    else:
        return smgr.createInstanceWithArgumentsAndContext(name, args, ctx)

def get_basic_macros_provider(ctx, context):
    return create(ctx, "com.sun.star.script.provider.ScriptProviderForBasic", (context,))


def get_list_of_macros(ctx, sp):
    introspection = create(ctx, "com.sun.star.beans.Introspection")
    macros = []
    for lib_node in sp.getChildNodes():
        #print(lib_node.getName())
        for module_node in lib_node.getChildNodes():
            #print(module_node.getName())
            for method_node in module_node.getChildNodes():
                d = None
                inspected = introspection.inspect(method_node)
                idl_getName = inspected.getMethod("getName", -1)
                idl_getPropertyValue = inspected.getMethod("getPropertyValue", -1)
                name, d = idl_getName.invoke(method_node, ())
                uri, d = idl_getPropertyValue.invoke(method_node, ("URI",))
                macros.append((name, uri))
    return macros

def test(*args):
    ctx = XSCRIPTCONTEXT.getComponentContext()
    doc = XSCRIPTCONTEXT.getDocument()
    
    sp = get_document_basic_macros_provider(ctx, doc)
    #sp = get_basic_macros_provider(ctx, "user")
    macros = get_list_of_macros(ctx, sp)
    for name, uri in macros:
        print(name)
        print(uri)
Once you have URI to a macro, you can call it like described in http://user.services.openoffice.org/en/ ... pt#p242949

Re: List of Modules and Macros

Posted: Sun Aug 05, 2012 2:01 am
by saleem145
Hello,

I have been able to obtain the list of macros. But am having difficulty invoking them from Python...

Code: Select all

        sp = ctx.ServiceManager.createInstanceWithArgumentsAndContext("com.sun.star.script.provider.ScriptProviderForBasic", (spreadsheet,), self.ctx)

        try:
            macro_exists = True
            script = sp.getScript("vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=document")
        except:
            macro_exists = False

        if macro_exists:
            script.Invoke([],[],[])
It finds the macro but fails on the call to script.Invoke. Any help is appreciated as always. Thanks,

Saleem

Re: List of Modules and Macros

Posted: Sun Aug 05, 2012 7:09 am
by hanya

Code: Select all

script.invoke((), (), ())
See: http://www.openoffice.org/api/docs/comm ... cript.html
And PyUNO bridge does not allow to take instance of list as method argument, use tuple.