Hi @turner111 You need to target specific CharacterStyleGroup(s), so something like this:
var doc = app.activeDocument;
// this line will throw error if either group or style is not in document
var myStyle = doc.characterStyleGroups.itemByName('TextStyles').characterStyles.itemByName('dropShadowBoldItalic');
Personally—and people have their own opinions about this—I prefer another approach which I use routinely in my scripts:
(function () {
var doc = app.activeDocument;
// use the getThing function to search the 'allCharacterStyles' list
var myStyle = getThing(doc.allCharacterStyles, 'name', 'dropShadowBoldItalic');
// will return nothing if it doesn't find the style
if (!myStyle)
return alert('myStyle is not in document.');
// your script here ...
})();
/**
* Returns a thing with matching property.
* If `key` is undefined, evaluate the object itself.
* @author m1b
* @version 2024-04-21
* @param {Array|Collection} things - the things to look through.
* @param {String} [key] - the property name (default: undefined).
* @param {*} value - the value to match.
* @returns {*?} - the thing, if found.
*/
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];
};
I just add the getThing function to the end of the script file and pass it `doc.allCharacterStyles` which is an array of every CharacterStyle in the document, regardless of groups. It picks out the one you want and if it doesn't find it, it returns nothing. So the next line you check if it exists and you are ready to move on with confidence.
So you can choose either way, or write a different function to search the character style groups. But that gives you the basics I think.
- Mark