Copy link to clipboard
Copied
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("
...
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
:]
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);
Copy link to clipboard
Copied
yep I know it has a lot of overhead and that's why I posted my original brief. I also can see it being useful in the future and for a multitude of people (e.g: tightening up storyboards / comic panels from a multi-page document and preparing them for print) so I'm glad that your reply is here for the community 😃
Copy link to clipboard
Copied
Have you tested the previous 1-liner code to duplicate and merge visible layers in the active document in a single step without using the clipbaord?
https://theiviaxx.github.io/photoshop-docs/Photoshop/Document/duplicate.html
Copy link to clipboard
Copied
I already have a similar script I use instead of the default's ctrl+alt+shift+E, which works even if the currently-selected layer is hidden, and it allows me also to send the layer to the front and give it a custom label color and name. I don't share my code because I'm converting everything to Autohotkey v1 (which is already depricated) thanks to the COM API, instead of using JSX. In this particular case, I want to merge into a new document to automatically create different variations in different artboards that I can look at and reference while working on my unchanged main document.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now