Copy link to clipboard
Copied
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
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 () ===
Copy link to clipboard
Copied
Klaus, can you post a link to your test document. Thanks. -Rick
Copy link to clipboard
Copied
Rick, the fm document is in the mentioned zip: https://daube.ch/zz_tests/Test4.zip .
Copy link to clipboard
Copied
Hi Klaus,
your script works so far.
If you increase the length of the page, you'll see.
You have to consider a pagebreak;
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
Dear Rick,
Shame on me! I was so fixated (like the rabbit in front of the snake) on the LastPgf that I didn't notice the limitations of the TextFrame.
BTW: what does the CP mean in
alert (CP.getText (pgf, doc));
@Klaus: Even the observation that with enough room on the page the scripts works fine did not switch on a light in my thoughts.
Copy link to clipboard
Copied
I am sorry, I meant to take the CP.getText call out before I posted. I have a library of functions that I have loaded in my startup folder stored in a CP object. This allows me to build scripts without having to copy frequently-used functions into every script.