JimboHames
Member
- Messages
- 7
- Likes
- 2
File automation, copying layers to other open doc, close doc, and loop through other source files
I have a script which I feel is so close to working but I'm at the point where I'm pulling my hair out.
Let me give you a rundown.
I have a folder of Source PSD and a folder of Destination TIFF files.
The script runs and opens a source file then searches the Destination folder for a file which contains the name of the Source file and opens that.
I then need the script to copy the background layer from the Destination TIFF file over to the Source file
Delete Layer 1 in the Source file (Copied layer should be named Layer 2)
Save as PSD in Destination Folder using the same name as original Destination file
Close open docs
Then repeat for all other Source files
Right now it seems to want to save the original Destination file and refuses to cycle through the additional Source images
The script I have is this:-
// Prompt user to select Source and Destination folders
var sourceFolder = Folder.selectDialog("Select Source Folder");
var destFolder = Folder.selectDialog("Select Destination Folder");
if (sourceFolder && destFolder) {
var sourceFiles = sourceFolder.getFiles("*.psd");
var destFiles = destFolder.getFiles("*.tif");
for (var i = 0; i < sourceFiles.length; i++) {
var sourceFile = sourceFiles;
var sourceFileName = sourceFile.name.replace(/\.[^.]+$/, ""); // Remove extension
for (var j = 0; j < destFiles.length; j++) {
var destFile = destFiles[j];
var destFileName = destFile.name.replace(/\.[^.]+$/, ""); // Remove extension
if (destFileName.indexOf(sourceFileName) !== -1) {
var sourceDoc = app.open(sourceFile);
var destDoc = app.open(destFile);
// Delete Layer 1 in source document
if (sourceDoc.layers.length > 0) {
sourceDoc.layers[sourceDoc.layers.length - 1].remove();
}
// Duplicate the background layer from destination
var destBackgroundLayer = destDoc.artLayers[0];
var sourceLayer = destBackgroundLayer.duplicate(sourceDoc, ElementPlacement.PLACEATBEGINNING);
// Loop through layers in source document
for (var k = sourceDoc.layers.length - 1; k >= 0; k--) {
var sourceChildLayer = sourceDoc.layers[k];
var sourceParentLayer = sourceChildLayer.parent;
// Check if the source layer is a clipping mask
if (sourceChildLayer.grouped && sourceParentLayer) {
// Find the corresponding parent layer in destination
var destParentLayerName = sourceParentLayer.name;
var destParentLayer = destDoc.layers.getByName(destParentLayerName);
if (destParentLayer) {
sourceChildLayer.duplicate(destParentLayer, ElementPlacement.PLACEATBEGINNING);
}
}
}
// Save modified source file in destination folder with PSD file name
var newFileName = sourceFileName + "_modified.psd";
sourceDoc.saveAs(new File(destFolder + "/" + newFileName), SaveOptions.PSD);
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
destDoc.close(SaveOptions.DONOTSAVECHANGES);
break; // Once matched, move to the next source file
}
}
}
} else {
alert("Please select both Source and Destination folders.");
}
~ appreciate any help!
I have a script which I feel is so close to working but I'm at the point where I'm pulling my hair out.
Let me give you a rundown.
I have a folder of Source PSD and a folder of Destination TIFF files.
The script runs and opens a source file then searches the Destination folder for a file which contains the name of the Source file and opens that.
I then need the script to copy the background layer from the Destination TIFF file over to the Source file
Delete Layer 1 in the Source file (Copied layer should be named Layer 2)
Save as PSD in Destination Folder using the same name as original Destination file
Close open docs
Then repeat for all other Source files
Right now it seems to want to save the original Destination file and refuses to cycle through the additional Source images
The script I have is this:-
// Prompt user to select Source and Destination folders
var sourceFolder = Folder.selectDialog("Select Source Folder");
var destFolder = Folder.selectDialog("Select Destination Folder");
if (sourceFolder && destFolder) {
var sourceFiles = sourceFolder.getFiles("*.psd");
var destFiles = destFolder.getFiles("*.tif");
for (var i = 0; i < sourceFiles.length; i++) {
var sourceFile = sourceFiles;
var sourceFileName = sourceFile.name.replace(/\.[^.]+$/, ""); // Remove extension
for (var j = 0; j < destFiles.length; j++) {
var destFile = destFiles[j];
var destFileName = destFile.name.replace(/\.[^.]+$/, ""); // Remove extension
if (destFileName.indexOf(sourceFileName) !== -1) {
var sourceDoc = app.open(sourceFile);
var destDoc = app.open(destFile);
// Delete Layer 1 in source document
if (sourceDoc.layers.length > 0) {
sourceDoc.layers[sourceDoc.layers.length - 1].remove();
}
// Duplicate the background layer from destination
var destBackgroundLayer = destDoc.artLayers[0];
var sourceLayer = destBackgroundLayer.duplicate(sourceDoc, ElementPlacement.PLACEATBEGINNING);
// Loop through layers in source document
for (var k = sourceDoc.layers.length - 1; k >= 0; k--) {
var sourceChildLayer = sourceDoc.layers[k];
var sourceParentLayer = sourceChildLayer.parent;
// Check if the source layer is a clipping mask
if (sourceChildLayer.grouped && sourceParentLayer) {
// Find the corresponding parent layer in destination
var destParentLayerName = sourceParentLayer.name;
var destParentLayer = destDoc.layers.getByName(destParentLayerName);
if (destParentLayer) {
sourceChildLayer.duplicate(destParentLayer, ElementPlacement.PLACEATBEGINNING);
}
}
}
// Save modified source file in destination folder with PSD file name
var newFileName = sourceFileName + "_modified.psd";
sourceDoc.saveAs(new File(destFolder + "/" + newFileName), SaveOptions.PSD);
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
destDoc.close(SaveOptions.DONOTSAVECHANGES);
break; // Once matched, move to the next source file
}
}
}
} else {
alert("Please select both Source and Destination folders.");
}
~ appreciate any help!