If a Python code executes an UNO COMMAND, is there a way to capture the return result?

dispatch.executeDispatch(frame, url, ‘’, 0, args) The returned result doesn’t seem to have any practical use. Don’t I need to add a listener or something similar to get the actual return result?

@gulixiang have you checked INCOMING response in console ?

In simple local setup we have see the INCOMING response of every Uno command dispatch in console like this =>

Also, can you give me more info about what is the scenario here ? What exact thing you want to achieve with the returned result ?

Thanks,
Darshan

@darshan Thank you for your reply. I know that the console can be viewed, but that’s not what I want. I wrote a Python script that calls a UNO COMMAND, and I want to get the same return result in the Python script as I would see in the console.

@gulixiang oh sorry i totally misinterpret the problem. As i did some browsing, i found this com.sun.star.bridge.UnoUrlResolver, it may help you to get the response of a uno command.

For example ( Just a sudo code, I am not much familiar with python :wink: ):

import uno

def run_uno_command():
    local_context = uno.getComponentContext()
    resolver = local_context.ServiceManager.createInstanceWithContext(
        "com.sun.star.bridge.UnoUrlResolver", local_context)

    ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
    smgr = ctx.ServiceManager

    desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

    document = desktop.getCurrentComponent()

    dispatcher = smgr.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)

    command = ".uno:WordCount"

    args = ()

    result = dispatcher.executeDispatch(document.getCurrentController(), command, "", 0, args)

    return result

output = run_uno_command()
print(output)


This may not be an exact thing, but let me know if it helps :slight_smile:

And also check here a java example given in this page: LibreOffice Developer's Guide: Chapter 2 - Professional UNO - The Document Foundation Wiki

Search for term: com.sun.star.bridge.UnoUrlResolver

Thanks,
Darshan

@darshan Hi after testing, it still doesn’t work. The returned result is None.

Hrm, that resolve() would be for connecting to a remote soffice process from pyuno, I’m not sure that’s relevant for the COOL python scripting, which is meant to be in-process.

I’m also not sure if .uno:WordCount is meant to return a value.

If it does, what you probably want is to dispatch with an XDispatchResultListener. See here for a C++ example:

I.e. if you want to get feedback on when the command the command is dispatched, you want dispatchWithNotification() and not dispatch(), I guess.

1 Like

Hi @vmiklos Yes, this is what I want. The awkward thing is that I couldn’t find the relevant API on the Python side to implement it