Export Every Photoshop Layer as Its Own PSD File (Free Script)
Today i have a free script for you guys, but this time, it’s for photoshop.
In an effort to keep my After Effects project panel clean i have changed my workflow to using single psd’s and importing them as a composition. At least for images of the same kind (think team logos for sports mogrts for example). What seems like the opposite of organizing (dumping every single logo into the same psd) works surprisingly well in After Effects.
But as it always is, there comes a time where you do need those logos as individual psd’s. Of course one could export each logo seperately to its own file, but that isn’t how i want to do things. Who knows: i might need to do the exact same thing one more time in a year and instead of just exporting manually, i could spend that time to search for that one script i developed ages ago on my hard drive.
So without further rambling, here is the script. To use it, just open the photoshop file containing the layers you want to export individually and use photoshops built in script function.
▸ View script .jsx
#target photoshop
// Export every top-level layer of the active document as its own PSD file,
function main() {
if (app.documents.length === 0) {
alert("No document open.");
return;
}
var sourceDoc = app.activeDocument;
var destFolder = Folder.selectDialog("Select folder to save individual layer PSDs");
if (!destFolder) return;
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var layerNames = [];
for (var i = 0; i < sourceDoc.layers.length; i++) {
layerNames.push(sourceDoc.layers[i].name);
}
var exportedCount = 0;
for (var j = 0; j < layerNames.length; j++) {
try {
exportLayer(sourceDoc, layerNames[j], destFolder);
exportedCount++;
} catch (e) {
alert("Skipped layer \"" + layerNames[j] + "\": " + e.message);
}
}
app.preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
alert("Done. Exported " + exportedCount + " of " + layerNames.length +
" layer(s) to:\n" + destFolder.fsName);
}
function sanitizeFileName(name) {
return name.replace(/[\\\/:*?"<>|]/g, "_");
}
function exportLayer(sourceDoc, layerName, destFolder) {
var dupDoc = sourceDoc.duplicate(layerName, false);
for (var i = dupDoc.layers.length - 1; i >= 0; i--) {
if (dupDoc.layers[i].name !== layerName) {
dupDoc.layers[i].remove();
}
}
if (dupDoc.layers.length === 0) {
dupDoc.close(SaveOptions.DONOTSAVECHANGES);
throw new Error("no matching layer found");
}
dupDoc.layers[0].visible = true;
var fileName = sanitizeFileName(layerName) + ".psd";
var saveFile = new File(destFolder.fsName + "/" + fileName);
var psdOptions = new PhotoshopSaveOptions();
psdOptions.layers = true;
psdOptions.embedColorProfile = true;
psdOptions.alphaChannels = true;
dupDoc.saveAs(saveFile, psdOptions, true, Extension.LOWERCASE);
dupDoc.close(SaveOptions.DONOTSAVECHANGES);
}
main();
Quick thing to watch out for: the script matches layers by name, not by position, so if two layers end up with the same name (happens more than you'd expect on a file that's been edited a bunch of times) they'll get bundled into the same export instead of split apart. Worth a glance through your layers panel before you run it, just in case.