Copy link to clipboard
Copied
var gDoc = app.documents[0];
var tocStyle = gDoc.paragraphStyles.itemByName("Inhaltsverzeichnis bold");
var tocStyle2 = gDoc.paragraphStyles.itemByName("Inhaltsverzeichnis");
var tocMissing = gDoc.paragraphStyles.itemByName("MARKER"); // DEBUG
var sel = gDoc.bookmarks.everyItem().getElements();
var toc = gDoc.pages[0].textFrames.add();
var tocS = toc.parentStory;
for (var i = 0; i < sel.length; i++) { // iterate through bookmarks
var page = sel[i].destination.destinationPage;
var entry = sel[i].name;
var sec = sel[i].bookmarks.everyItem().getElements();
if (sec.length > 0) {
tocS.contents += entry + "\r";
$.writeln("- " + tocS.paragraphs[-1].contents);
tocS.paragraphs[-1].appliedParagraphStyle = tocStyle;
for (var k = 0; k < sec.length; k++) {
tocS.contents += sec[k].name + "\t" + (sec[k].destination.destinationPage.documentOffset + 1) + "\r";
tocS.paragraphs[-1].appliedParagraphStyle = tocStyle2;
$.writeln("---- " + tocS.paragraphs[-1].contents);
}
} else {
tocS.contents += entry + "\t" + (page.documentOffset + 1) + "\r";
$.writeln(tocS.paragraphs[-1].contents);
tocS.paragraphs[-1].appliedParagraphStyle = tocMissing;
}
}
var gB = toc.geometricBounds;
var gBnew = [ gB[0], gB[1], gB[0]+125, gB[1]+168 ];
toc.geometricBounds = gBnew;
You're adding \r at the end of your insertion, then trying to apply the style to paragraphs [-1], which is an empty paragraph. Try looking into that. Adding content and applying paragraph styles can be tricky like that. Often I will add in the contents with no styles but with some kind of marker, then use a FindGrep/Text function to remove the marker and add the style.
Copy link to clipboard
Copied
You're adding \r at the end of your insertion, then trying to apply the style to paragraphs [-1], which is an empty paragraph. Try looking into that. Adding content and applying paragraph styles can be tricky like that. Often I will add in the contents with no styles but with some kind of marker, then use a FindGrep/Text function to remove the marker and add the style.
Copy link to clipboard
Copied
Hi @brianp311
I think you're right.
I have to fiddle a little bit around this, on a quick test I'm not quite getting it.
I tried going a quicker route than adding findGrep
– putting the "\r" after the applying
– instead of -1 using .length-1
That doesn't change the result?
Copy link to clipboard
Copied
Instead of applying the style to paragraphs[-1], apply it to the last insertion point of the story: story.insertionPoints[-1]
P.
Copy link to clipboard
Copied
Hi @Peter Kahrel,
thanks for chipping in – although I don't know what difference this effectively should make, in the end your input actually lead to the real issue here, being .parentStory.contents += "mynewcontent".
I spend another hour debugging – and after undoing all steps and looking at the undo commands they read "Replace text" *lightbulb*.
Now it's obvious to me... I always replace the whole thing instead of just adding a line (as += lead me to believe)...
Thank you both.
And for feature readers, here's the working snippet (I sticked to using insertionPoints)
var gDoc = app.activeDocument;
var tocStyle = gDoc.paragraphStyles.itemByName("Inhaltsverzeichnis bold");
var tocStyle2 = gDoc.paragraphStyles.itemByName("Inhaltsverzeichnis");
var sel = gDoc.bookmarks.everyItem().getElements();
var toc = gDoc.pages[5].textFrames.add();
toc.applyObjectStyle(gDoc.objectStyles.itemByName("2sp"), true, false);
var gB = toc.geometricBounds;
var gBnew = [ gB[0]+11, gB[1], gB[0]+125, gB[1]+200 ];
toc.geometricBounds = gBnew;
var tocS = toc.parentStory;
for (var i = 0; i < sel.length; i++) { // iterate through bookmarks
var sec = [];
var page = sel[i].destination.destinationPage;
var entry = sel[i].name;
var sec = sel[i].bookmarks.everyItem().getElements();
if (sec.length > 0) { // are there nested bookmarks?
tocS.insertionPoints[-1].contents = entry;
tocS.insertionPoints[-1].appliedParagraphStyle = tocStyle;
tocS.insertionPoints[-1].contents = "\r";
for (var k = 0; k < sec.length; k++) { // iterate through nested bookmarks
tocS.insertionPoints[-1].contents = sec[k].name + "\t" + (sec[k].destination.destinationPage.documentOffset + 1);
tocS.insertionPoints[-1].appliedParagraphStyle = tocStyle2;
tocS.insertionPoints[-1].contents = "\r";
}
} else {
tocS.insertionPoints[-1].contents = entry + "\t" + (page.documentOffset + 1);
tocS.insertionPoints[-1].appliedParagraphStyle = tocStyle2;
if (i != sel.length-1) tocS.insertionPoints[-1].contents = "\r";
}
}
Copy link to clipboard
Copied
. length-1 is essentially the same as [-1]. I was thinking try paragraphs[-2], or try Peter's suggestion.