Look at the constructor and you may understand what the name property returns undefined
I get that it's an array. But consider:
returns a paragraph style object so
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.
returns a paragraph style object so
Not exactly. What you might need to understand here is that app.documents.paragraphStyles is a collection, not an array. And everyItem() will consider all the items of the collection.
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
OK, so now I understand that "app.selection.paragraphs.everyItem()" is an array, and I was expecting it to be a collection. I'll freely admit that I did not understand the difference between the two. Now that I have been educated on that point, I can't help but wonder why. Seems that app.selection would be more useful if it were a collection.
But the main point is: To get the names of the paragraph styles in the currently selected type, there is no alternative to a loop.
I may have misexplained myself.
" I understand that "app.selection.paragraphs.everyItem()" is an array, and I was expecting it to be a collection."
No it's the opposite. app.selection is be an array (be aware that selection returns null if no documents open). But since you adress paragragraphs objects, you get a collection.
"Seems that app.selection would be more useful if it were a collection."
Well think of a selection made of some text frames, some rectangles and some multimedia stuff, what would be the collection type here ? Collection only apply when you retrieve objects of a same type through something like app.documents…
Hi. I am trying to get paragraph styles first time in my life and just getting undefined when call app.activeDocument.paragraphStyles.
Maybe you know why it happens? Photoshop 2018. Maybe Adobe removed that variable at all? Cannot find paragraphStyles in documentation as well(((
Hi bodich ,
InDesign is not PhotoShop.
To get all paragraph styles of the active document written to an array do:
// Returns array of paragraph styles. Not a collection: var allParaStylesOfDoc = app.activeDocument.allParagraphStyles;
FWIW: Also paragraph styles in paragraph style groups are written to this array.