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

New Here ,
Jun 22, 2018 Jun 22, 2018

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

3.5K

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 24, 2018 Jun 24, 2018

Copy link to clipboard

Copied

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

    }

Likes

translate

Translate

Translate

Report

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
New Here ,
Jul 02, 2018 Jul 02, 2018

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 02, 2018 Jul 02, 2018

Copy link to clipboard

Copied

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

aaa.png

Likes

translate

Translate

Translate

Report

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 ,
Aug 01, 2018 Aug 01, 2018

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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 ,
Aug 01, 2018 Aug 01, 2018

Copy link to clipboard

Copied

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;

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

```

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 01, 2018 Aug 01, 2018

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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 ,
Feb 07, 2021 Feb 07, 2021

Copy link to clipboard

Copied

cool, your code is clean and the key of layer export!

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 07, 2021 Feb 07, 2021

Copy link to clipboard

Copied

On the occasion of new post in this thread I have quick question about quick export to r-bin. How you were able to create the presented (document / layer) export code, if that's not recorded by ScriptListener? Did you make some research and found it in other Adobe scripts, or were so smart to compose random compontents to create it? 🙂 Maybe you analyzed .js files of Ps Generator (in Required folder), or used the script you shared: Script Events Listener?

 

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 07, 2021 Feb 07, 2021

Copy link to clipboard

Copied

Kukurykus
 
Set this in the settings.
 
Start recording Actions. Do QuickExport.
Convert Action to script.
 
EDIT:
I can’t complete the post normally. What s.....
 
abc1.jpg

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 07, 2021 Feb 07, 2021

Copy link to clipboard

Copied

Excellent! That was so easy, but I would've not found myself that option must be ticked to do it 😕 I tried it earlier by various methods, incl. actions, but nothing worked till you said about the box!

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 01, 2018 Aug 01, 2018

Copy link to clipboard

Copied

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?

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

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?

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

I dont know. I think it will not help. The CC2018 generator files date back to 2016.

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

Kukurykus 

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?

Maybe I did not understand something. But in CC2015.5 export to PNG also gives 72dpi

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

Okey so let me explain. There are topics on Photoshop and Photoshop Family Customer Community where users say they had to downgrade their releases from CC 2018 to CC 2017 because current one doesn't respect resolution of layers they are exported to be created as single files with using 'File / Generate / Image Assets'. As I understand new files like .jpg's and png's are created by generator the same way you would do it by 'Layer / Quick Export as PNG' or 'Layer / Export As'. I don't know what resolution files were getting using exporting directly from 'Layer' manu in CC2017, but I suppose that had to be that document they were exported from got. Otherwise why using 'Image Assets' they kept original document resolution?

Likes

translate

Translate

Translate

Report

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