Skip to main content
Known Participant
January 27, 2023
Question

Migrated Content

  • January 27, 2023
  • 2 replies
  • 1705 views

a

This topic has been closed for replies.

2 replies

Known Participant
January 28, 2023

This script changes the color of the text in the active layer to black, exports it as a PNG file with the specified name, then it changes the color of the text in the active layer to white, and exports it again as a PNG file with the specified name, and it repeats the same process with different colors (pink, blue, green, red) and save them as PNG files with different names. 

 

The Black, Blue, Green and Red are exported correctly. However white (which comes right after black) is exported as light blue and the pink file (which comes after white) is exported as the same color of the blue file.

 

Here’s the code: var textColor = new SolidColor();
textColor.rgb.hexValue = “000000”;
app.activeDocument.activeLayer.textItem.color = textColor;

var textLayerName = app.activeDocument.activeLayer.name;

var newName = prompt(“Enter a new name for the export”, “black_” + textLayerName);

var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.quality = 100;
var file = new File("black " + newName + “.png”);
app.activeDocument.exportDocument(file, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “FFFFFF”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file2 = new File("white " + newName + “.png”);
app.activeDocument.exportDocument(file2, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “fc82d8”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file3 = new File("pink " + newName + “.png”);
app.activeDocument.exportDocument(file3, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “63a8e7”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file4 = new File("blue " + newName + “.png”);
app.activeDocument.exportDocument(file4, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “0c6a27”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file5 = new File("green " + newName + “.png”);
app.activeDocument.exportDocument(file5, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = " a80000";
app.activeDocument.activeLayer.textItem.color = textColor;

var file6 = new File("red " + newName + “.png”);
app.activeDocument.exportDocument(file6, ExportType.SAVEFORWEB, exportOptions);

Legend
January 28, 2023
Maybe the problem is in your file. Blend mode or transparency or layer effects. If you manually set the text color to white, what do you see.
 
Known Participant
January 28, 2023

I manually set the color of the background to white and it still does the same thing. It's strange because the first color is exported correctly, then white and pink which are the second and third operations respectively are exported as light blue (a color that i've not even put in the code) and blue (which is the blue used to export the "blue_filename" file). Meanwhile the 4th, 5th and 6th operation, so blue, green and red are exported in the right color.

Legend
January 27, 2023

First of all, I recommend putting some error-trapping code in so you can get some feedback and what is not working. Next, I'd check to confirm that the active layer is a text layer.

 

        try{
            var docRef = activeDocument;
            for(var i = 0; i < docRef.artLayers.length; i++){
                var LayerRef = docRef.artLayers[i];
                if(LayerRef.kind == LayerKind.TEXT){
                    var TextRef = LayerRef.textItem;
                    //do stuff here
                    }
                }
            }
        catch(e){alert(e + e.line);}

 

You also need to define colors properly. You define a variable as a new SolidColor.

 

var myColor = new SolidColor;
myColor.rgb.hexValue = "FFFFFF";
TextRef.color = myColor;

 

See if that works better.

Known Participant
January 27, 2023

Thanks! However i don't understand how to implement the code that you gave me with the code i wrote.

Known Participant
January 27, 2023

i've mistakenly marked my reply as correct answer ahaha sh1t