Skip to main content
K.Daube
Community Expert
Community Expert
April 18, 2015
Answered

Copy paragraph to another doc - lesson 2

  • April 18, 2015
  • 1 reply
  • 970 views

I need to separate the task into 2 functions, because the second function will be used multiple times if processing book files.

In GetBiblioFromRTF paragraphs from document-1 are stored into a global array.

In PasteAtEnd the array-items are read and copied/pasted to the end of the active document-2..

If not closing the first document (outcomment line 18), the second document does not become active and hence the data is copied to its end.

However, if closing document-1 after filling the global array, the array is empty in each position and nothng is pasted (see the alerts).

#target FrameMaker
var gaBibliography= [];                           // keep bibliography lines from processed RTF
GetBiblioFromRTF ();

PasteAtEnd();

function GetBiblioFromRTF () {
  var nBib = 0, newDoc, pgf, pgf1, tRange;
  newDoc = app.ActiveDoc;
  pgf = newDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
// --- read the bibliography into the global array
  while(pgf.ObjectValid()) {                      // until end of doc - take whole paragraphs
    nBib += 1;                                    // count biblio paragraphs for later use in message
    gaBibliography.push(pgf);                     // place the paragraph
    pgf = pgf.NextPgfInFlow;
alert (GetText (gaBibliography[nBib-1], newDoc)); // => expected text
  }
  newDoc.Close (Constants.FF_CLOSE_MODIFIED);     // source document
} // --- end GetBiblioFromRTF

function PasteAtEnd () {
  var j, jMax, oDoc, lastPgf, pgf, textLoc, tRange;
  jMax = gaBibliography.length;
  if (app.ActiveDoc.ObjectValid()) {              // target document
    oDoc = app.ActiveDoc;
  } else {
    Alert ("No active document!");
    return;
  }
  lastPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.LastPgf;
  pgf = oDoc.NewSeriesPgf (lastPgf);              // Add a new paragraph at end of flow
  tRange = new TextRange;

  for (j = 0; j < jMax; j += 1) {
    pgf1 = gaBibliography;                     // is object paragraph
alert (GetText (gaBibliography, oDoc));        // => nothing
    tRange.beg.obj = pgf1;                        // select it
    tRange.beg.offset = 0;
    tRange.end.obj = pgf1;
    tRange.end.offset = Constants.FV_OBJ_END_OFFSET;
    oDoc.TextSelection = tRange;
    PushClipboard ();
    oDoc.Copy (0);                                // copy selection to clipboard not interactive
    textLoc = new TextLoc (pgf, 0); 
    textRange = new TextRange (textLoc, textLoc);
    oDoc.TextSelection = textRange;               // set TextSelection to insertion point
 
    oDoc.Paste ();                                // "replace" TextSelection
    PopClipboard ();
  }
}

function GetText (textObj, doc) {
  var text = "", textItems;
  if (textObj.constructor.name !== "TextRange") { // Get a list of the strings in the text object or text range
    var textItems = textObj.GetText(Constants.FTI_String);
  } else {
    textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
  }
  for (var i = 0; i < textItems.len; i += 1) {    // Concatenate the strings
    text += (textItems.sdata);
  }
  return text;                                    // Return the text
} // --- end GetText

There seems to be a 'hidden connection' between the array items and the first document, which of course will be broken, if this is closed. I thought to have all the stuff in the array which is needed for the second step.

Can anybody give me a hint how get this running correctly?

This topic has been closed for replies.
Correct answer Klaus Göbel

Hi Klaus,

the simlest way is to avoid "acticeDoc".

Why dont you use sth. like:

myOldDoc = Open (fileNameOldDoc, openParams, openReturnParams);  

myNewDoc = Open (fileNameNewDoc, openParams, openReturnParams);  

And don't close one of the until the script has finished. Otherwise the ID's of the paragraphs get lost.

BTW: Are you in Darmstadt this week? http://tagungen.tekom.de/f15/startseite/

So we could meet!

1 reply

Klaus Göbel
Klaus GöbelCorrect answer
Legend
April 19, 2015

Hi Klaus,

the simlest way is to avoid "acticeDoc".

Why dont you use sth. like:

myOldDoc = Open (fileNameOldDoc, openParams, openReturnParams);  

myNewDoc = Open (fileNameNewDoc, openParams, openReturnParams);  

And don't close one of the until the script has finished. Otherwise the ID's of the paragraphs get lost.

BTW: Are you in Darmstadt this week? http://tagungen.tekom.de/f15/startseite/

So we could meet!

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
April 19, 2015

Thanks Klaus for the idea presented to avoid the ActiveDoc. The paragraph ID's seem to be the missing link which I assumed to exist.

For the first type of references it was sufficient to work with strings - so no paragraph-IDs were involved. But for replacing placeholders in footnotes with full bibliographic references I need the formatted paragraphs. I will continue my development with Your hint.

Retired since seven years I'm no more in real business - attend only now and then a tecom.ch or itl event in Switzerland or Munich; mainly to meet old friends and stay in contact with doc-technology.