Skip to main content
dublove
Legend
October 4, 2025
Answered

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

  • October 4, 2025
  • 1 reply
  • 211 views

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])
};
}
Correct answer Manan Joshi

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

1 reply

Community Expert
October 5, 2025

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

-Manan
dublove
dubloveAuthor
Legend
October 7, 2025

Sorry. @Manan Joshi 

I've updated the original post.

Manan JoshiCommunity ExpertCorrect answer
Community Expert
October 21, 2025

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