Skip to main content
K.Daube
Community Expert
Community Expert
July 10, 2019
Question

Insert a paragraph object somewhere else

  • July 10, 2019
  • 1 reply
  • 816 views

Dear friends, It seems that I stretch my knowledge to far...

I have collected footnotes in an array aoFNcollected.

A heading has already be placed: oPgf

Now I want to place the FN paragraphs to a new place forming endnotes (remember, a footnote can contain more than 1 paragraph).

for (j = 0; j < nNotes; j++) {

  oFn = aoFNcollected;                       // Fn to be handled now

  oFnPgf = oFn.FirstPgf;                        // first ¶ in footnote

  oPgf = oDoc.NewSeriesPgf(oPgf);               // new empty ¶ in doc

  oDoc.TextSelection = oFnPgf;                  // this does not work - halt of script

  oDoc.Copy(0);                                 // copy Fn para

  oTL = new TextLoc (oPgf, 0); 

  oTR = new TextRange(oTL, oTL);

  oDoc.TextSelection = oTR;

  oDoc.Paste();                                 // paste Fn para

  Fno_ApplyPgfFmt (oDoc, oPgf, goFno.FmtEn)     // apply ¶ format zen-endnote

// further code not yet verified - just showing my intention

  oFnPgf = oFnPgf.NextPgfInFlow;                // ??? is a FN also a flow of its own ???

  while (oFnPgf.ObjectValid()) {                // a footnote can have multiple paragraphs

    oPgf = oDoc.NewSeriesPgf(oFnPgf);

    Fno_ApplyPgfFmt (oDoc, oPgf, goFno.FmtEnCont) // apply ¶ format zenc-endnote-continue

    oFnPgf = oFnPgf.NextPgfInFlow;              // get next ¶ from footnote

  }

}

I tried to use the copy/paste method - but obviously line 05 does not provide a TextSelection, because the object comes from an array and not from the document.

But how can I place a Pfg object to a new place defined by a new Pgf (line 04)?

This topic has been closed for replies.

1 reply

frameexpert
Community Expert
Community Expert
July 10, 2019

You can't select a paragraph by just assigning it as the document's TextSelection property. You have to make a TextRange first:

var textRange;

textRange = new TextRange (

    new TextLoc (oFnPgf, 0),

    new TextLoc (oFnPgf, Constants.FV_OBJ_END_OFFSET));

oDoc.TextSelection = textRange;

Or, you could try selecting and copying them all at once:

var textRange;

textRange = new TextRange (

    new TextLoc (oFn.FirstPgf, 0),

    new TextLoc (oFn.LastPgf, Constants.FV_OBJ_END_OFFSET));

oDoc.TextSelection = textRange;

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

Thanks to Ric I'm a great step further - but the results are still not what I want:

Stopping the script at line 27 to inspect the results I see this (the pasted Fn para has its 'old' ¶-format):

Why the heck does Paste not insert at the 'current location' defined by oTL in line 22?

Whether I define the new paragraph oPgf in line 15 or do that later just before defining oTL ahs no influence.

function Fno_ConvertFootnotes (oDoc, aoFNcollected) { //=== convert the footnotes to endnotes =====

var j, nNotes, oFn, oFnPgf, oPgf, oPgfFmt, oPgfFmtC, oPgfProps, oPgfPropsC, oTL, oTR, sText;

  nNotes = aoFNcollected.length;

//                                                --- At end of document insert title paragraph ---

  oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.LastPgf;  // get last oPgf in flow

  oPgf = oDoc.NewSeriesPgf(oPgf);                 // add a paragraph

  oTL = new TextLoc (oPgf, 0); 

  sText = "Endnotes";                             //

  oDoc.AddText (oTL, sText);                      // Create the title

  oPgfFmt = oDoc.GetNamedObject(Constants.FO_PgfFmt, goFno.FmtEnTitle);

  oPgfProps = oPgfFmt.GetProps();                

  oPgf.SetProps(oPgfProps);                       // apply ¶ format

//                                                --- Add new paragraphs at end of doc and format

  for (j = 0; j < nNotes; j++) {

    oPgf = oDoc.NewSeriesPgf(oPgf);               // new empty ¶ for endnote

    oFn = aoFNcollected;                       // Fn to be handled now

    oFnPgf = oFn.FirstPgf;                        // first ¶ in footnote

    oDoc.TextSelection = new TextRange (

      new TextLoc (oFnPgf, 0),                    // Fn ¶s get different ¶-formats

      new TextLoc (oFnPgf, Constants.FV_OBJ_END_OFFSET)); 

    oDoc.Copy(0);                                 // copy Fn para

    oTL = new TextLoc (oPgf, 0); 

    oDoc.TextSelection = new TextRange (oTL, oTL); // essential ?

    oDoc.Paste();                                 // paste Fn para

    Fno_ApplyPgfFmt (oDoc, oPgf, goFno.FmtEn)     // apply ¶ format zen-endnote

// code to handle second to last Fn paragraph

  }

// code to replace the Fn anchor by cross ref to endnote

} //--- end Fno_ConvertFootnotes 

function Fno_ApplyPgfFmt (oDoc, oPgf, sPgfFmt) {

// Reference  https://forums.adobe.com/thread/1711161

var oPgfFmt = 0, oProps;

  oPgfFmt = oDoc.GetNamedPgfFmt(sPgfFmt); 

  if (oPgfFmt.ObjectValid()) { 

    oProps = oPgfFmt.GetProps(); 

    oPgf.SetProps(oProps); 

  } else { 

    oPgf.Name = sPgfFmt;                         

  }

} //--- end Fno_ApplyPgfFmt 

I can not see that the order of my statements are wrong, but I may be blindfolded...

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
July 11, 2019

To ease reproduction of the problem I have stripped down my script to something which should run on any document.

You may replace the format "Table Title" by something elsle in your document.

Running the below script on a document with the default template gives these results:

EDITED

Here the last inserted line gets the applied format, but the copy creates an own new paragraph!

How can I get the copied ¶ to get the new format - or copy into the willingly inserted new ¶?

Edited again

I have probably shot myself into the knee: Since I copy a paragraph, a paste will insert a paragraph, not just its contents...

Will have to check this after lunch break.

// CopyParagraph.jsx

// add a copy of the first para to the end

#target framemaker

main ();

function main () {

var oDoc, oFnPgf, oPgf, oTL;

  oDoc = app.ActiveDoc;

  oFnPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

  oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.LastPgf;

  oTL = new TextLoc (oPgf, 0);

  oDoc.TextSelection = new TextRange (

    new TextLoc (oFnPgf, 0),                    //

    new TextLoc (oFnPgf, Constants.FV_OBJ_END_OFFSET));

  oDoc.Copy(0);                                // copy Fn para

  oPgf = oDoc.NewSeriesPgf(oPgf);              // new empty ¶

  Fno_ApplyPgfFmt (oDoc, oPgf, "Table Title")  // apply ¶ format

  oTL = new TextLoc (oPgf, 0);

  oDoc.TextSelection = new TextRange (oTL, oTL); // essential !

  oDoc.Paste(0);                                // paste Fn para

}

function Fno_ApplyPgfFmt (oDoc, oPgf, sPgfFmt) {

var oPgfFmt = 0, oProps;

  oPgfFmt = oDoc.GetNamedPgfFmt(sPgfFmt);

  if (oPgfFmt.ObjectValid()) {

    oProps = oPgfFmt.GetProps();

    oPgf.SetProps(oProps);

  } else {

    oPgf.Name = sPgfFmt;                        

  }

} //--- end Fno_ApplyPgfFmt