Skip to main content
brian_p_dts
Community Expert
Community Expert
May 24, 2013
Question

Checking whether Paragraph Style name exists in document

  • May 24, 2013
  • 1 reply
  • 2569 views

In JS, I have an object of shorthand keys and paragraph style names, like so:

  var styles = {

     BDY: "Body Text",

      HED1: "Headline 1",

     HED2: "Headline 2",

     etc. 

  }

Is there a way to quickly check whether a name is present in the document, without having to loop through all the styles in the document?

I tried something like:

var docStyles = doc.paragraphStyles;    

for ( var key in styles ) {

          if ( typeof docStyles[ styles[key] ] == "undefined" ) ...

   }

But the invalid name still is defined, it's still an instanceof PargraphStyle, but of course, if I try to use it later in the script, I get an error.

Any thoughts, or do I have to call allParagraphStyles, and loop through that to figure out if the name matches one of those in the list?

This topic has been closed for replies.

1 reply

Inspiring
May 24, 2013

Hi,

this'll check only pStyleNames that are not in Groups. You'll get those by 'allParagraphStyles' (as already mentioned and will return a array) or recursiley thru pStyleGroups where youo could work with collections.

var styles = {

     BDY: "Body Text",

      HED1: "Headline 1",

     HED2: "Headline 2",

  }

for ( var key in styles ) {

          if ( app.activeDocument.paragraphStyles.everyItem().name.join().indexOf(styles[key]) == -1)

          {alert('pStyle: ' + styles[key] + ' may exist in Group, but not plain ...')}

   }

brian_p_dts
Community Expert
Community Expert
May 24, 2013

Worked like a charm. Thank you very much.

Vamitul
Legend
May 24, 2013

another quick and dirty way:

myDoc.paragraphStyles.itemByName(styles.BDY).isValid