Selecting text in a document throught UNO & postMessage

Hello,

I just started using collabora code. I can do some interactions through UNO and postMessage - for example selectAll.

I would like to select / find / search for speciffic text programatically, without openning any toolbars etc. Is it possible to do that in collabora office.

For the uno methods is there some kind of system on how to use them. I could not get DownSearch to work for example. Also using LibreOffice: Main Page as the reference. Is it possible to use any uno function? If so I am havin trouble definign the Arguments.

Is there a way to get feedback on postMessage calls.

Best,

Matej

hii @sirceljm

Good question this is one of those things that can be a bit confusing when you’re just starting with Collabora Online + UNO + postMessage.

  1. Can you select specific text programmatically?

Yes. Collabora Online exposes most of the LibreOffice UNO commands (the same you’d use in a desktop LibreOffice macro).
You can call them via postMessage into the editor frame. For example:

document.getElementById('iframe-id').contentWindow.postMessage({
    "MessageId": "uno",
    "args": {
        "command": "SearchDialog", // or .uno:Search
        "arguments": [
            { "name": "SearchItem.SearchString", "value": "MyText" },
            { "name": "SearchItem.Backward", "value": false },
            { "name": "SearchItem.Command", "value": 3 } // 3 = find all, 1 = find next
        ]
    }
}, "*");

This won’t pop up the toolbar — it just runs the UNO command with the given parameters.


  1. About DownSearch and others

Many of those names you see in the wiki (“DownSearch”, “UpSearch”, etc.) are old-style slot commands or macro helpers. In Collabora Online you normally stick to the .uno: commands (the desktop sends them on the wire too). For searching, the canonical command is .uno:ExecuteSearch.

Example:

postMessage({
  "MessageId": "uno",
  "args": {
    "command": ".uno:ExecuteSearch",
    "arguments": [
      { "name": "SearchItem.SearchString", "value": "hello" },
      { "name": "SearchItem.Backward", "value": false }
    ]
  }
}, "*");

  1. Is there a “system” for arguments?

Yes — arguments are the same property names the desktop UNO dispatcher uses.
You can discover them in a few ways:

  • Look at LibreOffice source code / wiki (search for .uno:ExecuteSearch, .uno:GoToStartOfDoc, etc.).
  • Open LibreOffice desktop with --record-macro, perform an action, then inspect the recorded Basic macro. The argument names you see there are what you pass in JSON.
  • In Collabora logs (coolwsd.log) you can see which .uno: commands are triggered when you click UI buttons.

4. Can you use any UNO command?

Mostly yes, but:

  • Collabora Online only implements a subset — most of the common editing/formatting/insert/search commands work.
  • Things that require a modal dialog (like Tools ▸ Options) usually don’t exist in Online.
  • For arguments, you must match exactly the expected property names and types (string, bool, int).

So: Yes, you can select specific text programmatically. Use .uno:ExecuteSearch (or .uno:FindAll) with the right arguments, not the old DownSearch.

Thanks
Darshan

Thanks!

I also discovered that I can call the search from the app object - like window.app.searchService.search() - probably not the most official way.

Another question, is it possible to capture events from user interaction in JS - for example user selects part of the text?