[scribus-dev] Scripter: linked frames

Michael J Gruber mjg at fedoraproject.org
Wed Mar 3 10:16:11 UTC 2021


Gregory Pittman venit, vidit, dixit 2021-03-02 21:46:38:
> On 3/2/21 2:09 PM, Jean Ghali wrote:
> > Le 02/03/2021 à 19:41, Gregory Pittman a écrit :
> >> Has anyone had any luck using getFirstLinkedFrame() and getNextLinkedFrame()?
> >>
> >> I have been trying to write a script that would march down a set of linked frames, so I could analyze the content. getFirstLinkedFrame() seems to work as expected, but not getNextLinkedFrame(). The script spins and spins and spins and won't quit.
> >> The documentation says that if you do this:
> >>
> >> textbox = scribus.getNextLinkedFrame()
> >>
> >> it returns None if there are no more, but what "value" is assigned to textbox in that case? I'm trying to stop execution of frame content analysis when it reaches that point.
> >> I even tried
> >>
> >> while (textbox):
> >>
> >> thinking that would evaluate to 0 if there were no more, but that doesn't work. Trying to put str(textbox) into a messageBox() doesn't work.
> >>
> >> Greg
> >>
> > 
> > Here an example which loops across frames of a text chain and print the name of each linked frame:
> > 
> > textbox = scribus.getNextLinkedFrame("Text1")
> > while textbox is not None:
> >     print(textbox)
> >     textbox = scribus.getNextLinkedFrame(textbox)
> > 
> Great! Thanks, Jean.
> 
> The thing I didn't understand is the need for getNextLinkedFrame() to have an object inside the parentheses. Without that, it keeps going back to the initial frame that I selected before I ran the script.

"" (empty) means "selected frame", and getNextLinkedFrame("") gives you
the next frmae linked from the selected frame - without selcting that
next frame. Thus, repeated calls give the same frame.

So, this works as expected from the meaning of "" but it's not very
pythonic - "next" sounds much like the next method of an iterator. You
can turn it into one using a generator

def LinkedFrames(frame=None):
	frame = scribus.getNextLinkedFrame(frame)
	while frame:
		yield frame
		frame = scribus.getNextLinkedFrame(frame)

and then "for frame in LinkedFrames()" works as expected.

Simplifying with py3.10's ":=" is left as an exercise ;)

Cheers
Michael



More information about the scribus-dev mailing list