Skip to main content
Participant
June 22, 2018
解決済み

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

  • June 22, 2018
  • 返信数 1.
  • 12078 ビュー

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

このトピックへの返信は締め切られました。
解決に役立った回答 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

r-bin解決!
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).