Skip to main content
Participant
June 22, 2018
Answered

How do I quickly export a layer as a png file in Photoshop with extendscript

  • June 22, 2018
  • 1 reply
  • 12096 views

I need to export every layer as png file, just like the context menu item(Quick Export as PNG) of  layer does. Currently, I do this by:

... // Hide all layers

layer.visible = true; // Show the current layer only

doc.trim(TrimType.TRANSPARENT)

var options = new ExportOptionsSaveForWeb()

options.format = SaveDocumentType.PNG

options.PNG8 = false

var saveFile = new File(destFileName);

doc.exportDocument(saveFile, ExportType.SAVEFORWEB, options)

It does work, but it takes about 2 seconds for a layer in several PSD files. Why is it so slow? Is there a better approach to do this? like using runMenuItem()?

This topic has been closed for replies.
Correct answer r-bin

// export activeDocument

quick_export_png(activeDocument.path.fsName)

// export activeLayer

quick_export_png(activeDocument.path.fsName, true);

function quick_export_png(path, layer)

    {

    try

        {

        if (layer == undefined) layer = false;   

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        d.putReference(stringIDToTypeID("null"), r);

        d.putString(stringIDToTypeID("fileType"), "png");

        d.putInteger(stringIDToTypeID("quality"), 32);

        d.putInteger(stringIDToTypeID("metadata"), 0);

        d.putString(stringIDToTypeID("destFolder"), path);

        d.putBoolean(stringIDToTypeID("sRGB"), true);

        d.putBoolean(stringIDToTypeID("openWindow"), false);

        executeAction(stringIDToTypeID(layer?"exportSelectionAsFileTypePressed":"exportDocumentAsFileTypePressed"), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }

1 reply

r-binCorrect answer
Legend
June 25, 2018

// export activeDocument

quick_export_png(activeDocument.path.fsName)

// export activeLayer

quick_export_png(activeDocument.path.fsName, true);

function quick_export_png(path, layer)

    {

    try

        {

        if (layer == undefined) layer = false;   

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        d.putReference(stringIDToTypeID("null"), r);

        d.putString(stringIDToTypeID("fileType"), "png");

        d.putInteger(stringIDToTypeID("quality"), 32);

        d.putInteger(stringIDToTypeID("metadata"), 0);

        d.putString(stringIDToTypeID("destFolder"), path);

        d.putBoolean(stringIDToTypeID("sRGB"), true);

        d.putBoolean(stringIDToTypeID("openWindow"), false);

        executeAction(stringIDToTypeID(layer?"exportSelectionAsFileTypePressed":"exportDocumentAsFileTypePressed"), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }

Kukurykus
Legend
August 1, 2018

You'll be master if you do exported images keep resolution of document. Do it for example with some .jpg that has 300dpi. You will see new exported .png layer has 72dpi. It looks the same, the only difference is a number when you checks its res. What I read it still worked fine in previous CC 2017 release but in CC 2018 they did something with generator and now you can't export layers with original resolution. Are you able to fix it?

Kukurykus
Legend
August 2, 2018

I do not like this export at all.

I do not know how to fix this. It probably needs to change the Generator extension. I am not qualified in this field.

In addition, the utility runs asynchronously and when the script finishes.

For example, if you run a script

quick_export_png("C:\\PATH", true); 

alert()

then until you click OK in the alert and finish the script, the file is not exported.

In addition, you can not change the layer name and active layer after calling the export function.

This can disrupt the entire work of the Generator.


What I had to do personally but didn't yet was to download CC 2017 to see how that worked as users say it still worked well in that release. So maybe it would be sufficient to compare generator files of this and previous version and simply copy old generator part that is responsible for exporting to new generator. It should not be hard as generators are written in .js and can be found in some Ps folder. Do you think that could work if we specified that difference in both generators or maybe Generator files are the same but something in Photoshop was changed that disturpts proper performance of Generator?