Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can I ensure newly created object styles are unique?

Guide ,
Oct 02, 2025 Oct 02, 2025

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?

TOPICS
How to , Scripting
319
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 02, 2025 Oct 02, 2025

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
}

 

Screen Shot 14.png

Translate
Guide ,
Oct 02, 2025 Oct 02, 2025

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

 

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 02, 2025 Oct 02, 2025
Thank you very much. I remember there was an allparagraphs
alert(app.documents[0].allObjectStyles.itemByName(“a1”))
Still need to iterate?
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2025 Oct 02, 2025

Still need to iterate?

 

Yes, allObjectSyles is an Array not a Collection—an Array has no itemByName function.

 

Your alert(app.documents[0].allObjectStyles.itemByName(“a1”)) would throw an error:

 

Screen Shot 9.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 02, 2025 Oct 02, 2025

I feel the execution efficiency below is a bit low:

if(app.documents[0].objectStyleGroups.itemByName(“AA”).objectStyles.itemByName(“a1”).isValid)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2025 Oct 02, 2025

That would also throw an error if the AA group does not exist.

 

Screen Shot 11.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 02, 2025 Oct 02, 2025

How to efficiently determine if there is an a1?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2025 Oct 02, 2025

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
}

 

Screen Shot 14.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 02, 2025 Oct 02, 2025

Thanks rob day.

This one seems to work too.
It just doesn't feel quite as wonderful.

var obs = app.documents[0].allObjectStyles;
var have= 0;
for (var i = 0; i < obs.length; i++) {
    if (obs[i].name == "a1") {
        have= 1;
    }
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 02, 2025 Oct 02, 2025

After seeing your examples, I seem to be starting to like “return”.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2025 Oct 02, 2025

Right, the advantage of a function with a returned value is you can call it with a single line of code as many times as needed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 02, 2025 Oct 02, 2025

@Manan Joshi

@rob day 

I want to set the attribute as a parameter again. Similar to this previous post.
Seems like it won't work.

https://community.adobe.com/t5/indesign-discussions/how-to-transfer-myparstn-to-n-in-myjson-n-v-chan...

I want allCellStyles, allObjectStyles, and allParagraphs to share a single function.
Like this:

alert(checForOS("a1",allCellStyle))

function checForOS(n,ty){
    var os = app.documents.item(0).[ty];
    var b = false
    for (var i = 0; i < os.length; i++){
        if (os[i].name == n) {
            b = true
        } 
    }; 
    return b
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2025 Oct 02, 2025

@dublove your function won't work like that. It should be like this:

alert(checkForOS("a1", "allCellStyles"));

function checkForOS(n, ty) {
    var os = app.documents.item(0)[ty];  // no dot before [ty]!
    for (var i = 0; i < os.length; i++) {
        if (os[i].name == n) {
            return true;
        }
    };
    return false;
}

 

but, in my opinion, a better approach would be:

var a1CellStyle = getThing(app.activeDocument.allCellStyles, "name", "a1");
alert(a1CellStyle != undefined);

function getThing(things, key, value) {

    for (var i = 0; i < things.length; i++)
        if ((undefined == key ? things[i] : things[i][key]) == value)
            return things[i];

};

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 03, 2025 Oct 03, 2025

@m1b 

I don't understand this line:

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

Also, what does “key” refer to?

What exactly does “name” refer to?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 03, 2025 Oct 03, 2025

`key` is the second parameter. In this case we pass it the string "name" because we want to match the name property of the CellStyle.

 

For your self, you could simplify the function:

function getThing(things, key, value) {

    for (var i = 0; i < things.length; i++)
        if (things[i][key] == value)
            return things[i];

};
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 03, 2025 Oct 03, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2025 Oct 11, 2025
LATEST

@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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2025 Oct 02, 2025

By the way, @dublove go back and read my old code I've given you. There is a reason I like to use the getThing function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 02, 2025 Oct 02, 2025

@m1b Yes, a well-designed function can save a lot of trouble.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines