[Solved] How to use GetCaretPosition()?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Alectrona
Posts: 17
Joined: Tue Mar 24, 2020 7:04 pm

[Solved] How to use GetCaretPosition()?

Post by Alectrona »

https://www.openoffice.org/api/docs/com ... eText.html

I am trying

Code: Select all

pos = GetCaretPosition()
msgbox(pos)
But the variable pos is blank.
Last edited by Alectrona on Wed Mar 25, 2020 2:23 am, edited 1 time in total.
LibreOffice 6.2.8.2 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to use GetCaretPosition()?

Post by JeJe »

I suspect you want something different from what you're asking. If what you really want is the viewcursor, the information is here:

https://wiki.openoffice.org/wiki/Writer/API/View_cursor

*

As for the accessiblecontext, which is what you're asking about. I suggest getting Mri if you haven't got it already. Then take an object like the current componentwindow and explore its accessiblecontext by typing

Code: Select all

mri thiscomponent.currentcontroller.componentwindow.accessiblecontext

You need to go through the child windows till you find the one which supports XAccessibleText and explore that. The information is read only.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Alectrona
Posts: 17
Joined: Tue Mar 24, 2020 7:04 pm

Re: How to use GetCaretPosition()?

Post by Alectrona »

JeJe wrote:I suspect you want something different from what you're asking. If what you really want is the viewcursor, the information is here:

https://wiki.openoffice.org/wiki/Writer/API/View_cursor

*

I don't think so, I need to use GetCaretPosition(), and then use the position it returned to check which character is next to my caret, with GetCharacter(nIndex)

As for the accessiblecontext, which is what you're asking about. I suggest getting Mri if you haven't got it already. Then take an object like the current componentwindow and explore its accessiblecontext by typing

Code: Select all

mri thiscomponent.currentcontroller.componentwindow.accessiblecontext

You need to go through the child windows till you find the one which supports XAccessibleText and explore that. The information is read only.
Thanks, I am going to download mri
Last edited by Alectrona on Tue Mar 24, 2020 10:03 pm, edited 1 time in total.
LibreOffice 6.2.8.2 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to use GetCaretPosition()?

Post by JeJe »

this gets that information in Writer:

Code: Select all

vc=thiscomponent.currentcontroller.viewcursor
tc = vc.text.createtextcursorbyrange(vc)
tc.goright(1,true)
msgbox tc.string
tc.collapsetostart
tc.goleft(1,true)
msgbox tc.string
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Alectrona
Posts: 17
Joined: Tue Mar 24, 2020 7:04 pm

Re: How to use GetCaretPosition()?

Post by Alectrona »

ok, interesting.
but how could I check if the character next to the caret is a character-anchored frame?
I was thinking about using getCharacterAttributes with XAccessibleText. I am not sure it is possible with your idea

Image

My goal is to move the text cursor to my character-anchored frame when I'm next to it.
So my idea is to trigger my macro on Right and Left keys event, and check for the character next to the caret. If it is a frame, we jump to its textrange.
LibreOffice 6.2.8.2 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to use GetCaretPosition()?

Post by JeJe »

It doesn't say on that page you linked to that getCharacterAttributes returns that information. I could be wrong but I presume it returns these:

https://www.openoffice.org/api/docs/com ... rties.html

But you can find out when you explore.

The viewcursor doesn't appear to give the information about the objects that are anchored to it either. It may be that that information can only be found from the object itself.

If that's the case the only way might be to create a list of textranges for your anchors and test the new cursor textrange against them every time you move the text caret/cursor. That might or might not be unacceptably slow.

Edit: slow and/or crash prone.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Alectrona
Posts: 17
Joined: Tue Mar 24, 2020 7:04 pm

Re: How to use GetCaretPosition()?

Post by Alectrona »

JeJe wrote:It doesn't say on that page you linked to that getCharacterAttributes returns that information. I could be wrong but I presume it returns these:

https://www.openoffice.org/api/docs/com ... rties.html

But you can find out when you explore.
Hmm. That's sad
The viewcursor doesn't appear to give the information about the objects that are anchored to it either. It may be that that information can only be found from the object itself.

If that's the case the only way might be to create a list of textranges for your anchors and test the new cursor textrange against them every time you move the text caret/cursor. That might or might not be unacceptably slow.

