Skip to main content
Participant
February 1, 2023
Question

[scripting] Export Layer with applied adjustments

  • February 1, 2023
  • 3 replies
  • 1667 views

Adobe Photoshop allows to (fast) export selected Layer as .png. Export result contains a Layer picture with all adjustments applied to this Layer from Layers above (it is fine and expected).

 

However both 'ScriptingListener - plugin' and 'ActionRecorder - in app' do not see and do not record this action at all. (why?)

 

I know that there is a solution to enable only required layers and use 'document.saveAs()'.

But is it the only one way to do this? Can I do something like 'selectedLayer.saveAs()' ?

This topic has been closed for replies.

3 replies

Stephen Marsh
Community Expert
Community Expert
February 18, 2023

@EUGENE23910139t6nb 

 

Try this for a first draft script. Make your target layer active, then run the script. You will be prompted for an output folder (there is no warning if a file is being overwritten, but the check could be added). There is placeholder code commented out in the script for a hard-coded save path, rather than a prompt.

 

EDIT: I have found some problems with this script 1.0 version with raster content in layer sets, so depending on your layer structure results may be incorrect... I'll look at fixing this when I have time!

 

/*
Faux Export Active Layer As PNG.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/scripting-export-layer-with-applied-adjustments/m-p/13576245
v1.0 - 18th February 2023, Stephen Marsh
*/

var actLayer = activeDocument.activeLayer;
var docName = actLayer.name.replace(/\.[^\.]+$/, '');
selectActiveLayerToTopLayer(false);
dupeLayers(docName);
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length - 1];
var baseLayer = activeDocument.activeLayer;
activeDocument.activeLayer.name = docName;
for (var i = 0; i < activeDocument.layers.length; i++) {
    if (activeDocument.layers[i].typename === "LayerSet" && activeDocument.layers[i] !== baseLayer) {
        activeDocument.layers[i].remove();
        if (activeDocument.layers[i].kind == LayerKind.NORMAL && activeDocument.layers[i].typename === "ArtLayer" && activeDocument.layers[i] !== baseLayer) {
            activeDocument.layers[i].remove();
        }
    }
}
activeDocument.trim(TrimType.TRANSPARENT);
mergeVisible();
/* 
var outputPath = Folder("~/Desktop");
*/
var outputPath = Folder.selectDialog("Please select an export location:");
var saveOptions = new PNGSaveOptions();
saveOptions.compression = 0; // 0 Largest file size to 9 smallest file size
saveOptions.interlaced = false;
app.activeDocument.saveAs(outputPath, saveOptions, false, Extension.LOWERCASE);
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
activeDocument.activeLayer = actLayer;


///// Functions /////

function selectActiveLayerToTopLayer(makeVisible) {
    // Select all visible layers above and including the active layer
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("front"));
    descriptor.putReference(s2t("null"), reference);
    descriptor.putEnumerated(s2t("selectionModifier"), s2t("selectionModifierType"), s2t("addToSelectionContinuous"));
    descriptor.putBoolean(s2t("makeVisible"), makeVisible);
    // The current active layer id
    list.putInteger(activeDocument.activeLayer.id);
    // The top layer id
    list.putInteger(activeDocument.layers[0].id);
    descriptor.putList(s2t("layerID"), list);
    executeAction(s2t("select"), descriptor, DialogModes.NO);
}

function dupeLayers(docName) {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    var reference2 = new ActionReference();
    reference.putClass(s2t("document"));
    descriptor.putReference(s2t("null"), reference);
    descriptor.putString(s2t("name"), docName);
    reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("using"), reference2);
    executeAction(s2t("make"), descriptor, DialogModes.NO);
}

function mergeVisible() {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    executeAction(s2t("mergeVisible"), undefined, DialogModes.NO);
}

 

Participant
February 27, 2023

Thank you for the code provided. It helped me to understand some details.

My main question was 'why there is no saveAs/exportAs method on ArtLayer object' and I got the answer. Thanks a lot.

 

I see that a lot of actions in a custom scripts made with an ActionDescriptor functionality. Where can I find an api with list of available properties and actions? At this moment all I can do is to record manual changes and look at the script to see what properties have been changed.

Stephen Marsh
Community Expert
Community Expert
February 13, 2023
quote

However both 'ScriptingListener - plugin' and 'ActionRecorder - in app' do not see and do not record this action at all. (why?)

 

Because the programmers didn't include support for actions or scripting when creating Export As/Quick Export. Adobe knows about this and the expectation is that Export As will be supported in Actions/Scripting Listener AM code and possibly DOM code at some "future" point in time.

 

 

quote

I know that there is a solution to enable only required layers and use 'document.saveAs()'.

But is it the only one way to do this? Can I do something like 'selectedLayer.saveAs()' ?


By @EUGENE23910139t6nb

 

Do you mean activeDocument rather than document?

 

Do you mean activeLayer rather than selectedLayer (as there is no such thing as selectedLayer)?

 

Earth Oliver
Legend
February 13, 2023

That's because Adobe hasn't yet been able to make Export As an actionable tool... more than seven years after it's introduction. It's mind blowing that they don't seem to care about this, but Export As has been one disaster after another, so maybe not so surprising. 

c.pfaffenbichler
Community Expert
Community Expert
February 12, 2023

I suspect you may need to create a more involved Script that evaluates the Layers and hides them accordingly. 

Personally I would recommend starting with creating a duplicate so as to make sure the original image does not accidentally get changed. 

Stephen Marsh
Community Expert
Community Expert
February 18, 2023
quote

I suspect you may need to create a more involved Script that evaluates the Layers and hides them accordingly.


By @c.pfaffenbichler

 

Ha, indeed... I didn't realise how much "under the hood stuff" was involved with the Layer > Quick Export As PNG command until I tried to script this myself!