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

Save as png containing visible layer names Script

Community Beginner ,
Mar 24, 2017 Mar 24, 2017

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

TOPICS
Actions and scripting
1.7K
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

Advocate , Mar 24, 2017 Mar 24, 2017

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

...
Translate
Adobe
Advocate ,
Mar 24, 2017 Mar 24, 2017

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.

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 ,
Mar 24, 2017 Mar 24, 2017

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

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
Advocate ,
Mar 24, 2017 Mar 24, 2017

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

    }

})();

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 ,
Mar 25, 2017 Mar 25, 2017

THANK YOUUU

This is exacly what i needed.

I am extremely grateful for this

Thanx

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 ,
Mar 28, 2017 Mar 28, 2017

One more question. The script saves the file as 85,1MB, while usually saving makes it about 8MB, could anything be done to that?

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 ,
Mar 28, 2017 Mar 28, 2017

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

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
Advocate ,
Mar 29, 2017 Mar 29, 2017
LATEST

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

};

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