Skip to main content
dublove
Legend
June 18, 2026
Answered

Why can't I detect the target style in the group?

  • June 18, 2026
  • 2 replies
  • 26 views

I want to check if the 'myPar' paragraph style exists.
When it is in the group, return true
But when it is within a certain group, it is not recognized.
Where is the problem?


var ifStyEx = styleExist("myPar", "allParagraphStyles")
//Check if the style exists
function styleExist(stn, astc) {
var d = app.activeDocument;
for (var i = 0; i < d[astc].length; i++) {
if (d[astc][i].name == stn) {
//style = d[astc][i];
//break;
return true;
}
else {
return alert("No exist");
}
}
}

 

    Correct answer rob day

    Not sure you need the 2nd parameter?

     

    var ifStyEx = styleExist("myPar")
    alert(ifStyEx)

    function styleExist(stn) {
    var d = app.activeDocument.allParagraphStyles;
    for (var i = 0; i < d.length; i++) {
    if (d[i].name == stn) {
    return true
    break
    }
    }
    return false;
    }

     

     

     

    2 replies

    Community Expert
    June 18, 2026

    It is because your else is misplaced. The first entry for which the name does not match you are returning false. Use the following

    var ifStyEx = styleExist("myPar", "allParagraphStyles")
    //Check if the style exists
    function styleExist(stn, astc) {
    var d = app.activeDocument;
    for (var i = 0; i < d[astc].length; i++) {
    if (d[astc][i].name == stn) {
    //style = d[astc][i];
    //break;
    return true;
    }
    }
    return false;
    }

     

    -Manan
    Community Expert
    June 18, 2026

    The problem was the else inside your loop. In the original at line 11 as soon as the first style was not myPar, so it never checked the remaining styles.

    So this searches for
    styles directly in the document
    styles inside paragraph style groups
    styles inside nested paragraph style groups

    var ifStyEx = styleExist("myPar");
    alert(ifStyEx);

    // Check if a paragraph style exists, including styles inside nested groups.
    function styleExist(styleName) {
    return styleExistsInContainer(app.activeDocument, styleName);
    }

    function styleExistsInContainer(container, styleName) {
    var i;
    var styles = container.paragraphStyles;

    for (i = 0; i < styles.length; i++) {
    if (styles[i].name == styleName) {
    return true;
    }
    }

    var groups = container.paragraphStyleGroups;
    for (i = 0; i < groups.length; i++) {
    if (styleExistsInContainer(groups[i], styleName)) {
    return true;
    }
    }

    return false;
    }

     

    dublove
    dubloveAuthor
    Legend
    June 18, 2026

    @Eugene Tyson 

    Thanks .
    Your one is complicated, mine is simple.

    I found this feasible.


    var ifStyEx = styleExist("myPar", "allParagraphStyles")
    alert(ifStyEx)
    //Check if the style exists
    function styleExist(stn, astc) {
    var ex = 0;
    var d = app.activeDocument;
    for (var i = 0; i < d[astc].length; i++) {
    if (d[astc][i].name == stn) {
    //style = d[astc][i];
    //break;
    ex = +1;
    }
    }
    return ex;
    }

    or 

     var ex = false;

     var ex = true;

    Community Expert
    June 18, 2026

    Your version is not optimal. Once you get the style i.e. you are in the if body there is no need to waste computation on executing the remaining iteration of the for loop. You should break out immediately. Use the version I gave.

    -Manan