Skip to main content
Known Participant
October 27, 2022
Answered

Find a character style with a multiple color overrides and make it uniform

  • October 27, 2022
  • 3 replies
  • 1103 views

I'm not sure if this is possible with a script, it seems from what I've read that any scripted color search has to have pre-defined colors to look for. But here goes. I have a document with a lot of color overrides. There are dozens of different colors. In some instances, the color overrides were not uniformly applied. The last few characters of the string got a different color than the rest. For example:

Now, the good news is, the colored text does have a character style applied, "Colored Text". The problem is, there is one Colored Text character style, but lots of different colors. I.e., the set character color of the style is red, but in lots of cases it is then overridden, as in the image above.

 

Is there a way in scripting to find any string of text with "Colored Text" applied, ask "what is the color of the first character in the string?" And then apply that color to the rest of the characters in the found string? In other words, using the above image as an example, the script would find "sunto etusandiosam" and see that the "s" is pink, and apply that pink to the rest of the phrase.

 

(Because I know it's tempting to provide "best practice" advice, I'll just leave a note that I did not set this up, I'm just trying to fix it in the most efficient way :). )

This topic has been closed for replies.
Correct answer m1b

Ah I see! No problem—that's quite easy. Please try this script and let me know how it goes. - Mark

/**
 * Find character style and set fillColor
 * to the fillColor of the first character.
 * @7111211 m1b
 * @version 2022-10-29
 * @discussion https://community.adobe.com/t5/indesign-discussions/find-a-character-style-with-a-multiple-color-overrides-and-make-it-uniform/m-p/13304307#M499641
 */
function main() {

    var doc = app.activeDocument;

    // reset grep prefs
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    // find criteria
    app.findGrepPreferences.appliedCharacterStyle = 'Pink';

    // do the find
    var found = doc.findGrep();

    // set the fillColor to the first character's fillColor
    for (var i = 0; i < found.length; i++) {
        var text = found[i];
        text.fillColor = text.characters[0].fillColor;
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fix character style color');

 

3 replies

Community Expert
October 28, 2022

 

Find

Character Style 

Colour Text

 

Replace with

Character Style

Colour Text (same as you already have)

 

Change all.

 

m1b
Community Expert
Community Expert
October 27, 2022

Hi @defaultu0e43kqloi6n, I recently wrote a function to clear overrides for another user, and it'll be perfect for your needs I think (it really depends if the text has the character style applied—if it doesn't we have to do it another way).

Please try this and see what you think.

- Mark

function main() {

    var doc = app.activeDocument;

    // reset grep prefs
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    // find criteria
    app.findGrepPreferences.appliedCharacterStyle = 'Pink';

    clearTextOverridesInItems(doc.findGrep());

    /**
     * Clears style overrides from text
     * or textFrame(s) or story.
     * @author m1b
     * @version 2022-10-22
     * @param {PageItems} items - any indesign objects with 'texts' property.
     */
    function clearTextOverridesInItems(items) {

        if (!items.hasOwnProperty('0'))
            items = [items];

        var texts = [];

        for (var i = 0; i < items.length; i++)

            if (
                items[i].hasOwnProperty('texts')
                && items[i].texts[0].isValid
            ) {
                // note we want hard references here, because
                // the text may reflow after clearing overrides
                texts.push(resolve(items[i].texts[0].toSpecifier()));

            }

        for (var i = 0; i < texts.length; i++)
            texts[i].clearOverrides(OverrideType.ALL);

    };

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Clear text overrides');
Known Participant
October 28, 2022

Thanks, I'm sure I'll use this elsewhere sometime, looks like a useful script! But in this case, I don't actually want to clear the overrides. I want to ensure that for each set of characters with "Color Text" applied, all the characters in that set have the SAME override applied as the first character in the set. You could say I want to change the override color of certain characters in the set, not clear it. So in the image in my original post, it would change the last 2 letters in the colored text to pink, but the pink color itself is an override.

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
October 28, 2022

Ah I see! No problem—that's quite easy. Please try this script and let me know how it goes. - Mark

/**
 * Find character style and set fillColor
 * to the fillColor of the first character.
 * @7111211 m1b
 * @version 2022-10-29
 * @discussion https://community.adobe.com/t5/indesign-discussions/find-a-character-style-with-a-multiple-color-overrides-and-make-it-uniform/m-p/13304307#M499641
 */
function main() {

    var doc = app.activeDocument;

    // reset grep prefs
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    // find criteria
    app.findGrepPreferences.appliedCharacterStyle = 'Pink';

    // do the find
    var found = doc.findGrep();

    // set the fillColor to the first character's fillColor
    for (var i = 0; i < found.length; i++) {
        var text = found[i];
        text.fillColor = text.characters[0].fillColor;
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fix character style color');

 

Robert at ID-Tasker
Legend
October 27, 2022

If you need to just clear local overrides and set one color to all instances of the "Colored Text" CharacterStyle - just do Find & Change this CharacterStyle on itself.

 

If there is not too many different colors - you can do Find & Change - this time searching for "Colored Text" - but without specifying color - doing step-by-step changes to a new CharacterStyle - "Colored Text Red", etc. - skipping the ones that are "green", then do another one and change to "green" and so on.

 

But if there is too many colors and you need to have overrides anyway - or apply new CharacterStyles - based on the color of the first character - then only script would solve the problem. 

 

PC or Mac? 

Known Participant
October 28, 2022

"if there is too many colors and you need to have overrides anyway - or apply new CharacterStyles - based on the color of the first character - then only script would solve the problem."

 

Thanks for your suggestions but Yes, that is where I'm at at this point.