Skip to main content
dublove
Legend
December 13, 2025
Answered

How can this script read paragraph styles within the group?

  • December 13, 2025
  • 2 replies
  • 1022 views

Currently, it can only read the external B1.
A1 and A2 are not recognized.

makeDialog();
//cs will hold the result
var cs, ps;
function makeDialog() {
    //the dialog object
    var theDialog = app.dialogs.add({ name: "Test ", canCancel: true });
    //add a dropdown list object, stringList gets the name of every available character style
    //note this would not get grouped styles
    with (theDialog.dialogColumns.add()) {


        staticTexts.add({ staticLabel: "UP:  " });
        staticTexts.add({ staticLabel: "Below:  " });
        staticTexts.add({ staticLabel: "Change:  " });


    }
    //note this would not get grouped styles
    with (theDialog.dialogColumns.add()) {
        styleA = dropdowns.add({ stringList: app.activeDocument.paragraphStyles.everyItem().name, selectedIndex: 0, minWidth: 80 });
        styleB = dropdowns.add({ stringList: app.activeDocument.paragraphStyles.everyItem().name, selectedIndex: 0, minWidth: 80 });
        styleBN = dropdowns.add({ stringList: app.activeDocument.paragraphStyles.everyItem().name, selectedIndex: 0, minWidth: 80 });
    }


    if (theDialog.show() == true) {
        //get the result
        //cs = app.activeDocument.characterStyles.item(cs.selectedIndex);
        styleA = app.activeDocument.paragraphStyles.item(styleA.selectedIndex);
        styleB = app.activeDocument.paragraphStyles.item(styleB.selectedIndex);
        styleBN = app.activeDocument.paragraphStyles.item(styleBN.selectedIndex);
        theDialog.destroy();
    }
}

668.png

Correct answer m1b

The important difference is using the "paragraphStyles" property vs the "allParagraphStyles" property. The latter gives an Array of every paragraph style in the document. The former only returns the paragraphs styles on that level (eg. root level of the document or, if you are looking at a ParagraphStyleGroup, then the styles in that group).

 

So you can do this:

var doc = app.activeDocument;
var paragraphStyleNames = [];
for (var i = 0; i < doc.allParagraphStyles.length; i++)
    paragraphStyleNames.push(doc.allParagraphStyles[i].name);

 and use that list to populate your menu.

- Mark

 

P.S. I tried to use your actual code but it didn't work for me.

2 replies

rob day
Community Expert
Community Expert
December 13, 2025

Hi @dublove , Are you building or controling the styles via the script? If not there can be any number of nested styles and groups with the same name. What would you do with a case like this?:

 

Screen Shot 5.png

m1b
Community Expert
Community Expert
December 13, 2025

@dublove because A1 and A2 are inside a ParagraphStyleGroup. You need to get to them this way:

styleA1 = app.activeDocument.paragraphStyleGroups.itemByName('A').paragraphStyles.itemByName('A1');

 

or another way, which I prefer, is my little `getThing` function—just go back and look at almost any of my other scripts; I use it all the time, and it saves the hassle.

- Mark 

dublove
dubloveAuthor
Legend
December 13, 2025

HI m1b.

Thank you .

 

I didn't quite grasp your “getThing.”
It doesn't translate well to the new usage.

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
December 13, 2025

The important difference is using the "paragraphStyles" property vs the "allParagraphStyles" property. The latter gives an Array of every paragraph style in the document. The former only returns the paragraphs styles on that level (eg. root level of the document or, if you are looking at a ParagraphStyleGroup, then the styles in that group).

 

So you can do this:

var doc = app.activeDocument;
var paragraphStyleNames = [];
for (var i = 0; i < doc.allParagraphStyles.length; i++)
    paragraphStyleNames.push(doc.allParagraphStyles[i].name);

 and use that list to populate your menu.

- Mark

 

P.S. I tried to use your actual code but it didn't work for me.