Skip to main content
K.Daube
Community Expert
Community Expert
July 16, 2019
Answered

Strange behaviour at bottom of page

  • July 16, 2019
  • 1 reply
  • 1013 views

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 

This topic has been closed for replies.
Correct answer frameexpert

You problem is likely line 18, because the last paragraph in the first text frame might not be the last paragraph in the flow. You may want to use a function to get the last paragraph in the flow.

#target framemaker

var doc, pgf;

pgf = getLastPgfInMainFlow (doc);

alert (CP.getText (pgf, doc));

function getLastPgfInMainFlow (doc) {

   

    var textFrame;

   

    textFrame = doc.MainFlowInDoc.LastTextFrameInFlow;

    while (textFrame.ObjectValid () === 1) {

        if (textFrame.LastPgf.ObjectValid () === 1) {

            return textFrame.LastPgf;

        }

        textFrame = textFrame.PrevTextFrameInFlow;

    }

}

1 reply

frameexpert
Community Expert
Community Expert
July 16, 2019

Klaus, can you post a link to your test document. Thanks. -Rick

www.frameexpert.com
K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
July 16, 2019

Rick, the fm document is in the mentioned zip: https://daube.ch/zz_tests/Test4.zip .

Klaus Göbel
Legend
July 16, 2019

Hi Klaus,

your script works so far.

If you increase the length of the page, you'll see.

You have to consider a pagebreak;