Share global variables in Python macros

Hello. I’am testing work with global variables, which I can use across Python macros.
I’ve found the script, but it does’t work, I think there is wrong methods name (maybe old version)

def _set_global_var(doc, name, value):

    props = doc.getDocumentProperties().getUserDefinedProperties()
    props.setPropertyValue(name, value)


def _get_global_var(doc, name, default=None):

    props = doc.getDocumentProperties().getUserDefinedProperties()
    return props.getPropertyValue(name)

def run(*_):
    calc_doc = XSCRIPTCONTEXT.getDocument()
    _set_global_var(calc_doc, 'param1', 'value1')

What you are trying to save is part of the document. In the UI, you find the user defined properties under menu:File>Properties… tab “Custom”.
If there is no property “param1”, you can’t set that property. You have to create it first.

1 Like

I need to store user session parametrs for example auth token, user name… Can I create session params with python macro?