Skip to main content
Iuigidesu
Inspiring
November 19, 2023
Answered

Color change script based on style

  • November 19, 2023
  • 1 reply
  • 554 views

Hey, this might be a rookie question, but i cant fix this, I got this script that changes the text color based on its style, really simple. Here it is for reference.

var doc = app.activeDocument;

function changeColorByFont(target) {
    var layers = target.layers;
    for (var i = 0; i < layers.length; i++) {
        if (layers[i].typename == "LayerSet") {
            changeColorByFont(layers[i]);
        } else if (layers[i].kind == LayerKind.TEXT) {
            var textItems = layers[i].textItem.contents.split('\r'); // Split text by line breaks
            for (var j = 0; j < textItems.length; j++) {
                var fontName = layers[i].textItem.font;
                switch (fontName) {
                    case "Bebas-Regular":  // Replace "Font1" with the actual name of your first font
                        layers[i].textItem.color.rgb.red = 255;  // Red color
                        break;
                    case "CCWildWords-Italic":  // Replace "Font2" with the actual name of your second font
                        layers[i].textItem.color.rgb.blue = 255;  // Blue color
                        break;
                    case "CCJScottCampbell-Italic":  // Replace "Font3" with the actual name of your third font
                        layers[i].textItem.color.rgb.black = 0;  // Black color
                        break;
                    // Add more cases for additional fonts and colors as needed
                }
            }
        };
    };
};

changeColorByFont(doc);

 It works just fine when dealing with just one single font per layer, but when i mix 2 or more fonts it kinda messes up the thing, heres a visual example.

Is there any way to make it check and change every font in a text layer instead of the most used? Thanks.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

For something like this you need to use AM code instead of DOM code. 

The code in this thread might serve to illustrate the principle and you could adapt it. 

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
November 19, 2023

For something like this you need to use AM code instead of DOM code. 

The code in this thread might serve to illustrate the principle and you could adapt it. 

Iuigidesu
IuigidesuAuthor
Inspiring
December 3, 2023

Yeah that worked, I stole most part of that code and merged it into my old code and it works now, thanks.