Skip to main content
Inspiring
July 28, 2025
Answered

FindReplaceByList - but search targets are styles in folders...

  • July 28, 2025
  • 1 reply
  • 199 views

Running a script to find and replace based on a Character Style, and it works fine if the Style isn't in a folder.

However... in a folder... not so much!

 

What I have now:*

appliedCharacterStyle:"dropShadowBoldItalic"

 

But if I put that Charactrer Style in the folder "Text Styles", it doesn't work, and I haven't figured out how to get it to work. 

I tried:
"Text Styles:dropShadowBoldItalic",

which I saw somewhere, but didn't work. Couldnt' find any other reference for doing something like this, so hoping there's a standard syntax that works.

Thanks!

Bonus question: is there an offical Adobe GREP reference?

 

*Ok, I'm not really using bold italic with a drop shadow

Correct answer m1b

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

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 29, 2025

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

turner111Author
Inspiring
August 5, 2025

Sorry - thought I replied to this!

Anyway - thanks, I'll check it out. I don't do much scripting, so may be back with a question or two...

cheers