[Solved] Cursor Paragraph in AOO-Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

[Solved] Cursor Paragraph in AOO-Basic

Post by DavidHMcCracken »

I have created a Basic library under Libre that I'm trying to make available to users of Open. My code uses the TextParagraph property of view cursors, which doesn't exist in OO. OO-Basic text cursors also do not include this property and neither view cursors nor text cursors provides a method to get the paragraph. I can enumerate paragraphs until finding one that contains the text range of the view cursor but this is an extremely retrograde solution compared to the simple property afforded by LO. Does OO have anything comparable?
OO 4.1.8 on W7/10. LO 6.1.5.2 on W10. LO 6.0.7.3 on Ubuntu-Mate 18.04
Last edited by Hagar Delest on Tue Dec 01, 2020 11:03 pm, edited 1 time in total.
Reason: tagged solved.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
User avatar
Zizi64
Volunteer
Posts: 11363
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Cursor Paragraph in OO-Basic

Post by Zizi64 »

OO-Basic text cursors also do not include this property and neither view cursors nor text cursors provides a method to get the paragraph.
The StarBasic of the Apache OpenOffice and the StarBasic of the LibreOffice almost identical. They has same commands. The differences are in the API-s of the AOO and LO. The two API-s has many differences for now.

API : Application Programming Interface; a huge library of programming objects, methods and properties of the office suite. You can call these methods, and can control the properties from many programming environments and programming languages.


Do you use a third party object inspection tool like the XrayTool or the MRI? I suggest you to use one of them. Then you will able to list all of the existing properties of the programming objects of the AOO or LO.
Last edited by robleyd on Sat Nov 28, 2020 10:06 am, edited 1 time in total.
Reason: Typo Yeay insted of Xray
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Cursor Paragraph in OO-Basic

Post by Lupp »

The TextParagraph property only was introduced with LibreOffice 6.0.
See https://api.libreoffice.org/docs/idl/re ... 287f0b4314
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Cursor Paragraph in OO-Basic

Post by JeJe »

Code: Select all


vc =thiscomponent.currentcontroller.viewcursor
tc=vc.text.createtextcursorbyrange(vc)

p=tc.createenumeration.nextelement
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Cursor Paragraph in OO-Basic

Post by JeJe »

You could write your own function but this improvement in LO is trivial, one word and a dot less than typing .createenumeration.nextelement

Code: Select all

Sub Main
vc =thiscomponent.currentcontroller.viewcursor
tc=vc.text.createtextcursorbyrange(vc)

p=TextParagraph(tc)

End Sub

function TextParagraph(tc)
TextParagraph=tc.createenumeration.nextelement
end function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: Cursor Paragraph in OO-Basic

Post by DavidHMcCracken »

JeJe, thank you for that suggestion. It would seem to be the solution in some cases but it is not working in the context of my program. My macro wants to find any bookmark or hyperlink coincident with the view cursor. It iterates over (elements of) the paragraph that contains the cursor and compares the range of each bookmark or hyperlink to that of the cursor.
secEnum = vc.TextParagraph.createEnumeration()
do while secEnum.hasMoreElements()
elem = secEnum.nextElement()
' If elem.Bookmark is not null or elem.HyperLinkURL is not empty then check its range.
This works in LO. Each element of the paragraph is a SwXTextPortion. Replacing vc.TextParagraph with p from your suggestion doesn't work. In both LO and OO, secEnum has only one element. It is SwXTextPortion. I'm not sure what it references but it is not the first element of the paragraph.
tc=vc.text.createtextcursorbyrange(vc)
p=tc.createenumeration.nextelement
secEnum = p.createEnumeration()
In LO, xray shows p and vc.TextParagraph as SwXParagraph with identical content but EqualUNOObjects shows that they are not the same object. I thought that perhaps one was a reference (lval) while the other was a copy (rval) but in both cases changing the String property (using xray) changed the document, which means that both are references. In both cases secEnum is created as SwXTextPortionEnumeration. Thinking that the problem might be local, I moved your code to a function. This had no effect.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Cursor Paragraph in OO-Basic

Post by JeJe »

I created a document added a hyperlink and a bookmark within the hyperlink. The viewcursor was positioned at the bookmark location.
in the code below
Mri vc gives all the information about the hyperlink
Mri pp gives a text portion which is the bookmark

Code: Select all


Sub Main
vc =thiscomponent.currentcontroller.viewcursor
mri vc
tc=vc.text.createtextcursorbyrange(vc)
en=tc.createenumeration
do while en.hasmoreelements = true
p = en.nextelement
en2 =p.createenumeration
do while en2.hasmoreelements = true
pp = en2.nextelement
mri pp
loop
loop

End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: Cursor Paragraph in OO-Basic

Post by DavidHMcCracken »

JeJe, thanks for you quick reply. I previously tested by modifying my rather complex macro. I tested your new suggestion with a dedicated macro on W10 with LO 6.1.5.2, W7 with OO 4.1.8, and Ubuntu-Mate 18.04 with LO 6.0.7.3. In all cases I got the same thing. The paragraph iterator (en.hasmoreelements) executed once while the element iterator (en2.hasmoreelements) didn't iterate even once. This is slightly different from the results in my modified code, where the element iterator served up one element. The only significant code difference is that the latest example creates the iterator and then exercises it while the previous example (which I incorporated into my original code) merges creating and exercising the iterator (one time) in p=tc.createenumeration.nextelement. This code difference should not produce the behavior difference that I see but I also can't understand why I'm not getting the same results that you get. I feel like I'm in the Bermuda Triangle.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Cursor Paragraph in OO-Basic

Post by JeJe »

I thought you were trying to get something that runs on OO which is what I used. If you're trying to get code that runs on both then you can test whether its LO or OO and run different code depending on which. With the amount of divergence between the two that's sometimes going to be the only way.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Cursor Paragraph in OO-Basic

Post by JeJe »

Works the same for me on LO 7.0 and Windows 8 as OO. I've attached the test document. With the cursor within the hyperlink viewcursor gives the info about the hyperlink. If you go to navigator and go to the bookmark, it no longer does so but the textportion is of the type bookmark and you get that instead.
Attachments
tmp.odt
(10.1 KiB) Downloaded 177 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: Cursor Paragraph in OO-Basic

Post by DavidHMcCracken »

JeJe, I have discovered why your suggestion has not been working for me. p=tc.createenumeration.nextelement does get the paragraph but its elements are enumerable only if the paragraph is selected. The OO code also works in LO but I think that vc.TextParagraph is a bit faster.

Code: Select all

function getEnumerableParagrah(vc as object)
    dim tc as object
    on error goto OO
    getEnumerableParagrah() = vc.TextParagraph
    exit function
OO:
    tc = vc.text.createTextCursorByRange(vc)
    tc.gotoStartOfParagraph(FALSE)
    tc.gotoEndOfParagraph(TRUE)
    getEnumerableParagrah() = tc.createEnumeration.NextElement
end function
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
Post Reply