Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
1

Color change script based on style

Explorer ,
Nov 18, 2023 Nov 18, 2023

Copy link to clipboard

Copied

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.

Iuigidesu_0-1700369510183.pngIuigidesu_1-1700369537288.png

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

TOPICS
Actions and scripting

Views

332
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 19, 2023 Nov 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. 

Votes

Translate
Adobe
Community Expert ,
Nov 19, 2023 Nov 19, 2023

Copy link to clipboard

Copied

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. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 03, 2023 Dec 03, 2023

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines