I've developed a code that copies images from one folder to another, matching them with their respective PSD file names. Each image with the same name is pasted as a new layer into the corresponding PSD with the same name. The condition checking if names are matched is:
if(fileListString.search(workingDocName) !== -1)
Everything was working fine until I added a condition to prevent adding images to the wrong PSDs in the previous if statement:
globals.markupFileNotFound = false;
And later if the condition is false:
else {globals.markupFileNotFound = true;}
Previously, there was a bug which ran the script and added the last copy of a matched image from the source folder (markup locations) to all remaining PSDs in the target folder (working locations). For example, we have 001.PSD, 002.PSD, 003.PSD and 001.JPG, 002.JPG. Without the additional new condition, 003.PSD would get a copy of the image from 002.JPG, and any next PSD file would get it too.
With the new condition, only 002.PSD gets the image 002.JPG, but the previous 001.PSD does not, even when it has 001.JPG respectively. And the needed conditions worked:
if(fileListString.search(workingDocName) !== -1) = true
globals.markupFileNotFound = false;
So the code below should work, but it's not:
//Paste the markups onto the working document
if(globals.markupFileNotFound == false) {
//Create the blank layer
var blankLayer = openDoc.artLayers.add();
//Rename the layer to blank Layer
blankLayer.name = "markups";
//paste the markups onto the markups layer
workingDoc.paste();
}
As far as I know, this code is supposed to work in newer versions of Photoshop CC.
[Link to folder structure]
#target photoshop
globals = {};
main();
function main() {
//Create a dialog box to get the details of where the markups and the working are stored
var dialogPrefs = "dialog{statictext:StaticText{bounds:[10,10,240,27], text:'Set the folder location of the markups'}, " +
"markupsButton:Button{bounds:[10,80,190,101], text:'Markups location'}, " +
"statictext:StaticText{bounds:[130,10,390,27], text:'Set the folder location of the working files'}," +
"workingButton:Button{bounds:[205,80,390,101], text:'Working location'}, " +
"transferButton:Button{bounds:[205,120,390,141], text:'Transfer markups'}, " +
"cancelButton:Button{bounds:[205,160,390,181], text:'Cancel'}};"
var windowFileLocation = new Window(dialogPrefs, "Set file locations");
//This is the markup window button
windowFileLocation.markupsButton.onClick = function() {
globals.markupFolder = Folder.selectDialog("Select markup location");
}
//Store the location of the markup files
//This is the working window button
windowFileLocation.workingButton.onClick = function() {
globals.workingFolder = Folder.selectDialog("Select working folder location");
}
//Store the location of the markup files
//This is the transfer button
windowFileLocation.transferButton.onClick = function() {
//Compare both folders to find the files with the same names and transfer markups
//Check both locations to make sure that they are valid
if (globals.markupFolder === null){
alert("You have not selected the markups folder. Please select and try again");
} else if (globals.workingFolder === null){
alert("You have not selected the working folder. Please select and try again");
} else {
//Define an empty array to store the file names in
var workingFileNameArray = [];
//Get a list of all the files in the working folder
var fileList = globals.workingFolder.getFiles();
for(var a = 0; a < fileList.length; a++) {
//check to see if the fileList item is a file or folder
if(fileList[a] instanceof File) {
//Converting filename to a string
var fileListString = fileList[a].toString();
if(fileListString.match(/.(jpg|tif|psd|bmp|gif|png|ico)$/)) {
workingFileNameArray[a] = fileList[a];
//open the file in photoshop
var openDoc = open(workingFileNameArray[a]);
//Make a variable containing the active document
var workingDoc = app.activeDocument;
//get the name of the file and cut the extension
var workingDocName = ((workingDoc.name).toString()).slice(0, -4);
//getting the color profile of the working file
var targetProfile = workingDoc.colorProfileName;
//Start working markups
searchMarkups(workingDocName, targetProfile);
//Paste the markups onto the working document
if(globals.markupFileNotFound == false) {
//Create the blank layer
var blankLayer = openDoc.artLayers.add();
//Rename the layer to blank Layer
blankLayer.name = "markups";
//paste the markups onto the markups layer
workingDoc.paste();
}
//Save document
workingDoc.save();
//Close the document
workingDoc.close();
}
}
}
alert("All markups have been transferred");
windowFileLocation.close();
}
}
//Cancel button
windowFileLocation.show();
}
function searchMarkups(workingDocName, targetProfile) {
//This is a function that will find the markup files that match the working file
//Define an empty array to store the file names in
var workingFileNameArray = [];
//Define an empty array to store the file names in
var fileList = globals.markupFolder.getFiles();
for(var a = 0; a < fileList.length; a++){
//check to see if the fileList item is a file or folder
if(fileList[a] instanceof File) {
//Converting filename to a string
var fileListString = fileList[a].toString();
if(fileListString.match(/.(jpg|tif|psd|bmp|gif|png|ico)$/)) {
//Check the name of the open working file against all of the files in the markups folder and find one that matches
if(fileListString.search(workingDocName) !== -1){
//open that file
var openDoc = open(fileList[a]);
//Convert the markup file to match the profile on the working
openDoc.convertProfile(targetProfile, Intent.RELATIVECOLORIMETRIC, true, true);
//Select the whole canvas
openDoc.selection.selectAll();
//Add a new blank layer to the file
var blankLayer = openDoc.artLayers.add();
//Rename the layer to blank Layer
blankLayer.name = "blankLayer";
//copy merge
openDoc.selection.copy(true);
//Remove the blank layer
openDoc.layers.getByName("blankLayer").remove();
globals.markupFileNotFound = false;
//close the document
openDoc.close(SaveOptions.DONOTSAVECHANGES);
} else {
globals.markupFileNotFound = true;
}
}
}
}
}
Thanks in advance. Additional credits to code author jamesmcdonald3d.com