Move view cursor to text shape

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Kyrela
Posts: 5
Joined: Mon Sep 27, 2021 2:15 pm

Move view cursor to text shape

Post by Kyrela »

Hello, I'm trying to edit the values in all text shapes of my document based on my search (Ctrl+R equivalent, but for text shapes). The difficulty here is that I want to paste text instead of simply replace it, so it can format text using html. This implies that I have to use the ViewCursor, move it to the text shape, select the text to replace, and paste. a simple TextCursor would not work as it would paste at the ViewCursor position instead of the TextCursor's one.

The main problem here is that I can't move my ViewCursor the the text shape. Here's what my code looks like and what I've tried:

Code: Select all

# variables is a string that looks like this: "$variable"
# doc is my current text document
# cursor is the ViewCursor of this document
for page in doc.getDrawPages():
    for shape in page:
        if shape.getShapeType() == "com.sun.star.drawing.TextShape":
            while (index_occurrence := shape.String.find(variable)) != -1:
                cursor.gotoRange(shape, False)
                cursor.goRight(index_occurrence, False)
                cursor.goRight(len(variable), True)
                self.cnx.dispatcher.executeDispatch(
                    doc.CurrentController.Frame,
                    ".uno:Paste", "", 0, ())
However, this code does not work, as it raises the following error:

Code: Select all

File "test.py", line 364, in text_fill
  cursor.gotoRange(shape, False)
uno.com.sun.star.uno.RuntimeException: at ./sw/source/uibase/uno/unotxvw.cxx:1040
I've also tried creating a text cursor and move my view cursor to it, like this:

Code: Select all

# variables is a string that looks like this: "$variable"
# doc is my current text document
# cursor is the ViewCursor of this document
for page in doc.getDrawPages():
    for shape in page:
        if shape.getShapeType() == "com.sun.star.drawing.TextShape":
            shape_cursor = shape.createTextCursor()
            shape_cursor.collapseToStart()
            while (index_occurrence := shape.String.find(variable)) != -1:
                cursor.gotoRange(shape_cursor, False)
                cursor.goRight(index_occurrence, False)
                cursor.goRight(len(variable), True)
                self.cnx.dispatcher.executeDispatch(
                    doc.CurrentController.Frame,
                    ".uno:Paste", "", 0, ())
However, it leeds me to the exact same error message.

Code: Select all

File "test.py", line 363, in text_fill
  cursor.gotoRange(shape_cursor, False)
uno.com.sun.star.uno.RuntimeException: at ./sw/source/uibase/uno/unotxvw.cxx:1040
Do you have any idea to solve my problem, fix this error message, or maybe a workaround or even help me to find a better way to insert formatted html without using executeDispatch?
Debian 11 | LibreOffice 7.0.4.2
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Move view cursor to text shape

Post by FJCC »

Since gotoRange() takes a text range as the first argument, try

Code: Select all

cursor.gotoRange(shape_cursor.Start, False)
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.
Kyrela
Posts: 5
Joined: Mon Sep 27, 2021 2:15 pm

Re: Move view cursor to text shape

Post by Kyrela »

Unfortunately, the exact same error is raised, without further information... Any idea why?
Debian 11 | LibreOffice 7.0.4.2
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Move view cursor to text shape

Post by FJCC »

Kyrela wrote: Thu Mar 23, 2023 4:36 pm Any idea why?
I can make something up, after finding out that an error was thrown. The shape_cursor may only know about positions within the shape where it was made and have no information about where it is in the document. That is, the View Cursor can't jump to the Start of the shape_cursor because the Start object does not contain information about where it is in the document.
I have no idea if that is true.
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.
Kyrela
Posts: 5
Joined: Mon Sep 27, 2021 2:15 pm

Re: Move view cursor to text shape

Post by Kyrela »

Interesting. How can I "inform" it about the position of the shape in the document?
Debian 11 | LibreOffice 7.0.4.2
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Move view cursor to text shape

Post by JeJe »

Manually putting the viewcursor inside a drawing textbox and examining with MRI - says no text selection.

This (in basic) will select the first control/textbox in a Writer document.

Code: Select all

thiscomponent.currentcontroller.select(thiscomponent.drawpage.getbyindex(0))
Following which, manually typing a letter key will move the selection to the text and then a paste operation can be performed.

Simulating manually typing a letter can be done with the Windows API, but I don't know about on Linux.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Move view cursor to text shape

Post by JeJe »

In Basic in Windows it would be this:
(adds a space which could be removed.)

Code: Select all

Public Declare Sub keybd_event Lib "user32" Alias "keybd_event" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const VK_SPACE = &H20
Const KEYEVENTF_KEYUP = &H2


sub focusDrawingTextbox()

thiscomponent.currentcontroller.select(thiscomponent.drawpage.getbyindex(0)) 'adding .gettext gives the same result, selects the textbox not text
keybd_event VK_SPACE, 0, 0, 0 ' press Space
keybd_event VK_SPACE, 0, KEYEVENTF_KEYUP, 0 ' release Space
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply