How to execute a python function before XSCRIPTCONTEXT is available?

I used to have BASIC macros attached to “Start Application” and “Open Document” but with python, XSCRIPTCONTEXT is “NoneType” at this point. I am hoping to get the desktop CurrentComponent but can’t figure out to access the interfaces in time. It seems like maybe this is possible using a socket? Even with the diagram I am confused on the process of LibreOffice booting up and when the python files are loaded


Probably need more on just what you are doing. Currently I use XSCRIPTCONTEXT on the Open Document event of the Base application to load a form without a problem. Maybe a bit of the code you are using or exactly what you are wanting to do or both. Also, where are your scripts located? Mine are currently embedded in the document.

First I have the global objects:

desktop = XSCRIPTCONTEXT.getDesktop()
oComponent = desktop.getCurrentComponent()

Now for the Custom Events:
“Start Application” is ignored completely so I do not get any errors.
Event (Start Application) → Assigned Action ($foo() )

def foo(*argv):
    print("Starting Application...)
    # Nothing printed to Console
    oController = oComponent.getCurrentController()
    oController.Frame.ContainerWindow.IsMaximized = True
    # No error and not maximized

“Open Document” runs and XSCRIPTCONTEXT is “NoneType”
Event (Open Document) → Assigned Action ($bar() )

def bar(*argv):
    oController = oComponent.getCurrentController()
    oController.Frame.Title = "Hello World"

Gets the error:

 (<class 'AttributeError'>: 'NoneType' object has no attribute 'getCurrentController' is in user space ~/.config/libreoffice/4/user/Scripts/python/

One item still not clear - what/where is Start Application event?

Never mind - found it. LO start screen. What necessitates starting from there? DB is not accessible from there. It continues to be the question of exactly what you are trying to accomplish.

@Ratslinger I did just notice that packaging the into the file so it is read in document space fixes this issue. I actually use a .fods file but it seems that the .ods version also has the same issue when the macro is in user space for me.

This probably can/should be another thread, but have you ever done interactive testing with a document space macro? Only way I can think of is:

  1. Edit the macro in user space
  2. Change all of the items from (user, python) to (document, python)
  3. Unzip the .ods
  4. Replace the py file
  5. zip the directory

I’m assuming there is a better way. Or does everyone just write perfect macros that rarely need to be changed?

I was just using “Start Application” to set the window title and set fullscreen (wanted this as soon as possible). So this can just be moved to “Open Document” and isn’t a big deal
EDIT: What I am trying to accomplish:
In “open document” I start logic that involves handling outside files to fill sheets/cell. I want to disable control forms, edit the LayoutManager, set up key handler & other listeners.

@UserRyan Sorry about the Base reference. Work most often there. Have better understanding of your question now. Already starting to review. Will have more info on packaging/testing when I put together an answer for you.


After much testing here are some of the things I find.

With your settings/code Start Application event seems to work from LO Start Up screen when you create a new Writer, Calc, etc document. Open Document event also works. HOWEVER, it only worked the first time and then the screen needed to be closed & reopened for it to work again.

Now the Open Document event will work consistently if you move the two global items into the routine. View Created event also seemed to work with that change.

As for debugging & packaging. In my current Python testing I use primarily two tools - MRI & APSO.

Mri (also a similar tool is XRay) lets you examine much of the internals of LO - Properties, Methods, Interfaces and Services. If you are unfamiliar with either tool, see this post → To learn LibreOffice Base are there introductions or tutorials?. Make sure to click ‘more’ at answer bottom to see MRI & XRay stuff.

APSO is a tool for organizing Python Scripts. It also has a feature to embed a script in a document making it easier than your zipping. It still requires you to delete & replace but is pretty quick. In light of that, most testing is from ~/.config/libreoffice/4/user/Scripts/python/ and when working can be moved into the document. APSO also is handy here at times since you can run the script from there. Some APSO links:

Did you know that you could also use APSO to Organize your Python scripts within LibreOffice?

Can I embed python script and integrate with basic? (solved)

Last one has brief document on how to use APSO to embed script in document.

Now one other item which I find handy and use often is this template → unostarter which also provides (among a multitude of other items) easy access to MRI or XRay from within your script.

Almost forgot. This page may be of interest → Designing & Developing Python Applications

Also just noted your comment on interactive testing. That would be your link in the question on ‘using a socket’. Have tried it and works. Not so good for Base where I do most things.

@Ratslinger adding the globals “Open Document” worked. Not entirely sure why though. The globals should be loaded before the function is available. Other globals (“constants” I have defined) work fine in “Open Document”.

I will look into the debugging tools; they look much better than my current methods. Thank you for always taking the time to write detailed answers and provide lots of other support links. It’s a huge help!


Certainly glad you have something which works. I am certainly not proficient in Python. There are some things yet, as you mention, which still raise the “I’m not sure why” question. Have answered some other Python questions which I and others still are not able to explain completely (mainly dealing with needing threading).

With LibreOffice 5.4+ I get an available & active Python environment inside LibreOffice launcher/desktop, that is without requiring to open a document. See how to run Python interpreter “Application Start” either with a Basic macro either with a Python macro.

However I suffer from similar symptoms when trying to grab the Desktop.

May I suggest you drop file into your INSTALL\Program directory and add the following line on top of “Start Application”-like Python macros:

from IDE_utils import XSCRIPTCONTEXT

Although I initially built for a different purpose, It does operate in this context. Therefore I’d be glad you let me know the outcome of your attempts.


More information in Python IDE Basics