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

**URL:** <https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843>\
**Category:** Development\
**Created:** [August 8, 2024, 10:00am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843 "2024-08-08T10:00:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [August 8, 2024, 10:00am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843/1 "2024-08-08T10:00:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![darshan](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/darshan/32/2230_2.png) [@darshan](https://forum.collaboraonline.com/u/darshan)\
**Post date:** [August 8, 2024, 11:18am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843/2 "2024-08-08T11:18:54Z")

</div>

@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 =\>

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/collaboraonline/original/2X/9/943cf6185777f1fbb5b6ee28afd2afba20260a5f.png)

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

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [August 9, 2024, 1:18am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843/3 "2024-08-09T01:18:14Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![darshan](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/darshan/32/2230_2.png) [@darshan](https://forum.collaboraonline.com/u/darshan)\
**Post date:** [August 9, 2024, 5:29am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843/4 "2024-08-09T05:29:41Z")

</div>

@gulixiang oh sorry i totally misinterpret the problem. As i did some browsing, i found this [`com.sun.star.bridge.UnoUrlResolver`](https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1bridge_1_1XUnoUrlResolver.html), 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 😉 ):

```auto
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 🙂

And also check here a java example given in this page: [LibreOffice Developer's Guide: Chapter 2 - Professional UNO - The Document Foundation Wiki](https://wiki.documentfoundation.org/Documentation/DevGuide/Professional_UNO)

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

Thanks,  
Darshan

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [August 9, 2024, 6:39am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843/5 "2024-08-09T06:39:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vmiklos](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/vmiklos/32/787_2.png) [@vmiklos](https://forum.collaboraonline.com/u/vmiklos)\
**Post date:** [August 9, 2024, 6:56am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843/6 "2024-08-09T06:56:32Z")

</div>

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:

> <https://github.com/libreoffice/core/blob/e7aa613e751c159881cb2448d5c7e08549f3fe49/comphelper/source/misc/dispatchcommand.cxx#L51-L59>

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

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [August 12, 2024, 1:41am UTC](https://forum.collaboraonline.com/t/if-a-python-code-executes-an-uno-command-is-there-a-way-to-capture-the-return-result/2843/7 "2024-08-12T01:41:55Z")

</div>

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
