Skip to main content
Flowgun
Inspiring
June 10, 2025
Answered

Script to open layer as a new document?

  • June 10, 2025
  • 2 replies
  • 511 views

Is it possible to create a script that opens current layer in a new document? (not as smart object). I know I can handle that using the clipboard, but I want to know there's another possibility.

Correct answer Stephen Marsh

@Flowgun 

 

Try this:

/*
Dupe Active Layer to New Doc.jsx
Stephen Marsh
v1.0 - 11th June 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-open-layer-as-a-new-document/m-p/15364728
*/

(function () {

    if (app.documents.length === 0) {
        alert("No document is open!");
        return;
    }
    if (!hasActiveLayer()) {
        alert("No active layer selected!");
        return;
    }
    if (isInvalidLayerType(app.activeDocument.activeLayer)) {
        alert("Adjustment layers aren't supported!");
        return;
    }

    var docName = app.activeDocument.activeLayer.name.replace(/[\/\\:*?"<>|]/g, "");

    dupeLayer(docName);

    var doc = app.activeDocument;
    var layer = doc.activeLayer;
    origDocWidth = doc.width.as('px');
    origDocHeight = doc.height.as('px');
    var docWidth = doc.width.as('px');
    var docHeight = doc.height.as('px');
    var bounds = layer.bounds;
    var left = bounds[0].as('px');
    var top = bounds[1].as('px');
    var right = bounds[2].as('px');
    var bottom = bounds[3].as('px');
    var layerWidth = right - left;
    var layerHeight = bottom - top;

    if (left < 0 || top < 0 || right > docWidth || bottom > docHeight) {
        confirmation = confirm(
            "Do you wish to reveal oversize layer content?\n" +
            "Canvas size: " + origDocWidth + " x " + origDocHeight + " px\n" +
            "Layer size:  " + layerWidth + " x " + layerHeight + " px" +
            "\n\n" +
            "Reveal?", false);
        if (confirmation) {
            app.activeDocument.revealAll();
        }
    } else {
        // Nothing to see here folks, please move along...
    }

})();


///// Functions /////
function dupeLayer(theName) {
    var s2t = function (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"), theName);
    reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("using"), reference2);
    executeAction(s2t("make"), descriptor, DialogModes.NO);
}

function hasActiveLayer() {
    try {
        // selected layer check by jazz-y
        var s2t = stringIDToTypeID;
        var r = new ActionReference();
        r.putProperty(s2t('property'), s2t('targetLayers'));
        r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
        var desc = executeActionGet(r);
        var targetLayers = desc.getList(s2t('targetLayers'));
        return targetLayers.count > 0;
    } catch (e) {
        return false;
    }
}

function isInvalidLayerType() {
    /*
    https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-layer-kind-using-javascript/m-p/13174707
    */
    s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
    r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    var layerKind = executeActionGet(r).getInteger(p);
    // Skip adjustment layers
    if (layerKind == 2) {
        return true;
    }
}

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
June 11, 2025

@Flowgun 

 

Try this:

/*
Dupe Active Layer to New Doc.jsx
Stephen Marsh
v1.0 - 11th June 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-open-layer-as-a-new-document/m-p/15364728
*/

(function () {

    if (app.documents.length === 0) {
        alert("No document is open!");
        return;
    }
    if (!hasActiveLayer()) {
        alert("No active layer selected!");
        return;
    }
    if (isInvalidLayerType(app.activeDocument.activeLayer)) {
        alert("Adjustment layers aren't supported!");
        return;
    }

    var docName = app.activeDocument.activeLayer.name.replace(/[\/\\:*?"<>|]/g, "");

    dupeLayer(docName);

    var doc = app.activeDocument;
    var layer = doc.activeLayer;
    origDocWidth = doc.width.as('px');
    origDocHeight = doc.height.as('px');
    var docWidth = doc.width.as('px');
    var docHeight = doc.height.as('px');
    var bounds = layer.bounds;
    var left = bounds[0].as('px');
    var top = bounds[1].as('px');
    var right = bounds[2].as('px');
    var bottom = bounds[3].as('px');
    var layerWidth = right - left;
    var layerHeight = bottom - top;

    if (left < 0 || top < 0 || right > docWidth || bottom > docHeight) {
        confirmation = confirm(
            "Do you wish to reveal oversize layer content?\n" +
            "Canvas size: " + origDocWidth + " x " + origDocHeight + " px\n" +
            "Layer size:  " + layerWidth + " x " + layerHeight + " px" +
            "\n\n" +
            "Reveal?", false);
        if (confirmation) {
            app.activeDocument.revealAll();
        }
    } else {
        // Nothing to see here folks, please move along...
    }

})();


///// Functions /////
function dupeLayer(theName) {
    var s2t = function (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"), theName);
    reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("using"), reference2);
    executeAction(s2t("make"), descriptor, DialogModes.NO);
}

function hasActiveLayer() {
    try {
        // selected layer check by jazz-y
        var s2t = stringIDToTypeID;
        var r = new ActionReference();
        r.putProperty(s2t('property'), s2t('targetLayers'));
        r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
        var desc = executeActionGet(r);
        var targetLayers = desc.getList(s2t('targetLayers'));
        return targetLayers.count > 0;
    } catch (e) {
        return false;
    }
}

function isInvalidLayerType() {
    /*
    https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-layer-kind-using-javascript/m-p/13174707
    */
    s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
    r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    var layerKind = executeActionGet(r).getInteger(p);
    // Skip adjustment layers
    if (layerKind == 2) {
        return true;
    }
}
Flowgun
FlowgunAuthor
Inspiring
June 11, 2025

Thank you for the reply. In the end I just reused some functions I already have which might be more suitable for my use case. I basically copy-merge my open document, create a new document with clipboard as preset, and paste the clipboard.

Stephen Marsh
Community Expert
Community Expert
June 11, 2025
quote

Thank you for the reply. In the end I just reused some functions I already have which might be more suitable for my use case.

 

It would appear that your use-case wasn't the same as the original brief.

 

:]

 

quote

I basically copy-merge my open document, create a new document with clipboard as preset, and paste the clipboard.


By @Flowgun

 

That's a lot of work (3 steps) and overhead (clipboard)...

 

app.activeDocument.duplicate(app.activeDocument.name.replace(/\.[^\.]+$/, ''), true);

 

Stephen Marsh
Community Expert
Community Expert
June 10, 2025

@Flowgun 

 

Certainly, a check should be performed to ensure that the layer is suitable (not an adjustment layer) and I presume that the new document name should be based on the layer name (possibly sanitised if illegal filename characters are in the layer name).