-1

This is about automation of LO Base using Python macros.

Please see this question in the LO forum posed by me yesterday.

As you can see, from the link in my second post, it is trivial to open a form on the OpenDocument event, i.e. when the file is opened, if you use a VisualBasic macro.

However, attempts to open a form programmatically using Python macros always seem to lead to WrappedTargetException. e.g.:

def open_contacts_form(e):
    odb = e.Source
    container = odb.FormDocuments
    obj = container.getByHierarchicalName('kernel.contacts')
    obj.open() # causes the WrappedTargetException

But I can't find out how to access the initial (target) exception. I printed out (to a file) dir(e), and I don't see the attributes I expect to find from the API page for WrappedTargetException, such as TargetException, etc.

I have a suspicion unorthodox thread use could be causing the problem. But I don't understand how to dig into WrappedTargetException for greater enlightenment.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • Where to ask about LibreOffice. See accepted answer on meta here https://meta.stackexchange.com/a/364225/315464: "Is it about automating something with macros or writing code that uses the LibreOffice API? Consider Stack Overflow" – mike rodent Apr 21 '23 at 09:36
  • It sounds like you are relying on `dir`, but introspection tools such as MRI are the proper way to dig into UNO objects. Also, the code posted does not seem to be complete. `e` is an event, yet you speak of it as a caught exception? Please post a reproducible example. – Jim K Apr 21 '23 at 15:16
  • It is the line `obj.open()` which causes the WTE. At no point do I refer to `e` as an exception. Why not show what you mean with MRI and given an answer? – mike rodent Apr 21 '23 at 18:30
  • It seems that the question has the same answers as this one: https://stackoverflow.com/a/67215155/. Beyond that, based on the last comment, it sounds like you are investigating the event (calling it `oEvent` would be clearer than simply `e`) but you should actually catch and investigate the error itself if you want to see a WrappedTargetException. I don't understand your train of thought, but hopefully you will see clearer with MRI. It's probably best to close out or delete this question, as it seems too unclear to help anyone else. So I will vote to do so. – Jim K Apr 22 '23 at 18:28
  • It's really got nothing to do with that other question. Yes, I called this variable "e" because I just started learning about Python LO/UNO macros about 2 days ago, so I was copying someone else's badly written code and didn't even know this was an event at the time. But what's so mysterious or incomprehensible about wanting to extract and examine the target from a WrappedTargetException? I've since learnt how to open a form programmatically using a Python macro (controller.loadComponent), but I'd still like to dig into this WTE. I don't understand how that could be done with the MRI tool. – mike rodent Apr 22 '23 at 18:44
  • `to extract and examine the target from a WrappedTargetException` Okay, I'll ignore the parts of this conversation that I don't understand and simply answer with some pseudocode: `catch (WrappedTargetException err) { mri(err) }`. But surely you've already thought of that? Well, maybe you're unclear how to import WrappedTargetException, and that's understandable — the Transfer from Basic to Python link in my other answer should help with that. – Jim K Apr 22 '23 at 19:01
  • You overestimate my abilities! Not at all: I've just (thanks to you) found about about this MRI tool today, and haven't yet understood you can use it WITHIN the Python code: I thought it was just for examining static parts of the forms, etc. Do you know if, when using Python, you have to `import` any module to use this `mri` method? Or should it just be "there"? – mike rodent Apr 22 '23 at 19:04
  • Wow, sounds like I was off target in trying to help, but now I finally understand. I'll add some code in an answer. – Jim K Apr 22 '23 at 19:08

1 Answers1

0

To dig into WrappedTargetException, catch the exception and use an introspection tool such as MRI.

import uno
from com.sun.star.lang import WrappedTargetException

def open_contacts_form(oEvent):
    odb = oEvent.Source
    container = odb.FormDocuments
    obj = container.getByHierarchicalName('kernel.contacts')
    try:
        obj.open()
    except WrappedTargetException as err:
        mri(err)

def mri(myObject):
    """Display a dialog to analyze UNO objects.
    The MRI extension is required.
    """
    ctx = XSCRIPTCONTEXT.getComponentContext()
    mri_obj = ctx.ServiceManager.createInstanceWithContext(
        "mytools.Mri", ctx)
    mri_obj.inspect(myObject)

(I did not test this code.)

For further information, including the link for downloading MRI, see Need some explanation about interface in LibreOffice basic.

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • Thanks for showing me how to inspect an element in-code with MRI. Unfortunately this didn't work with the WTE, and I'm not that surprised. py.uno l. 513 (in my version) raises `AttributeError`. The line is `return setattr(self.__dict__["value"], name, value)`. It looks like it is difficult to get the target. I could try tweaking py.uno a bit but I have a feeling it wouldn't lead anywhere. – mike rodent Apr 23 '23 at 11:00
  • Another interesting thing however: if this code in `open_contacts_form` is executed in another method responding to an event on a form (e.g. "AfterRecordChange"), it executes without a problem. This might corroborate the suspicion of the "wrong thread" hypothesis: maybe when you open an .odb the thread being used is a "non-event" thread. Currently I know nothing whatsoever about how LO uses threads but will look into it in due course. – mike rodent Apr 23 '23 at 11:03
  • Oops, I mean "uno.py" of course! – mike rodent Apr 23 '23 at 12:19
  • Plus... the MRI tool doesn't appear to list the attributes (in the UNO sense, i.e. starting with a capital letter) of the object, which I'm surprised by. Can it be configured to do that? – mike rodent Apr 23 '23 at 15:35
  • If you're having trouble getting MRI to work with `err`, perhaps start with something easier such as `mri(oEvent)` on the first line of the function. Your posted code isn't easily reproducible, so there's not a good way for me to copy your setup to offer more specific advice. – Jim K Apr 24 '23 at 12:56
  • Thanks... I've understood the benefits of MRI for most UNO objects, and got it working fine. Is there a way of tweaking something so that it lists the UNO attributes though? (i.e. "Camel case": beginning with Capital letters). At the moment I see all the interfaces, available methods, etc., but not the UNO attributes. – mike rodent Apr 25 '23 at 08:17
  • Sounds like something is wrong, but I can't help further without specific details, including a simple, reproducible example, along with your OS and version of LO. An example in Writer or Calc would be better if it demonstrates the issue, as there is a lot of variation between setups in Base. A screenshot of the missing properties would be worthwhile to include. This would be best in a new question. – Jim K Apr 25 '23 at 18:22