Skip to main content
dublove
Legend
October 2, 2025
Answered

How can I ensure newly created object styles are unique?

  • October 2, 2025
  • 1 reply
  • 630 views

Object styles seem to allow duplicate names as long as they're in different groups.
The condition

"if(!d.objectStyles.itemByName(a1).isValid)" 

doesn't appear to check for "a1" within its own group.

Instead, it requires this approach:

if(!d.objectStyleGroups.itemByName(“AA”).objectStyles.itemByName(a1).isValid))

 

Since I don't know all group names, is there a way to check if the style `a1` exists in any group or outside all groups?

Correct answer rob day

How to efficiently determine if there is an a1?


With an Array you have to loop, something like this

 

alert(checForOS("a1"))
  
/**
* Check if an object style named n exists 
* @ param n 
* @ return true if object style named n exists * 
*/
function checForOS(n){
    var os = app.documents.item(0).allObjectStyles;
    var b = false
    for (var i = 0; i < os.length; i++){
        if (os[i].name == n) {
            b = true
        } 
    }; 
    return b
}

 

1 reply

Legend
October 2, 2025

You could use allObjectStyles to get an array, then check the array.

 

P.

dublove
dubloveAuthor
Legend
October 2, 2025

I feel the execution efficiency below is a bit low:

if(app.documents[0].objectStyleGroups.itemByName(“AA”).objectStyles.itemByName(“a1”).isValid)
m1b
Community Expert
Community Expert
October 12, 2025

Hi m1b.

I now understand the meaning of “key”.
In this example, the key attribute we are checking is “name”.

 

Could you translate the meaning of the sentence below?

if ((undefined == key ? things[i] : things[i][key]) == value)

 Thank you.


@dublove this line

if ((undefined == key ? things[i] : things[i][key]) == value)

returns true if `value` matches things[i][key] *if `key` exists`* but if key doesn't exist (is undefined) it matches things[i] alone to value. It means I can pass it an array of some object and search for a know object inside it, so I would do:

var myObj = getThing(allObjs, undefined, someObj) || defaultObj;

In your case you don't really need this functionality. It's just a shortcut to make the function a bit more useful for me. I rarely use it though.

- Mark