Skip to main content
February 8, 2016
Question

getting the names of paragraph styles in selection

  • February 8, 2016
  • 1 reply
  • 3302 views

Seems like this ought to work:

app.selection[0].paragraphs.everyItem().appliedParagraphStyle.name;

or

app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle.name;

(assuming, of course, that some text is selected, which I've already checked).

But both snippets return "undefined." If I don't specify the name, I get an array of paragraph styles and so can loop through to extract the names. But I just wanted to check here first to see if I'm missing something obvious.

This topic has been closed for replies.

1 reply

Loic.Aigon
Legend
February 8, 2016

Look at the constructor and you may understand what the name property returns undefined

app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle.constructor.name

Loic

www.ozalto.com

February 8, 2016

I get that it's an array. But consider:

app.documents[0].paragraphStyles.everyItem();

returns a paragraph style object so

app.documents[0].paragraphStyles.everyItem().names

returns an array of style names

I think Adobe's selection object is weaker for this kind of implementation. We're not all writing big robotic scripts that create new documents from scratch, lay them out and send them to the printer. I write a lot of smaller toolbox style scripts that have to work with whatever the user clicked on before running the script.

Loic.Aigon
Legend
February 8, 2016

app.documents[0].paragraphStyles.everyItem();

returns a paragraph style object so

Not exactly. What you might need to understand here is that app.documents[0].paragraphStyles is a collection, not an array. And everyItem() will consider all the items of the collection.

So

var doc = app.activeDocument;

var pss = doc.paragraphStyles;

pss.everyItem().constructor.name // ParagraphStyle

The good point is that collection are sometimes way cooler than arrays because you can tweak all the items without the need of a loop.

var doc = app.activeDocument;

var pss = doc.paragraphStyles;

pss.everyItem().pointSize = 12; //will make all paragraph style point size to 12

But if you try to change the name for example, you will get an error of course

var doc = app.activeDocument;

var pss = doc.paragraphStyles;

pss.everyItem().name = "Let's make InDesign yelling";

So when you change a property through everyItem(), this property will apply to every item in the collection.

However, when you retrieve a common property, you actually get an array. That's really logical because you just grab some data for multiple objects.

In case you do need true arrays of objects, you can use "getElements()" will which throw all the collections items in a form of an array:

var doc = app.activeDocument;

var pss = doc.paragraphStyles;

pss.everyItem().getElements().constructor.name; //Array

HTH,

Loic

www.ozalto.com