Skip to main content
dublove
Legend
Oct 4, 2025 6:11 AM UTC
Answered

Can this be changed to a shared function that works for multiple instances as well as a single one?

I don't want to nest “a1” and “a2” inside it.
Because later, I also need to create separate paragraph styles “b1” and “b2” within that group.

Is it possible to achieve something like this:

`setParGroup([“b1”,“b2”])`

That is, maintain the ability to create multiple styles at once while also allowing creation of a single style.
Both should share the same

 

SetParGroup1();
SetParGroup2();
function SetParGroup1() {
    var wn = ["a1","a2"];
    for (var i = 0; i < wn.length; i++) {
        w = newParGroStyle(app.activeDocument, magGroParStn, wn[i])
    };
}
function SetParGroup2() {
    var wn = ["b1","b2"];
    for (var i = 0; i < wn.length; i++) {
        w = newParGroStyle(app.activeDocument, magGroParStn, wn[i])
    };
}
function newParGroStyle(d, g, n,) {
    // Get the group by name
    var group = d.paragraphStyleGroups.itemByName(g);
    // If group exists, check or create style
    if (!group.isValid)
        group = d.paragraphStyleGroups.add({ name: g })
    if (!group.paragraphStyles.itemByName(n).isValid)
        group.paragraphStyles.add({ name: n });
}

Specifically, can the following section share a single instance?

function SetParGroup1() {
var wn = [“a1”,“a2”];
for (var i = 0; i < wn.length; i++) {
w = newParGroStyle(app.activeDocument, magGroParStn, wn[i])
};
}
Manan JoshiCorrect answer
Community Expert
Oct 21, 2025 3:16 AM UTC

I am not sure if I am still understanding it correct but how about the following code

function SetParGroup(wn) {
    for (var i = 0; i < wn.length; i++) {
        w = newParGroStyle(app.activeDocument, magGroParStn, wn[i])
    };
}

function newParGroStyle(d, g, n,) {
    // Get the group by name
    var group = d.paragraphStyleGroups.itemByName(g);
    // If group exists, check or create style
    if (!group.isValid)
        group = d.paragraphStyleGroups.add({ name: g })
    if (!group.paragraphStyles.itemByName(n).isValid)
        group.paragraphStyles.add({ name: n });
}

SetParGroup(["a1","a2", "b1", "b2"]);

-Manan

-Manan

4 replies

Community Expert
Oct 5, 2025 1:38 AM UTC

Hi @dublove,

Please give more context, I am unable to understand your ask. Like we don't know what newParGroStyle method does, what is magGroParStn, what it is doing currently and what you want it to do.

-Manan

Translate
-Manan
dublove
dubloveAuthor
Legend
Oct 7, 2025 2:44 PM UTC

Sorry. @Manan Joshi 

I've updated the original post.

Translate
Manan JoshiCommunity ExpertCorrect answer
Community Expert
Oct 21, 2025 3:16 AM UTC

I am not sure if I am still understanding it correct but how about the following code

function SetParGroup(wn) {
    for (var i = 0; i < wn.length; i++) {
        w = newParGroStyle(app.activeDocument, magGroParStn, wn[i])
    };
}

function newParGroStyle(d, g, n,) {
    // Get the group by name
    var group = d.paragraphStyleGroups.itemByName(g);
    // If group exists, check or create style
    if (!group.isValid)
        group = d.paragraphStyleGroups.add({ name: g })
    if (!group.paragraphStyles.itemByName(n).isValid)
        group.paragraphStyles.add({ name: n });
}

SetParGroup(["a1","a2", "b1", "b2"]);

-Manan

Translate
-Manan