JavaScript Photoshop code not working correctly

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.

It exports correctly the 6 files, BUT exports every file as the color black and not in the respective colors.

Tried adding =null before recoloring but activeLayer can’t be equal to null or undefined.

Here’s the code:

app.activeDocument.activeLayer.textItem.color = new SolidColor();
app.activeDocument.activeLayer.textItem.color.rgb.hexValue = "000000";

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);

app.activeDocument.activeLayer.textItem.color = new SolidColor();
app.activeDocument.activeLayer.textItem.color.rgb.hexValue = "FFFFFF";


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

app.activeDocument.activeLayer.textItem.color = new SolidColor();
app.activeDocument.activeLayer.textItem.color.rgb.hexValue = "fc82d8";


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

app.activeDocument.activeLayer.textItem.color = new SolidColor();
app.activeDocument.activeLayer.textItem.color.rgb.hexValue = "63a8e7";


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

app.activeDocument.activeLayer.textItem.color = new SolidColor();
app.activeDocument.activeLayer.textItem.color.rgb.hexValue = "0c6a27";


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

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


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

Try establishing the color separately before trying to assign it?
so instead of

app.activeDocument.activeLayer.textItem.color = new SolidColor();
app.activeDocument.activeLayer.textItem.color.rgb.hexValue = "FFFFFF";

try

var color;
...

color = new SolidColor();
color.rgb.hexValue="FFFFFF";
app.activeDocument.activeLayer.textItem.color = color;

?

Yeah, I’ve modified the code correspondingly and now color 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.

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);

Is the original image in RGB mode?

It is a text layer, not an image. It is not resterized or converted to a smart object either. Here’s the screenshot of the color picker i don’t know if it’s useful at all ahaha
sssssssssssssssssss

It’s a text layer, in an image. That has a mode. And if that mode isnt RGB-8, it might be messing with the thing. With the image open, go to the Image menu, and look at Mode.

It is set as RGB.

SOLVED!
Apparently if i add write “FFFFFF” it exports it wrong. But if i write " FFFFFF" it will export it white as it should. Same thing goes for red and pink, not for blue or green or black.

Do you know how could i export the files in a normal folder called “number that goes up every output + name inputed in the prompt” instead of the .zip folder it creates?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.