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

    }

tut75941724
Participant
August 1, 2018

Hi r-bin,

 

I try your code, it works well by this simple script. However, I find sometimes the selection doesn't exported, and the quick export option in photoshop broken the same time. I need to restart photoshop to get the quick export feature works.

 

One situation is batch export. I want to export the layers under some layerSet. But the layer seems don't exported.

 

```

LayerSet

     LayerToExport 1 // when only one layer under LayerSet the export success, but fails on two layers

     LayerToExport 2

```

 

The other situation is snapshot. Before I export, I just create a snapshot, and after select and export ,I restore the snapshot, but it seems break the export too.

 

 

Some Code:

 

Situation one

```

selectLayer = function(layer) {
    var idslct = charIDToTypeID( "slct" );
    var desc258 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref148 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref148.putIndex( idLyr, layer.itemIndex );
    desc258.putReference( idnull, ref148 );
    executeAction( idslct, desc258, DialogModes.NO );
    return desc258;
  },
  getExportSelectedLayer = function(layer) {
    var path = activeDocument.path.fsName;
    try { 
      var desc = new ActionDescriptor(); 
      var ref = new ActionReference(); 
      ref.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 
      desc.putReference(stringIDToTypeID("null"), ref); 
      desc.putString(stringIDToTypeID("fileType"), "png"); 
      desc.putInteger(stringIDToTypeID("quality"), 32); 
      desc.putInteger(stringIDToTypeID("metadata"), 0); 
      desc.putString(stringIDToTypeID("destFolder"), path); 
      desc.putBoolean(stringIDToTypeID("sRGB"), true); 
      desc.putBoolean(stringIDToTypeID("openWindow"), false); 
      executeAction(stringIDToTypeID("exportSelectionAsFileTypePressed"), desc, DialogModes.NO); 
      return path + '/' + layer.name;
    } catch (e) {
      return '';
    }
    return '';
  }
var layers = app.activeDocument.activeLayer.layers;
for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];
        selectLayer(layer);
        getExportSelectedLayer(layer)
    }

```

 

Situation Two

```

createSnapshot = function(name) {
    var desc = new ActionDescriptor();
    var sref = new ActionReference();
    sref.putClass(charIDToTypeID("SnpS"));
    desc.putReference(charIDToTypeID("null"), sref);
    var fref = new ActionReference();
    fref.putProperty(charIDToTypeID("HstS"), charIDToTypeID("CrnH"));
    desc.putReference(charIDToTypeID("From"), fref);
    desc.putString(charIDToTypeID("Nm  "), name);
    executeAction(charIDToTypeID("Mk  "), desc, DialogModes.NO);
    return time;
  },
  resetPsd = function(name) {
    var doc = app.activeDocument;
    app.activeDocument.activeHistoryState = app.activeDocument.historyStates.getByName(name);
    app.activeDocument.save();
  }
createSnapshot('snap');
// do some export
resetPsd('snap')

```

Legend
August 1, 2018

It may be easier for you to remake to your own needs the native Photoshop script - "Export Layers To Files.jsx",

than to use unreliable newfangled exports of Photoshop CC,

Perhaps it will suit you and without alteration.