Copy link to clipboard
Copied
I have a script that iterates through PSD files in a folder (main folder), opening them one at a time and then referencing a another folder (reference folder) to find files that contain the same filename. If it finds one or more, it opens those and adds them as layers in the original document. However when it pastes them into the original file the layer names are "Background Copy", "Background Copy 1", etc. I've tried a few different ways to rename the layer using replace, but nothing seems to work. Can anyone tell me where I need to put that code in order to have the reference layers paste in with their original filename?
// Function to duplicate layers from one document to another
function duplicateLayers(sourceDocument, targetDocument) {
for (var i = 0; i < sourceDocument.artLayers.length; i++) {
var layer = sourceDocument.artLayers[i];
layer.duplicate(targetDocument, ElementPlacement.PLACEATEND);
}
}
// Main function to process each PSD file
function processFile(mainFilePath, layersFolderPath) {
var mainDocument = app.open(new File(mainFilePath));
// Extract the filename without extension
var mainFileName = mainDocument.name.replace(/\.[^\.]+$/, '');
// Iterate through the layers folder and open matching PSD files
var layersFolder = new Folder(layersFolderPath);
if (layersFolder.exists) {
var fileList = layersFolder.getFiles();
for (var i = 0; i < fileList.length; i++) {
if (fileList[i] instanceof File && fileList[i].hidden == false) {
var layerFile = fileList[i];
var layerFileName = layerFile.name.replace(/\.[^\.]+$/, '');
// Check if the layer file matches the main file
if (layerFileName.indexOf(mainFileName) > -1) {
var layerDocument = app.open(layerFile);
// Duplicate layers from the layer document to the main document
duplicateLayers(layerDocument, mainDocument);
// Close the layer document without saving changes
layerDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
} else {
$.writeln("Layers folder does not exist: " + layersFolderPath);
}
// Save and close the main document
mainDocument.save();
mainDocument.close(SaveOptions.SAVECHANGES);
}
// Set Folder Locations
var mainFolderPath = Folder.selectDialog("Select the main folder:");
var layersFolderPath = Folder.selectDialog("Select the references folder:");
var mainFolder = new Folder(mainFolderPath);
if (mainFolder.exists) {
var mainFileList = mainFolder.getFiles();
for (var j = 0; j < mainFileList.length; j++) {
if (mainFileList[j] instanceof File && mainFileList[j].hidden == false) {
var mainFilePath = mainFileList[j].fsName;
processFile(mainFilePath, layersFolderPath);
}
}
} else {
$.writeln("Main folder does not exist: " + mainFolderPath);
}
I have added the following:
// Rename the duped layer
app.activeDocument.activeLayer.name = layerFileName;
It appears to work as expected... Full code:
// Function to duplicate layers from one document to another
function duplicateLayers(sourceDocument, targetDocument) {
for (var i = 0; i < sourceDocument.artLayers.length; i++) {
var layer = sourceDocument.artLayers[i];
layer.duplicate(targetDocument, ElementPlacement.PLACEATEND);
}
}
// Main function...
Copy link to clipboard
Copied
I have added the following:
// Rename the duped layer
app.activeDocument.activeLayer.name = layerFileName;
It appears to work as expected... Full code:
// Function to duplicate layers from one document to another
function duplicateLayers(sourceDocument, targetDocument) {
for (var i = 0; i < sourceDocument.artLayers.length; i++) {
var layer = sourceDocument.artLayers[i];
layer.duplicate(targetDocument, ElementPlacement.PLACEATEND);
}
}
// Main function to process each PSD file
function processFile(mainFilePath, layersFolderPath) {
var mainDocument = app.open(new File(mainFilePath));
// Extract the filename without extension
var mainFileName = mainDocument.name.replace(/\.[^\.]+$/, '');
// Iterate through the layers folder and open matching PSD files
var layersFolder = new Folder(layersFolderPath);
if (layersFolder.exists) {
var fileList = layersFolder.getFiles();
for (var i = 0; i < fileList.length; i++) {
if (fileList[i] instanceof File && fileList[i].hidden == false) {
var layerFile = fileList[i];
var layerFileName = layerFile.name.replace(/\.[^\.]+$/, '');
// Check if the layer file matches the main file
if (layerFileName.indexOf(mainFileName) > -1) {
var layerDocument = app.open(layerFile);
// Duplicate layers from the layer document to the main document
duplicateLayers(layerDocument, mainDocument);
// Close the layer document without saving changes
layerDocument.close(SaveOptions.DONOTSAVECHANGES);
// Rename the duped layer
app.activeDocument.activeLayer.name = layerFileName;
}
}
}
} else {
$.writeln("Layers folder does not exist: " + layersFolderPath);
}
// Save and close the main document
mainDocument.save();
mainDocument.close(SaveOptions.SAVECHANGES);
}
// Set Folder Locations
var mainFolderPath = Folder.selectDialog("Select the main folder:");
var layersFolderPath = Folder.selectDialog("Select the references folder:");
var mainFolder = new Folder(mainFolderPath);
if (mainFolder.exists) {
var mainFileList = mainFolder.getFiles();
for (var j = 0; j < mainFileList.length; j++) {
if (mainFileList[j] instanceof File && mainFileList[j].hidden == false) {
var mainFilePath = mainFileList[j].fsName;
processFile(mainFilePath, layersFolderPath);
}
}
} else {
$.writeln("Main folder does not exist: " + mainFolderPath);
}
Copy link to clipboard
Copied
Thank you.
Copy link to clipboard
Copied
You're welcome!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now