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

Photoshop script - set multiple text color values

Community Beginner ,
Dec 04, 2021 Dec 04, 2021

Hi everyone,

Here is the thing:

 

1) Bunch of psd files. Each file has 1 text layer inside. Each file is named after a hex color code (example: #111111.psd, #00ff00.psd, #ab6fe2.psd etc., there are about 300 of them)

2) The script below is to colorize all the text layers at ones. I found it from other discussion here

 

The problem:

 - I need all psd files to have the text inside colored according to the file name of each psd. How to set the text color to be "taken" from the file name of each psd and not just set as one specific color? This script makes all text in all psd to have the same color, which is not what is needed.

 

Thanks in advance

 

var textColor = new SolidColor();

textColor.rgb.hexValue = "111111"; //set color


//select folder where PSDs are held

var selectFolder = Folder.selectDialog( "Please select folder of PSDs");
var files = selectFolder.getFiles("*.psd");

//get an array of all PSDs in the folder

var fileList = selectFolder.getFiles("*.psd");

//iterate through file list

for (var i = 0; i < files.length; i++) {
var doc = app.open(files[i]);
for (var j= 0; j < doc.artLayers.length; j++) {
var lyr = doc.artLayers[j];

 

if (lyr.kind == LayerKind.TEXT) {
var lyr = doc.artLayers[j];
lyr.textItem.color = textColor;
}
}

doc.close(SaveOptions.SAVECHANGES)

}

 

TOPICS
Actions and scripting
2.2K
Translate
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 , Dec 04, 2021 Dec 04, 2021

Try this slight modification to the original code from @Manan Joshi –

 

#target photoshop

// input folder
var selectFolder = Folder.selectDialog("Please select folder of PSDs");
// get an array of all PSDs in the folder
var files = selectFolder.getFiles("*.psd");
// iterate through file list
for (var i = 0; i < files.length; i++) {
    var doc = app.open(files[i]);
    // filename #ffffff.psd to hex ffffff
    var hexCode = app.activeDocument.name.replace(/\#/, '').replace(/\.[^\.]+$/, '');
   
...
Translate
Adobe
LEGEND ,
Dec 04, 2021 Dec 04, 2021
Translate
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
Community Beginner ,
Dec 04, 2021 Dec 04, 2021

Thanks, but this is where I got the above code from. And I also left same question there too, but thought it would be better idea to post it as separate discussion.

Translate
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
Community Expert ,
Dec 04, 2021 Dec 04, 2021

@default6keyane8hq42 wrote:

Thanks, but this is where I got the above code from. And I also left same question there too, but thought it would be better idea to post it as separate discussion.


 

Now the same question appears in two separate topic threads... Perhaps go to the original topic thread and change your post so that it points to this topic, so that you are not asking the same question in two separate places, which may waste the time of forum members answering something that has already been answered.

 

Please use the button to insert code text as javascript formatted code rather than plain text:

formatting-options.png

 

You would need to extract the hex code from the filename to a variable, then use the variable instead of the static code, similar to:

 

var hexCode = app.activeDocument.name.replace(/(?:\#)(.{6})(?:\.[^\.]+$)/, '$1');

 

Then you would need to use the variable to colour the text, perhaps something similar to:

 

eval('textColor.rgb.hexValue = "' + hexCode + '"');

 

It will not be that easy though, however I believe that is the general idea...

 

Translate
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
Community Expert ,
Dec 04, 2021 Dec 04, 2021

Try this slight modification to the original code from @Manan Joshi –

 

#target photoshop

// input folder
var selectFolder = Folder.selectDialog("Please select folder of PSDs");
// get an array of all PSDs in the folder
var files = selectFolder.getFiles("*.psd");
// iterate through file list
for (var i = 0; i < files.length; i++) {
    var doc = app.open(files[i]);
    // filename #ffffff.psd to hex ffffff
    var hexCode = app.activeDocument.name.replace(/\#/, '').replace(/\.[^\.]+$/, '');
    // call the function
    colorTextLayer(doc.layers);
    // close and save
    doc.close(SaveOptions.SAVECHANGES);
}
// end of script notification
app.beep();
alert('Finished!');

function colorTextLayer(layerCol) {
    var layerCount = layerCol.length;
    for (var j = layerCount - 1; j >= 0; j--) {
        var l = layerCol[j];
        if (l.typename === "LayerSet")
            colorTextLayer(layerCol[j].layers);
        else {
            var textColor = new SolidColor();
            // static value
            // textColor.rgb.hexValue = "ffffff"
            // run filename to hexCode variable string as a statement
            eval('textColor.rgb.hexValue = "' + hexCode + '"');
            if (l.kind === LayerKind.TEXT)
                l.textItem.color = textColor;
        }
    }
}

 

NOTE: There is no error checking to ensure that the filename will produce a valid hex colour code.

Translate
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
Community Beginner ,
Dec 05, 2021 Dec 05, 2021

OMG it worked!!! Perfectly! Thank you so much!

Translate
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
Community Expert ,
Dec 05, 2021 Dec 05, 2021
LATEST

Your welecome!

Translate
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