Wrong paragraph style gets applied – appliedParagraphStyle

Contributor ,
Jan 12, 2021

Copy link to clipboard

Copied

Hi there,
okay, this drives me nuts – I'm writing a script, which creates a "manual" nested TOC based on (also nested) bookmarks.
Now everything works fine – except I can't apply the correct paragraph styles.

If I do a console output I get all the correct paragraphs, no wrong doubles or anything, so I would think I target the right paragraph but somehow everything has applied the style tocMissing at the end.

What am I missing?

I already tried applyParagraphStyle and changd toS to gDoc.pages[0].textFrames.parentStory

 

 

    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;
​

 

 

TOPICS
Scripting

Views

76

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Wrong paragraph style gets applied – appliedParagraphStyle

Contributor ,
Jan 12, 2021

Copy link to clipboard

Copied

Hi there,
okay, this drives me nuts – I'm writing a script, which creates a "manual" nested TOC based on (also nested) bookmarks.
Now everything works fine – except I can't apply the correct paragraph styles.

If I do a console output I get all the correct paragraphs, no wrong doubles or anything, so I would think I target the right paragraph but somehow everything has applied the style tocMissing at the end.

What am I missing?

I already tried applyParagraphStyle and changd toS to gDoc.pages[0].textFrames.parentStory

 

 

    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;
​

 

 

TOPICS
Scripting

Views

77

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 12, 2021 0
Adobe Community Professional ,
Jan 12, 2021

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 12, 2021 0
Contributor ,
Jan 12, 2021

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 12, 2021 0
Adobe Community Professional ,
Jan 13, 2021

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 13, 2021 0
Adobe Community Professional ,
Jan 13, 2021

Copy link to clipboard

Copied

. length-1 is essentially the same as [-1]. I was thinking try paragraphs[-2], or try Peter's suggestion.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 13, 2021 0
Contributor ,
Jan 13, 2021

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";
            }
        }

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 13, 2021 0