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
  • 12007 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); }

    }

Participant
July 2, 2018

Can I specify a custom file name instead of the default layer name?

Legend
July 2, 2018

I think nothing will come of it.

The export function works through the Generator plugin.
Renaming layers will not do anything good.

If you remove the Generator from the plugins, we get this error when the function is executed (after some time).