Copy link to clipboard
Copied
The question itself is pretty simple.
For example if i have:
Window handle layer, for example name ''woodhandle''
Window color layer, for example ''Ral1111''
Window whatever layer, for example ''decal01''
And when i save it it would be like ''woodhandle_Ral1111_decal01.png''
I use transparent layers stacked on eachother some visible some not so if i just change visibility handle layer into ''plastichandle''
It would look like this ''plastichandle_Ral1111_decal01.png''
Layer names vary often.
If anyone could help me with this simple thing id be very grateful ![]()
Hi. This script should collect all visible layer names into array and use it as a file name to save PNG. PNG file will be saved to same locations as your original PSD file is.
Hope that helps.
...(function() {
try {
// Get all visible layers in the file.
var visibleLayers = getVisibleLayers(app.activeDocument);
// If there are visible layers in the file, then proceed.
if (visibleLayers.length > 0) {
// Make a String from an Array object and replace , with _ g
Copy link to clipboard
Copied
I may be wrong, and if so, I will stand corrected, but PNG does not permit layers. Therefore, you will have to flatten the picture before you can save it as PNG.
That said, you will have to read out the layer names beforehand. Here we have the question whether these are all the layers (no other layers present), and whether they are always in the same stacking order.
If this is the case, you can read out the layers, retrieve their names, and assemble the file name. Then you flatten, and finally save. To assemble the script, you will have to look in the documentation for the Layers Object, the Document Object, and the save parameters for PNG.
Copy link to clipboard
Copied
Thank you for your response. The order isnt important, because i will always have them in certain order.
The final image doesnt need to have layers since indeed png cant contain layers.
I unfortunately have no scripting skills with photoshop ![]()
Copy link to clipboard
Copied
Hi. This script should collect all visible layer names into array and use it as a file name to save PNG. PNG file will be saved to same locations as your original PSD file is.
Hope that helps.
(function() {
try {
// Get all visible layers in the file.
var visibleLayers = getVisibleLayers(app.activeDocument);
// If there are visible layers in the file, then proceed.
if (visibleLayers.length > 0) {
// Make a String from an Array object and replace , with _ globally
var fileName = visibleLayers.join().replace(/,/g, "_");
// Establish file path to save file.
var saveFile = app.activeDocument.path + "/" + fileName + ".png";
// Pass file path to function to save PNG image
savePNG(File(saveFile));
} else {
return alert("Yo dude, none of your layers are visible!");
}
function getVisibleLayers(doc, layers) {
layers = layers || [];
for (var i = 0, il = doc.layers.length; i < il; i++) {
if (doc.layers.typename == "LayerSet") {
getVisibleLayers(doc.layers, layers)
} else {
if (doc.layers.visible) {
layers.push(doc.layers.name)
}
}
}
return layers
}
function savePNG(saveFile) {
activeDocument.saveAs(saveFile, new PNGSaveOptions(), true, Extension.LOWERCASE);
};
} catch (e) {
alert(e.toString() + "\nScript File: " + File.decode(e.fileName).replace(/^.*[\|\/]/, '') +
"\nFunction: " + arguments.callee.name +
"\nError on Line: " + e.line.toString())
}
})();
Copy link to clipboard
Copied
THANK YOUUU
This is exacly what i needed.
I am extremely grateful for this ![]()
Thanx
Copy link to clipboard
Copied
One more question. The script saves the file as 85,1MB, while usually saving makes it about 8MB, could anything be done to that?
Copy link to clipboard
Copied
Seems like what it needs is to use compression Smallest / Slow , but i dont know how to set that in the script, currently uses none
Copy link to clipboard
Copied
Hi man. Feel free to update savePNG(saveFile) function with this one. This will save PNG as if Compression: Smallest/Slow and Interlace: None are enabled
function savePNG(saveFile) {
var pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.compression = 9;
pngSaveOptions.interlaced = false;
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
};
Find more inspiration, events, and resources on the new Adobe Community
Explore Now