Copy link to clipboard
Copied
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?
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
}
Copy link to clipboard
Copied
You could use allObjectStyles to get an array, then check the array.
P.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
I feel the execution efficiency below is a bit low:
if(app.documents[0].objectStyleGroups.itemByName(“AA”).objectStyles.itemByName(“a1”).isValid)
Copy link to clipboard
Copied
That would also throw an error if the AA group does not exist.
Copy link to clipboard
Copied
How to efficiently determine if there is an a1?
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
After seeing your examples, I seem to be starting to like “return”.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I want to set the attribute as a parameter again. Similar to this previous post.
Seems like it won't work.
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
}
Copy link to clipboard
Copied
@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];
};
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
`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];
};
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@m1b Yes, a well-designed function can save a lot of trouble.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now