Copy link to clipboard
Copied
Hi all,
I need a script for Photoshop that does the following step
- Select all layers and groups from an opened document
- Duplicate them in a new document
- Call that new document like the document it copied the layers/groups from
- Close the original document and leave the copied document active
Many many thanks.
I originally wrote this script for another topic, however, I removed the code as I was having issues duplicating the paths, as there were problems with vector shape layers and regular paths in the paths panel getting messed up.
As you didn't ask for paths to be copied, I have modified that script. It still copies any guides and alphas in addition to all the layers. It's easy to disable the function calls to dupe the guides and alphas if you don't require them. The original
...Copy link to clipboard
Copied
I originally wrote this script for another topic, however, I removed the code as I was having issues duplicating the paths, as there were problems with vector shape layers and regular paths in the paths panel getting messed up.
As you didn't ask for paths to be copied, I have modified that script. It still copies any guides and alphas in addition to all the layers. It's easy to disable the function calls to dupe the guides and alphas if you don't require them. The original document bit depth and ICC profile are retained.
All original document-level metadata is removed via the layer duplication process, which was the original idea behind this script.
/*
Dupe All Layers to New Doc Named After Original Doc.jsx
v1.0 - 20th June 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-to-rename-duplicate-document-as-the-original-document/td-p/14692580
Based on:
https://community.adobe.com/t5/photoshop/script-to-remove-all-meta-data-from-the-photo/m-p/10400906
*/
#target photoshop
var theLayers = app.activeDocument.layers;
var sourceDoc = app.activeDocument;
selectAllLayers();
dupeSelectedLayers();
var dupedDoc = app.activeDocument;
app.activeDocument = sourceDoc;
dupeGuides();
dupeAlphas();
// Paths are not currently copied as vector shape layers and regular paths complicate things!
alert('The original document has been duplicated! You will now be prompted to save changes to the original before closing...');
sourceDoc.close(SaveOptions.PROMPTTOSAVECHANGES); // (SaveOptions.SAVECHANGES) | (SaveOptions.DONOTSAVECHANGES)
app.activeDocument = dupedDoc;
// Functions
function selectAllLayers() {
var c2t = function (s) {
return app.charIDToTypeID(s);
};
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(c2t("null"), reference2);
executeAction(s2t("selectAllLayers"), descriptor, DialogModes.NO);
try {
// Add the Background layer if it exists
reference.putProperty(s2t("layer"), s2t("background"));
descriptor2.putReference(c2t("null"), reference);
descriptor2.putEnumerated(s2t("selectionModifier"), s2t("selectionModifierType"), s2t("addToSelection"));
descriptor2.putBoolean(s2t("makeVisible"), false);
executeAction(s2t("select"), descriptor2, DialogModes.NO);
} catch (e) {
alert("Error!" + "\r" + e + ' ' + e.line);
}
}
function dupeSelectedLayers() {
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"), app.activeDocument.name.replace(/\.[^\.]+$/, ''));
reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("using"), reference2);
executeAction(s2t("make"), descriptor, DialogModes.NO);
}
function dupeGuides() {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var guidesArray = new Array;
for (var g = 0; g < sourceDoc.guides.length; g++) {
guidesArray.push([sourceDoc.guides[g].coordinate, sourceDoc.guides[g].direction]);
}
app.preferences.rulerUnits = originalRulerUnits;
if (dupedDoc != app.activeDocument) {
app.activeDocument = dupedDoc;
app.preferences.rulerUnits = Units.PIXELS;
for (var a = 0; a < guidesArray.length; a++) {
dupedDoc.guides.add(guidesArray[a][1], guidesArray[a][0]);
}
app.preferences.rulerUnits = originalRulerUnits;
}
}
function dupeAlphas() {
app.activeDocument = sourceDoc;
var sourceDocAlphas = sourceDoc.channels;
var targetDoc = dupedDoc;
if (app.activeDocument.mode === DocumentMode.RGB || app.activeDocument.mode === DocumentMode.LAB) {
for (a = 3; a < sourceDocAlphas.length; a++) {
sourceDocAlphas[a].duplicate(targetDoc);
}
} else if (app.activeDocument.mode === DocumentMode.CMYK) {
for (a = 4; a < sourceDocAlphas.length; a++) {
sourceDocAlphas[a].duplicate(targetDoc);
}
} else if (app.activeDocument.mode === DocumentMode.GRAYSCALE || app.activeDocument.mode === DocumentMode.DUOTONE) {
for (a = 1; a < sourceDocAlphas.length; a++) {
sourceDocAlphas[a].duplicate(targetDoc);
}
} else { }
app.activeDocument = dupedDoc;
dupedDoc.activeChannels = app.activeDocument.componentChannels;
}
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
Copy link to clipboard
Copied
Why not just Save As, close the original, and rename the new file?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now