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

Script problem

Explorer ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

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

 

TOPICS
Windows

Views

711

Translate

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
Adobe
LEGEND ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

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 ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

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

Votes

Translate

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 ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

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

Votes

Translate

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 ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

Thanks! However i don't understand how to implement the code that you gave me with the code i wrote. I tried running this implementing your suggestions but it will still export the white file as a black text: "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;

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

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

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

}
}
}
catch(e){alert(e + e.line);}"

Votes

Translate

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
LEGEND ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

You are defining the color wrong. Do it with the code I posted.

Votes

Translate

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 ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

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

Votes

Translate

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
Valorous Hero ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

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.
 

Votes

Translate

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 ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

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 ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

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.

 

Votes

Translate

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
Community Expert ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

The path seems to be missing. 

Anyway, the results seem to be about as expected. 

Screenshot 2023-01-28 at 14.14.01.png

var textColor = new SolidColor();
textColor.rgb.hexValue = "000000";
app.activeDocument.activeLayer.textItem.color = textColor;
var newName = "_aaa";
try {var myPath = app.activeDocument.path}
catch (e) {var myPath = "~/Desktop"};
var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.quality = 100;
//
exportOptions.PNG8 = false;	
exportOptions.transparency = true;
exportOptions.interlaced = 0;
exportOptions.includeProfile = true;
exportOptions.optimized = true;
//
var file = new File(myPath+"/"+"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(myPath+"/"+"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(myPath+"/"+"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(myPath+"/"+"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(myPath+"/"+"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(myPath+"/"+"red" + newName + ".png");
app.activeDocument.exportDocument(file6, ExportType.SAVEFORWEB, exportOptions);

 

Votes

Translate

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 ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Thanks!
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?

Votes

Translate

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
Community Expert ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

In the Script I posted the pngs are exported next to the file (the desktop if the file is unsaved), you can redirect it to some other Folder. 

Votes

Translate

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 ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Yeah, however I'd like to create a new folder every time it outputs, which is automatically named as "*number that goes up every output* + *name inputed in the prompt*". Unfortunately I have no idea how to do it.

Votes

Translate

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
Community Expert ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Could you please post screenshots that illustrate the naming convention of the Folders? 

Are the Folders next to the original image or somewhere else? 

 

I expect you need to check the Folders at the target location, use RegExp (or simple String-editing) to compare their names and determine the highest existing number to be able to decide on the next one. 

Votes

Translate

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 ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

So, Photoshop will automatically export the 6 files created through the code in my zip folder called "output.zip". This folder is donwloaded spontaneously in the "Downloads" path, not having declared a specific path (and I'm fine with that because in fact I'd liked that the files were downloaded in the "downloads" folder). 
The thing that I'm trying to get help with is to implement a piece of code that will make Photoshop export the 6 files created with the script, in a normal folder which I'd like to be able to name 

automatically as the "newName" variable which I alredy input through a prompt as its the first thing the code above operates at the moment
OR ELSE

to be able to name the folder with an input that I write through a new prompt.

Votes

Translate

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
Community Expert ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

LATEST

This would get you an Array of all the Folders beside the active document that start with numbers. 

var theFiles = Folder(app.activeDocument.path).getFiles();
var theFolders = new Array;
var theRegExp = /^\d{0,}/;
for (var m = 0; m < theFiles.length; m++) {
    var thisOne = theFiles[m];
    if (thisOne.constructor == Folder && String(Folder(thisOne).name.match(theRegExp)) != "") {
        theFolders.push(thisOne)
    }
};
alert (theFolders.join("\n"));

Votes

Translate

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