Copy paragraph to another doc - lesson 2
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?