Edit: slow and/or crash prone.
Well I can indeed compare the distance between my text cursor and every frame, but only if the character returned by your code is null. Still may me slow when looping with multilple frames.
LibreOffice 6.2.8.2 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to use GetCaretPosition()?

Post by JeJe »

https://www.openoffice.org/api/docs/com ... mpare.html

This will return 0 if the text caret is in the right place

Code: Select all


anchstart = thiscomponent.textframes.getbyindex(0).getanchor.getstart
vcend = thiscomponent.currentcontroller.viewcursor.getend 
msgbox thiscomponent.text.compareRegionStarts(anchstart,vcend)

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Alectrona
Posts: 17
Joined: Tue Mar 24, 2020 7:04 pm

Re: How to use GetCaretPosition()?

Post by Alectrona »

Thank you very much for your precious help.

At the moment the speed of this code is almost instant, even when I have multiple frames

Code: Select all

sub left
oDoc = thiscomponent
oVC = oDoc.getCurrentController.getViewCursor
vc=oDoc.currentcontroller.viewcursor
tc = vc.text.createtextcursorbyrange(vc)
tc.goright(1,true)
tc.collapsetostart
tc.goleft(1,true)
mychar = tc.string()
if(mychar = "") then

			vcend = oDoc.currentcontroller.viewcursor.getend
			myFrames = oDoc.textframes
			count = myFrames .Count
			 For i = 0 to count - 1			
 				anchstart = myFrames.getbyindex(i).getanchor.getstart
   				If (oDoc.text.compareRegionStarts(anchstart,vcend)= 1) then
      				target = myFrames.getbyindex(i)
   				 end If
			 next i
			oVC.gotoRange(shup, false)
			oVC.goRight(1,false)
else
oVC.goLeft(1,false) 'We want the caret to move left since we unassigned the move left event from left button.
end if
end sub
LibreOffice 6.2.8.2 on Windows 10
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: How to use GetCaretPosition()?

Post by hubert lambert »

Hi,

If I understand correctly your need, here is a function that returns any object anchored right after the view cursor position:

Code: Select all

sub main
    doc = thiscomponent
    ctrl = doc.CurrentCOntroller
    vc = ctrl.ViewCursor
    anchor = following_anchor(vc)
    if not isnull(anchor) then
        mri anchor
    end if
end sub

function following_anchor(vc)
    following_anchor = Nothing
    T = vc.Text
    c = T.createTextCursorByRange(vc.End)
    c.goRight(1, True)
    e = c.createContentEnumeration("com.sun.star.text.TextContent")
    on error resume next
    following_anchor = e.nextElement()
end function
Regards
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
User avatar
robleyd
Moderator
Posts: 5081
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: How to use GetCaretPosition()?

Post by robleyd »

Note: also asked on AskLibreOffice
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
Alectrona
Posts: 17
Joined: Tue Mar 24, 2020 7:04 pm

Re: How to use GetCaretPosition()?

Post by Alectrona »

However, this line bugs when I have frames inside another frame

Code: Select all

compa = oDoc.text.compareRegionStarts(anchstart,vcend)
Is there a way to skip this loop step if it fails? or is there a way to test only the distance between the text cursor, and the frames within the current textframe? (and not with all the frames within the document)[/strike]
Last edited by Alectrona on Wed Mar 25, 2020 1:11 am, edited 1 time in total.
LibreOffice 6.2.8.2 on Windows 10
Alectrona
Posts: 17
Joined: Tue Mar 24, 2020 7:04 pm

Re: How to use GetCaretPosition()?

Post by Alectrona »

hubert lambert wrote:Hi,

If I understand correctly your need, here is a function that returns any object anchored right after the view cursor position:

Code: Select all

sub main
    doc = thiscomponent
    ctrl = doc.CurrentCOntroller
    vc = ctrl.ViewCursor
    anchor = following_anchor(vc)
    if not isnull(anchor) then
        mri anchor
    end if
end sub

function following_anchor(vc)
    following_anchor = Nothing
    T = vc.Text
    c = T.createTextCursorByRange(vc.End)
    c.goRight(1, True)
    e = c.createContentEnumeration("com.sun.star.text.TextContent")
    on error resume next
    following_anchor = e.nextElement()
end function
Regards
And then I can get the frame name with

Code: Select all

anchor.getName()
This looks way more efficient when having multiple frames. Brilliant.

Thanks everyone
LibreOffice 6.2.8.2 on Windows 10
Post Reply