Scribus Forums

Scribus => Scripts and Plugins => Topic started by: prcek on May 09, 2025, 03:28:12 PM

Title: Replacing selected text
Post by: prcek on May 09, 2025, 03:28:12 PM
Hello everyone.

I'm trying to write a script that gets a selected text from a frame, changes it and replaces the original text by the new one.

What I cannot find out is how can I get the selection's start position to insert the new text at.

Here is very simplified version of my script, that should put selected text into parentheses.
It expects a text in a text frame selected.
Of course if you select text from start it will work.

import scribus

frame = scribus.getSelectedObject()
txt = scribus.getAllText(frame)
txt = '(' + txt + ')'
scribus.deleteText(frame)

position = 0
scribus.insertText(txt, position, frame)

scribus.layoutTextChain(frame)
scribus.setRedraw(True)
scribus.docChanged(True)

I want to avoid workarounds like getting the selected text and search it to get it's position as there is no certainty it is unique.

Is there any function like getTextSelection([name]) or similar?

Title: Re: Replacing selected text
Post by: a.l.e on May 09, 2025, 05:06:23 PM
It's

scribus.getSelectedTextRange()
... No idea why it's not in my documentation.
... I guess it's time to fix the script that generates it and do an update!
Title: Re: Replacing selected text
Post by: prcek on May 10, 2025, 06:44:32 AM
Thank you very much!
It was really weird to me nobody needed it before. And I didn't find it in any script I checked

I was looking for it in output of
import scribus
help(scribus)

I run it on 1.6.1 (appimage) and it is missing there (checked right now). Could it be the reason it is missing in your generated doc?

It is present in 1.6.3 and 1.6.4. help, though.
Title: Re: Replacing selected text
Post by: a.l.e on May 10, 2025, 08:18:30 AM
Now that you say it, I have the feeling that I already have used it...

I did a quick check and found this:


https://github.com/aoloe/scribus-script-repository/blob/master/sort_lines/sort_lines.py

It does not seem to do what it should, but it does get the text selection and it even provides a polyfill : - )
Title: Re: Replacing selected text
Post by: prcek on May 10, 2025, 09:55:37 AM
Well, definitely I was checking that repo as well :-)
But if you don't know what exactly you're looking for it is more difficult to find it.
And as I wrote, the problem with find() can arise in case of a repetitive text parts.
Thank you again, getSelectedTextRange() seems to do what I need.