python macro - toggle enablevisible on a textfield

I’m using python macro to do things to a libreoffice writer file. And I’d like a possibility to toggle the EnableVisible flag of a TextField.

That means, toggle the little flag that you can use, when double clicking on that field, to make it visible or invisible.

So far I got this in my code :

import uno

def toggle_field(field_title):
    document = XSCRIPTCONTEXT.getDocument()
    textfields = document.getTextFields()
    enum = textfields.createEnumeration()
    while enum.hasMoreElements():
        tf = enum.nextElement()
        if tf.VariableName == field_title:
            visibility = tf.getPropertyValue('EnableVisible') #wrong
            tf.EnableVisible = not visibility                 #wrong
            tf.update()                                       #maybe right

This give me that

com.sun.star.beans.UnknownPropertyException: Unknown property: Enabled (Error during invoking function toggle_field in module (…)file.py (<class ‘lightproof_handler_pt_BR.com.sun.star.beans.UnknownPropertyException’>: Unknown property: EnableVisible

Also, if i comment te first wrong line, the second wrong line give me

com.sun.star.beans.UnknownPropertyException: Unknown property: Enabled (Error during invoking function toggle_field in module (…)file.py (<class ‘AttributeError’>: EnableVisible


update :

tf.IsFieldDisplayed = False

or

tf.setPropertyValue('IsFieldDisplayed', False)

is no longer an unknown property, but i got this error message :

com.sun.star.beans.UnknownPropertyException: IntrospectionAccessStatic_Impl::setPropertyValueByIndex(), property at index 13 is readonly (Error during invoking function toggle_field in module (…)file.py (<class ‘lightproof_handler_pt_BR.com.sun.star.beans.UnknownPropertyException’>: IntrospectionAccessStatic_Impl::setPropertyValueByIndex(), property at index 13 is readonly

what seems unfair, because it is not readonly in the doc, and BASIC can modify it (https://wiki.documentfoundation.org/images/b/b0/BH5009-Macros.pdf page 19)

@spoutnik16 Please do not post as wiki. It helps no one.

Also answered at python - libreoffice macro - toggle enablevisible on a textfield - Stack Overflow.

@jimk Thanks for the heads up.

@spoutnik16 Please note when you are cross posting! As was this case, you are actually wasting peoples time answering a question which has already been answered! What should one do when you ask for help again?

Hello @spoutnik16,

Try:

import uno

def toggle_field(field_title):
    document = XSCRIPTCONTEXT.getDocument()
    textfields = document.getTextFields()
    enum = textfields.createEnumeration()
    while enum.hasMoreElements():
        tf = enum.nextElement()
        impl_name = tf.getTextFieldMaster()
        if impl_name.Name == field_title:
            tf.IsVisible = False

If this answers your question please tick the :heavy_check_mark: (upper left area of answer). It helps others to know there was an accepted answer.

Please note: To actually “toggle” the field, once the field is found simply check current value of IsVisible and set to opposite value.