Skip to main content
dublove
Legend
June 10, 2026
Answered

How to move paragraph styles into a group?

  • June 10, 2026
  • 1 reply
  • 23 views

I want to create new paragraph styles named “a1” and ‘a2’ under the “AA” group.
When creating them:

I’ll first check if “a1” and ‘a2’ already exist; if so, I won’t create them again, but I’ll move them into the “AA” group.

If a1 and a2 do not exist, create them.

 

    Correct answer Manan Joshi

    Try the following

    (function () {
    var doc;
    try { doc = app.activeDocument; } catch (e) { alert("Open a document first."); return; }

    var targetGroupName = "AA";
    var styleNames = ["a1", "a2"];

    // Get or create top-level paragraph style group by name
    function getOrCreateGroup(name) {
    var grp = doc.paragraphStyleGroups.itemByName(name);
    if (grp.isValid) return grp;
    return doc.paragraphStyleGroups.add({ name: name });
    }

    // Move style into group (or duplicate if move not allowed)
    function placeStyleInGroup(styleName, group) {
    var style = null;
    for(var i = 0; i < doc.allParagraphStyles.length; i++) {
    if(doc.allParagraphStyles[i].name == styleName) {
    style = doc.allParagraphStyles[i];
    break;
    }
    }

    if (style && style.isValid) {
    if (style.parent.id === group.id) return;
    try {
    style.move(LocationOptions.AT_END, group);
    } catch (e) {
    alert("Could not move style")
    }
    } else {
    // Create under group
    try {
    group.paragraphStyles.add({ name: styleName });
    } catch (e) {
    // fallback: create at doc level then move
    try {
    var ns = doc.paragraphStyles.add({ name: styleName });
    ns.move(LocationOptions.AT_END, group);
    } catch (ee) { alert("Failed to create style '" + styleName + "': " + ee.message); }
    }
    }
    }

    var targetGroup = getOrCreateGroup(targetGroupName);
    for (var i = 0; i < styleNames.length; i++) placeStyleInGroup(styleNames[i], targetGroup);

    alert("Done.");
    })();

     

    1 reply

    Manan JoshiCommunity ExpertCorrect answer
    Community Expert
    June 10, 2026

    Try the following

    (function () {
    var doc;
    try { doc = app.activeDocument; } catch (e) { alert("Open a document first."); return; }

    var targetGroupName = "AA";
    var styleNames = ["a1", "a2"];

    // Get or create top-level paragraph style group by name
    function getOrCreateGroup(name) {
    var grp = doc.paragraphStyleGroups.itemByName(name);
    if (grp.isValid) return grp;
    return doc.paragraphStyleGroups.add({ name: name });
    }

    // Move style into group (or duplicate if move not allowed)
    function placeStyleInGroup(styleName, group) {
    var style = null;
    for(var i = 0; i < doc.allParagraphStyles.length; i++) {
    if(doc.allParagraphStyles[i].name == styleName) {
    style = doc.allParagraphStyles[i];
    break;
    }
    }

    if (style && style.isValid) {
    if (style.parent.id === group.id) return;
    try {
    style.move(LocationOptions.AT_END, group);
    } catch (e) {
    alert("Could not move style")
    }
    } else {
    // Create under group
    try {
    group.paragraphStyles.add({ name: styleName });
    } catch (e) {
    // fallback: create at doc level then move
    try {
    var ns = doc.paragraphStyles.add({ name: styleName });
    ns.move(LocationOptions.AT_END, group);
    } catch (ee) { alert("Failed to create style '" + styleName + "': " + ee.message); }
    }
    }
    }

    var targetGroup = getOrCreateGroup(targetGroupName);
    for (var i = 0; i < styleNames.length; i++) placeStyleInGroup(styleNames[i], targetGroup);

    alert("Done.");
    })();

     

    -Manan
    dublove
    dubloveAuthor
    Legend
    June 10, 2026

    Hi Manan Joshi.

    This is so helpful to me.
    Thank you very much.