• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Wrong paragraph style gets applied – appliedParagraphStyle

Enthusiast ,
Jan 12, 2021 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

264

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 12, 2021 Jan 12, 2021

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. 

Votes

Translate

Translate
Community Expert ,
Jan 12, 2021 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. 

Votes

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
community guidelines
Enthusiast ,
Jan 12, 2021 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?

Votes

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
community guidelines
Community Expert ,
Jan 13, 2021 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.

Votes

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
community guidelines
Enthusiast ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines
Community Expert ,
Jan 13, 2021 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.

Votes

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
community guidelines