[Solved] Selecting text by character index with TextField

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
mav
Posts: 2
Joined: Tue Mar 27, 2012 3:25 pm

[Solved] Selecting text by character index with TextField

Post by mav »

Hi community!

In order to provide support for speech recognition in an OO.o writer document, I'd need to access the document text on a character-index basis, i.e. "get the number of characters before the current selection", "select characters 17 thru 29", and so on.

This works quite well in simple documents, but as soon as there's a TextField in the document, the calculation gets messed up.

In order to get the character index for the current selection, I get myself a TextViewCursor, create a TextCursor from it, call collapseToStart() and gotoStart(true) to expand it to the beginning of the text. Then I get the text string contained in this TextCursor and thus the number of characters where the selection starts.
Using this method, for each TextField within the Range I get the number of characters inside the TextField, e.g. a TextField containing the date "27.03.12" results in 8 characters.

The problem occurs when I try to set the selection.
This is done by using a TextCursor at the beginning of the document and then calling goRight(n, false) when n is the character index I want to reach.
Unfortunately, goRight() doesn't always select the number of characters (as the documentation states). A TextField in this part of the document is being counted as a single character!
So when I'm at the beginning of the date TextField and call goRight(1,false), the selection has been moved by 8 characters instead of one!

Does anyone have an idea what to do about it?

Thanks in advance,
mav
Last edited by mav on Wed Mar 28, 2012 9:24 am, edited 1 time in total.
OpenOffice 3.3 on Windows 7 x64
FJCC
Moderator
Posts: 9284
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Selecting text by character index with TextField

Post by FJCC »

I confirmed that a text cursor treats a Date field as a single character. I then tried making enumerations of the paragraphs and portions (a uniformly formatted section of a paragraph) and found that a date field is properly counted as a separate portion. I can get the cursor to span the date field by sending it to the Start and End of each portion.

Code: Select all

oText = ThisComponent.getText()
Curs = oText.createTextCursor()
ParaEnum = oText.createEnumeration()
While ParaEnum.hasmoreElements()
	Para = ParaEnum.nextElement()	
	Portion_Enum = Para.createEnumeration()
	While Portion_Enum.hasMoreElements
		Portion = Portion_Enum.nextElement()
		Curs.gotoRange(Portion.Start, False)
		Curs.gotoRange(Portion.End,True)
		Print Curs.string
	WEnd
WEnd
Similarly, the value returned by Len(Portion.String) is 8 when examining the date field.
I'm not sure that will help you.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
mav
Posts: 2
Joined: Tue Mar 27, 2012 3:25 pm

Re: Selecting text by character index with TextField

Post by mav »

Hi!
Thanks for taking your time to try it out.
Meanwhile I had an idea how to solve the problem of selecting a given character position.

Instead of calling goRight(n, false), I now call goRight(1, true) in a loop and check the length of the string contained in the TextRange every time.

Quite ugly workaround :crazy: , but at least it works.
Don't know how the runtime behaviour is for longer documents, though.

Regards,
mav
OpenOffice 3.3 on Windows 7 x64
Post Reply