[Python] Get current document from keyevent

Creating a macro - Writing a Script - Using the API

[Python] Get current document from keyevent

Postby Bastien » Fri Apr 16, 2021 4:12 pm

Hi everybody,

I'm looking for the current document from a key event.

Code: Select all   Expand viewCollapse view
class KeyHandler(unohelper.Base, XKeyHandler):

   def disposing(self, ev):
        pass

    def keyPressed(self, ev):

       if ev.KeyCode == PAUSE:

            # here I need o access the current document to manipulate text
           
        else:
            return False

        return True

    def keyReleased(self, ev):
        return False


By instrospecting 'ev' I get a 'source' but I can't see how to reach my current document.
LibreOffice 6.2.3.2 on Ubuntu 19.04
Bastien
 
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Get current document from keyevent

Postby Villeroy » Fri Apr 16, 2021 4:36 pm

and what kind of animal is Source? Does it have an ImplementationName or something?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
 
Posts: 30157
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Python] Get current document from keyevent

Postby JeJe » Fri Apr 16, 2021 4:47 pm

The source is the Window which receives the key event... you could use the accessible context to go up the chain of accessible parent windows until you get the titledbordertext for the frame and then enumerate all the frames to find the document.

But as this should be the same as ThisComponent why not use that?
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1649
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Python] Get current document from keyevent

Postby Bastien » Fri Apr 16, 2021 4:48 pm

I'd say it's something like ".uno.XInterface" ? Which has :

Code: Select all   Expand viewCollapse view
(Name)             (Value Type)                       (Value)      (Info.)   (Attr.)    (Handle) 
AccessibleContext  .accessibility.XAccessibleContext  -INTERFACE-  Pseud     Read_Only       
Background         long                                            Pseud     WriteOnly       
ControlFont        .awt.FontDescriptor                             Pseud     WriteOnly       
DesignMode         boolean                            False        Pseud                     
Enable             boolean                                         Pseud     WriteOnly       
FloatingMode       boolean                                         Pseud     WriteOnly       
FontDescriptors    [].awt.FontDescriptor              -Sequence-   Pseud     Read_Only       
Foreground         long                                            Pseud     WriteOnly       
Graphics           .awt.XGraphics                     -void-       Pseud     Read_Only       
Group              [].awt.XWindow                                  Pseud     WriteOnly       
ImplementationId   []byte                             -SEQUENCE-   Pseud     Read_Only       
Info               .awt.DeviceInfo                    -STRUCT-     Pseud     Read_Only       
MinimumSize        .awt.Size                          -STRUCT-     Pseud     Read_Only       
OutputSize         .awt.Size                          -STRUCT-     Pseud                     
Pointer            .awt.XPointer                                   Pseud     WriteOnly       
PosSize            .awt.Rectangle                     -STRUCT-     Pseud     Read_Only       
PreferredSize      .awt.Size                          -STRUCT-     Pseud     Read_Only       
Properties         [].beans.Property                  -Sequence-   Pseud     Read_Only       
Size               .awt.Size                          -STRUCT-     Pseud     Read_Only       
StyleSettings      .awt.XStyleSettings                -INTERFACE-  Attr.     Read_Only       
Toolkit            .awt.XToolkit                      -INTERFACE-  Pseud     Read_Only       
Types              []type                             -Sequence-   Pseud     Read_Only       
Visible            boolean                            True         Pseud                     
Windows            [].awt.XWindow                     -Sequence-   Pseud     Read_Only       



Is it relevant?
LibreOffice 6.2.3.2 on Ubuntu 19.04
Bastien
 
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Get current document from keyevent

Postby JeJe » Fri Apr 16, 2021 4:52 pm

As I said the first item in that list... AccessibleContext... use that... or don't because ThisComponent does the job...
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1649
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Python] Get current document from keyevent

Postby Bastien » Fri Apr 16, 2021 5:00 pm

JeJe wrote:As I said the first item in that list... AccessibleContext... use that... or don't because ThisComponent does the job...

I dont have ThisComponent in this imported module.
LibreOffice 6.2.3.2 on Ubuntu 19.04
Bastien
 
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Get current document from keyevent

Postby RoryOF » Fri Apr 16, 2021 5:08 pm

Then you need to insert code something like
Code: Select all   Expand viewCollapse view
ctx = XSCRIPTCONTEXT.getComponentContext()
smgr = ctx.getServiceManager()
Apache OpenOffice 4.1.10 on Xubuntu 20.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32965
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Python] Get current document from keyevent

Postby Bastien » Fri Apr 16, 2021 5:13 pm

XSCRIPTCONTEXT is not available in an imported module.

The solution I found (to avoid launching multiple time the same keyhandler attachment) is to save the component in a class property.

Code: Select all   Expand viewCollapse view
class KeyHandler(unohelper.Base, XKeyHandler, metaclass=Singleton):

    components = set()

    def __init__(self, ctx, component):
        self.ctx = ctx
        KeyHandler.components.add(component)

   (...)

    @property
    def mission(self):
        self.smgr = self.ctx.ServiceManager
        self.desktop = self.smgr.createInstanceWithContext(
            "com.sun.star.frame.Desktop", self.ctx)
        return self.desktop.getCurrentComponent()




Then I can work on the current component (called here 'mission) using a property. I'm pretty sure it's not the best solution. It should be possible to get the current component straight from the KeyEvent (which was my initial question).
LibreOffice 6.2.3.2 on Ubuntu 19.04
Bastien
 
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Get current document from keyevent

Postby JeJe » Fri Apr 16, 2021 6:17 pm

In Basic, with a slightly different route to the titledbordertext, something like this.
Code: Select all   Expand viewCollapse view



title= source.gettoolkit.getactivetopwindow.accessiblecontext.titledbordertext

frames = stardesktop.getframes
for i = 0 to frames.count -1
if frames(i).title = title then
component =frames(i).getcontroller.model
exit for
end if
next

Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1649
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Python] Get current document from keyevent

Postby Villeroy » Fri Apr 16, 2021 7:44 pm

If the calling object is a window object, you have attached the listener to that object. Is there any controller availlable? A controller also has XKeyHandler and from there you get the document via obj.getModel()
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
 
Posts: 30157
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 6 guests