Python script API: How to access elements within groups?

Previous topic - Next topic

sbordag

I am writing a script to access all text frames to automatically fix/reduce their font size a little bit, so that any overflowing text becomes visible.
This works already, except that the only way to access an element is by using its name, and apparently I can only access top-level elements (top-level from the point of view of grouped objects not being top-level).

Thus, in a document with this structure:
- text_frame_1
- text_frame_2
- group1 with text_frame_3 and text_frame_4

I would only be able to access text frames 1 and 2, but not 3 and 4. For example scribus.getObjectType("text_frame_1") gives me the frame, but scribus.getObjectType("text_frame_3") results in NoValidObjectError instead.

The only way to do that seems to be to ungroup any group, do whatever I want to do, and then re-group them back again, which is ugly and error prone (like what if the groups initially had some useful attributes?)

Is there any way to non-destructively access nested elements?

AdmFubar

not sure if this will help or not you may want to use this command
getSelectedObject(...) getSelectedObject([nr]) -> string
Returns the name of the selected object. "nr" if given indicates the number of the selected object, e.g. 0 means the first selected object, 1 means the second selected Object and so on.
this may allow you to access the items withing the group.
Using Scribus 1.6.1, openSUSE 15.5
Advanced hobbyist

sbordag

Thanks for trying to help there!

Unfortunately, using selectObject requires something to be selected first, and the selectObject function also throws scribus.NoValidObjectError: Object not found

Thus, I still can't access elements within groups (or even elements within groups within groups...)

I think it's really simply that whoever implemented getAllObjects and the selectObject functions simply forgot that grouping could be a thing... Now, I am at a loss on how to proceed...

sbordag

Alright, so I went the long route around the problem. In pseudocode the script that starts having a document opened will:
- destroy all groups
- find out which text field to reduce the font size (and line spacing) by how much so that all text fits
- remember that in an object
- close the document without saving it
- go to the original SLA XML file
- apply the remembered fixes directly in the XML file
- re-open in scribus and proceed being happy now that all all texts fit and we still have our group structure in place

Note: the scribus.textOverflows(name) function is not perfect and will report that nothing overflows even though in some corner cases it will still overflow, so always reduce font size by one point more than seems necessary. Couldn't find any corner cases after that, but there still might be some.