Skip to main content
dublove
Legend
Sep 13, 2026 6:10 AM UTC
Answered

Can only determine non existence, but cannot determine non existence?

I need to determine if a paragraph style group exists. Generally, this avoids the judgment of non existence:


//This is the previous practice of using else to determine 'non-existent' in reverse
var d = app.activeDocument;
var item = d.selection[0];
if(d.paragraphStyleGroups.itemByName('AA').isValid){
var myGroup=d.paragraphStyleGroups.itemByName('AA');
alert('AA exist')
}
else{
alert('AA dose not exist')
}

But sometimes I need to directly determine that it doesn't exist and find that both of the following are invalid:

//Now I want to directly determine that 'AA' does not exist.
var d = app.activeDocument;
var item = d.selection[0];
if(d.paragraphStyleGroups.itemByName('AA').inValid){
alert('group does not exist');
}

if(!d.paragraphStyleGroups.itemByName('AA').isValid){
alert('group does not exist');
}

 

    rob day
    rob dayCorrect answer
    Community Expert
    Sep 13, 2026 10:07 PM UTC

    My code is not working.

     

    I think you need a recursive function than calls itself because nested style groups can be any number of groups deep, so something like this.

     

    //an array of all paragraph style names
    var g = GetOSGroups(app.activeDocument)
    alert("All Paragraph Style Groups " + g)
    //Check if style named AAA exists—returns true
    alert("Item named AAA exists: " + checkItem(g, "AAA"))


    /**
    * Get an array of all paragraph style group names—function calls itself to get nested groups
    * @ param the document to check
    * @ returns an array of the document’s paragraph group names
    */
    function GetOSGroups(f) {
    var a = []
    var osg = f.paragraphStyleGroups.everyItem().getElements()
    var gr
    for (var i = 0; i < osg.length; i++) {
    gr = osg[i];
    if (gr.paragraphStyleGroups.everyItem().getElements().length > 0) {
    a.push(gr.name)
    a = a.concat(GetOSGroups(gr))
    } else {
    a.push(gr.name)
    }
    }
    return a
    }


    /**
    * Checks if an item is in an array
    * @ param the array to check
    * @ param the item to search for
    * @ return true if the item is in the array
    */
    function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
    if (a[i] === obj) {
    return true;
    }
    }
    return false;
    }

     

    Result:

     

     

    11 replies

    Community Expert
    Sep 13, 2026 6:15 AM UTC

    What do you mean ​@dublove? Your title is confusing you say you can and then you say you can’t. Add more details, probably a file, code snippet, what you get and what you want would help in leading you to an answer.

    Translate
    -Manan
    dublove
    dubloveAuthor
    Legend
    Sep 13, 2026 6:39 AM UTC

    Hi ​@Manan Joshi 

    Sorry, my expression was a bit unclear.

    I have updated the initial post.
    Simply put:How do you directly determine that the AA paragraph style group does not exist?

    Translate
    Community Expert
    Sep 13, 2026 6:46 AM UTC

    Ok, so your code should work. Unless the group is inside another group. Can you provide a sample file and let me know what does not work for you?

    Translate
    -Manan