Ask Your Question
0

How to execute a python function before XSCRIPTCONTEXT is available?

asked 2018-12-06 03:28:55 +0100

UserRyan gravatar image

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

edit retag flag offensive close merge delete

Comments

Hello,

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.

Ratslinger gravatar imageRatslinger ( 2018-12-06 04:03:20 +0100 )edit

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 ( macro.py$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 ( macro.py$bar() )

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

Gets the error:

 (<class 'AttributeError'>: 'NoneType' object has no attribute 'getCurrentController'

macro.py is in user space ~/.config/libreoffice/4/user/Scripts/python/macro.py

UserRyan gravatar imageUserRyan ( 2018-12-06 19:50:29 +0100 )edit

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 gravatar imageRatslinger ( 2018-12-06 19:56:57 +0100 )edit

@Ratslinger I did just notice that packaging the macro.py 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?

UserRyan gravatar imageUserRyan ( 2018-12-06 20:11:05 +0100 )edit

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 gravatar imageUserRyan ( 2018-12-06 20:15:51 +0100 )edit

@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.

Ratslinger gravatar imageRatslinger ( 2018-12-06 20:37:36 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2018-12-06 23:57:26 +0100

Ratslinger gravatar image

Hello,

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.

edit flag offensive delete link more

Comments

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 gravatar imageRatslinger ( 2018-12-07 00:01:19 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-12-06 03:28:55 +0100

Seen: 28 times

Last updated: Dec 06