Answered
Script to open layer as a new document?
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.
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.
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;
}
}Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.