Skip to main content
Inspiring
September 13, 2016
Answered

looping through paragraph styles

  • September 13, 2016
  • 2 replies
  • 2655 views

Hello,

I'm trying to loop through the paragraph styles in my selection with this script:

var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;

for (var i=0;i<styles;i++) {
    styles.name;
    }

It just returns undefined.

I'd also like to be able to loop through all the applied paragraph styles in a document (to find out the names).

Any suggestions would be great!

Thanks

This topic has been closed for replies.
Correct answer Sajeev Sridharan

Select some paragraphs and try the below code:

var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;

for (var i=0;i<styles.length;i++) {//.length missing

    $.writeln(styles.name);

    }

I'd also like to be able to loop through all the applied paragraph styles in a document (to find out the names).

var styles = app.activeDocument.textFrames.everyItem().textStyleRanges.everyItem().appliedParagraphStyle;

for (i=0; i<styles.length; i++)

{

     $.writeln(styles.name);

}

2 replies

Jongware
Community Expert
Community Expert
September 13, 2016

You are just 'saying' the name. It's like saying '42;' in the middle of a script - what is it supposed to do? If you change that line to something that shows the name, you can see it works.

You also cannot 'loop' over a plain array! You need styles.length here:

var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;

for (var i=0;i<styles.length;i++) {
    alert (styles.name);
    }

Finally, beware that 'textStyleRanges' may return many items for one and the same paragraph, but its associated paragraph style will still be the same. So even if you only have selected a single paragraph, you would get its style name reported once for every italic word, every differently colored word, every custom tracked phrase ... and once for all 'regular' texts in between.

Inspiring
September 13, 2016

Thanks for your replies

Just out of curiosity, how could I avoid getting every instance of the paragraph style? Is there another property which returns just one style when it comes across many?

Legend
September 13, 2016

Could you explain more precisely.

Sajeev SridharanCorrect answer
Legend
September 13, 2016

Select some paragraphs and try the below code:

var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;

for (var i=0;i<styles.length;i++) {//.length missing

    $.writeln(styles.name);

    }

I'd also like to be able to loop through all the applied paragraph styles in a document (to find out the names).

var styles = app.activeDocument.textFrames.everyItem().textStyleRanges.everyItem().appliedParagraphStyle;

for (i=0; i<styles.length; i++)

{

     $.writeln(styles.name);

}