Strange behaviour at bottom of page
Friends of FrameMaker and ExtendScript!
With my script Convert Footnotes to Endnotes I got weird results and hence stepped back to the simpler case of just copying paragraphs.
Collect&CopyParagraphs.jsx does what the name says: copy paragraphs from the beginning of the document at the end.
To see what is copied the source paragraphs have format Body_1 … Body_5.
The script works fine as long as there is enough space towards the bottom of the page:

The first run of the script produces the output indicated by the upper brace.
The second run produces a mutilated output indicated by the lower brace.
The output of the third run can not be recognised, you only notice flicker on the bottom line.
The script, however seems to run correctly. The logging (see file test4-3times.log) of the 3 runs is identical.
→ What is going on here?
Use the files in https://daube.ch/zz_tests/Test4.zip for verification.
// Collect&CopyParagraphs.jsx
// collect pargraphs and copy them to the end fo teh document
#target framemaker
main ();
function main () {
var oDoc, aoPgfs = [], oPgf, oPgf1, oPgfLast, oPgfNew
nPara = 5;
oDoc = app.ActiveDoc;
CollectParagraphs (oDoc, aoPgfs, nPara);
for (j = 0; j < nPara; j++) {
oPgf1 = aoPgfs
; SelectParagraph (oDoc, oPgf1); // essential !
oDoc.Copy(0); // cut 1st para
oPgfLast = oDoc.MainFlowInDoc.FirstTextFrameInFlow.LastPgf;
$.writeln("\noPgf after copy:\t" + oPgfLast.Name);
oPgfNew = oDoc.NewSeriesPgf(oPgfLast); // add a paragraph
SelectParagraph (oDoc, oPgfNew); // essential !
oDoc.Paste(0); // paste fist para
oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.LastPgf; // Superfluous by Copy
$.writeln("oPgf after paste:\t" + oPgf.Name);
SelectParagraph (oDoc, oPgf); // essential !
oPgf.Delete();
oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.LastPgf;
$.writeln("oPgf inserted:\t" + oPgf.Name);
}
}
function SelectParagraph (oDoc, oPgf) {
oDoc.TextSelection = new TextRange (
new TextLoc (oPgf, 0),
new TextLoc (oPgf, Constants.FV_OBJ_END_OFFSET));
} //--- end SelectParagraph
function CollectParagraphs (oDoc, aoPgfs, nPara) { //=== collect the first n paragraphs in current document
// arguments aoPgfs Array of Pgf objects.
// nPara Numer of ¶ to be collected at the start of the document
var oPgf;
aoPgfs.length = 0; // clear array
oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
for (j = 0; j < nPara; j++) {
if (oPgf.ObjectValid()) {
aoPgfs.push(oPgf);
}
oPgf = oPgf.NextPgfInFlow;
}
return;
} //--- end CollectParagraphs

