Skip to main content
dublove
Legend
June 20, 2025
Answered

How to create a new “abc” paragraph style, at the same time new to the A style group?

  • June 20, 2025
  • 1 reply
  • 405 views

First judge whether there is an "A" style group, if there is no abc paragraph style in the group, then create a new "A" style group ("abc" paragraph style).

How do I modify this?

Thank you very much.

    if (!app.documents[0].paragraphStyles.item(abc).isValid) {
        app.documents[0].paragraphStyles.add({ name: abc});
    }

 

Correct answer Manan Joshi

So you mean both the Paragraph Style group and Paragraph Style should be created if not present right?

-Manan


Try this it will create both group and style if anyone of those or both are missing

var doc = app.documents[0];
var groupName = "A";
var styleName = "abc";

// Get the group by name
var group = doc.paragraphStyleGroups.itemByName(groupName);

// If group exists, check or create style
if (!group.isValid)
    group = doc.paragraphStyleGroups.add({ name: groupName })

if (!group.paragraphStyles.itemByName(styleName).isValid)
    group.paragraphStyles.add({ name: styleName });

-Manan

1 reply

Community Expert
June 20, 2025

Try the following

var doc = app.documents[0];
var styleName = "abc";

// Get the group by name
var group = doc.paragraphStyleGroups.itemByName("A");

// If group exists, check or create style
group.isValid && !group.paragraphStyles.itemByName(styleName).isValid &&
    group.paragraphStyles.add({ name: styleName });

This will check if there is a style named abc in a group named A. In not then it will create the Paragraph Style. I am not sure if you want Paragraph Style group to be created as well or not.

-Manan

-Manan
dublove
dubloveAuthor
Legend
June 20, 2025
var doc = app.activeDocument;
var grName = "A";
var styleName = "abc";

// Get the group by name
var group = doc.paragraphStyleGroups.itemByName(grName);
// If group exists, check or create style
if (!group.isValid && !group.paragraphStyles.itemByName(styleName).isValid) {
    doc.paragraphStyleGroups.add({ name: grName });
    group.paragraphStyles.add({ name: styleName });
}
else if (group.isValid && !group.paragraphStyles.itemByName(styleName).isValid) {
    group.paragraphStyles.add({ name: styleName });
}