Scribus Forums

Scribus => Scripts and Plugins => Topic started by: Lynn on September 27, 2025, 08:29:37 PM

Title: Get position of selected text in script
Post by: Lynn on September 27, 2025, 08:29:37 PM
I'm working on a script for Scribus to speed up making pdf bookmarks and I've gotten a bit stuck. It would be great to be able to speed up making chapter titles or section titles into bookmarks.

What I would like the script to do is:
    • copy the text currently highlighted
    • create a new text frame on a separate layer (let's call it "Bookmarks)
    • paste the highlighted text into our new frame
    • set that new frame to be a bookmark
    • swap back to our original layer

I've got most of that working. But what I can't figure out is aligning align my new bookmark text frame with the original selected text. Is there a script command to fetch the starting coordinates of currently selected text? The best I can do is position at the starting coordinates of the text frame, which in a book is probably the top of the page.

(code is a WIP, I need to add error checking, styling and setting the size for the text frame)
activeLayer = scribus.getActiveLayer()
position = scribus.getPosition()
size = scribus.getSize()
scribus.copyObjects()
bookmarkText = scribus.getFrameText()
bookmarkName = bookmarkText
scribus.setActiveLayer("Bookmarks")
scribus.createText(position[0], position[1], 5.5, 0.25, bookmarkName)
scribus.insertText(bookmarkText, 0, bookmarkName)
scribus.setPDFBookmark(1, bookmarkName)
scribus.setActiveLayer(activeLayer)
Title: Re: Get position of selected text in script
Post by: a.l.e on September 28, 2025, 12:27:45 PM
I'm not sure that Scribus has code to make available the position of the selection.

I guess that the information does exist (otherwise, Scribus could not draw the highlighting) but I don't think that it is exposed to other parts of the code.

Of course, being able to define bookmarks and links is THE use case for Scribus to track that information.
Even more important, now that we have a usable table of contents!
So, enhancing Scribus to provide the position of the selection (or of arbitrary groups of contiguous characters) would lead to the feature being added to Scribus and probably make your Script useless.

What we need is somebody with strong programming skills to tackle the problem and suggest a solution.
And it must be somebody who is patient enough to accept the slower feedback loops from the main developers.

Now, if you want a solution now, you might want to look at tools that identify the position of text in a PDF or PNG and use that information to define the frames in Scribus.
The problem is when you have multiple matches that are plausible (but also for this case there are workarounds...).
I've never tried it but it could be an interesting project and it could work : - )
Title: Re: Get position of selected text in script
Post by: Lynn on September 28, 2025, 07:25:33 PM
I figured that might be the case - thanks for confirming there's wasn't a scripting variable I was missing.

That all makes sense; and given that my use case is adding bookmarks for ~30 header pages in a digital short-story anthology for my writing club, it will definitely be faster to just use my script as-is than try to untangle locations. I'll generate the bookmarks at the top of the page and move into place manually once they're created.