2

I have code which makes copies of images from one folder to another folder into theirs respective PSDs.
Each image with the same name is pasted as new layer into another PSD with the same name.
Condition checking if names are matched is

if(fileListString.search(workingDocName) !== -1)

Everything worked fined, until I added condition which prevents adding images into wrong PSDs in previous if statement

globals.markupFileNotFound = false; 

and later if condition is false

else {globals.markupFileNotFound = true;}

Previously there was bug which runed script and add last copy of matched image from source folder (markups locations), to all remaining PSDs in target folder (working locations)

For example we have 001.PSD, 002.PSD, 003.PSD and 001.JPG, 002.JPG. Without additional new condition 003.PSD gets copy of image from 002.JPG. And any next PSD file gets it too.

So with new condition it comes out that only 002.PSD gets image 002.JPG.
But previous 001.PSD do not, even when it has 001.JPG respectivly. And needed conditions worked if(fileListString.search(workingDocName) !== -1) = true
globals.markupFileNotFound = false;

So 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();
                        }

What i know this code supposes to work in newer CC PS versions.

[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 gain");
        } else if (globals.workingFolder === null){
            alert("You have not selected the working folder. Please select and try gain");
        } else {
            //Define and empty array to store the file names in
            var workingFileNameArray = [];
            //Get a list of all the iles in the working folder
            var fileList = globals.workingFolder.getFiles();
            for(var a = 0; a < fileList.length; a++) {
                //check to see if hte 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 containg 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 and empty array to store the file names in
    var fileList = globals.markupFolder.getFiles();
    for(var a = 0; a < fileList.length; a++){
        //checck 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

5
  • I think if you were be able to narrow down to the part that doesn't work it'd be much easier to answer the question. 3 script files with tons of things that happen might scare away people Dec 6, 2019 at 16:51
  • 1
    So any image name 001 should be added as a layer only in a PSD file named 001? Same logic for 002? Dec 6, 2019 at 18:59
  • Yes, any image named 001 should be added in PSD named 001
    – Lukkar
    Dec 6, 2019 at 19:15
  • And you want to transfer "markups" images into "working" PSDs, correct? Dec 6, 2019 at 19:48
  • Yes, exactly this is what I want to do
    – Lukkar
    Dec 6, 2019 at 20:00

1 Answer 1

1

Try this code. Please let me know how it goes. I'm pretty sure that .toString() on a file is giving you a full path, so be careful that the names of yours folders don't contain the same characters as your file names. I think Photoshop's open() can accept a reference to a file but it might need a path instead ...

#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 workingFilesPaths = [];
            //Get a list of all the files in the working folder
            var workingFiles = globals.workingFolder.getFiles();
            for(var a = 0; a < workingFiles.length; a++) {
                //check to see if the workingFiles item is a file or folder
                if(workingFiles[a] instanceof File) {
                    //Converting filename to a string
                    var workingFilePath = workingFiles[a].toString();
                    // if(fileListString.match(/.(jpg|tif|psd|bmp|gif|png|ico)$/)) {
                        if(workingFilePath.match(/.psd$/)) {
                            workingFilesPaths[a] = workingFilePath;
                        //open the file in photoshop
                        var openWorkingPSD = open(workingFiles[a]);
                        //Make a variable containg the active document
                        var workingPSD = app.activeDocument;
                        //get the name of the file and cut the extension
                        var workingPSDname = ((workingPSD.name).toString()).slice(0, -4);
                        //getting the color profile of the working file
                        var workingPSDcolorProfile = workingPSD.colorProfileName;

                        //Start working markups
                        transferMatchingMarkupsToWorkingPSD(workingPSD,workingPSDname, workingPSDcolorProfile);


                    }
                }
            }
        alert("All markups have been transferred");
        windowFileLocation.close();
        }
    }

    //Cancel button
    windowFileLocation.show();
}

function transferMatchingMarkupsToWorkingPSD(workingPSD,workingPSDname, workingPSDcolorProfile) {
    //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 markupFilesPaths = [];
    //Define and empty array to store the file names in
    var markupFiles = globals.markupFolder.getFiles();
    for(var a = 0; a < markupFiles.length; a++){
        //checck to see if the fileList item is a file or folder
        if(markupFiles[a] instanceof File) {
            //Converting filename to a string
            var markupFilePath = markupFiles[a].toString();
            if(markupFilePath.match(/.(jpg|tif|psd|bmp|gif|png|ico)$/)) {
                //Check the name of the open working PSD against all of the files in the markups folder and find those that match
                if(markupFilePath.search(workingPSDname) !== -1){
                    //open that file
                    var openMarkupFile = open(markupFiles[a]);
                    //Convert the markup file to match the profile on the working
                    openMarkupFile.convertProfile(workingPSDcolorProfile, Intent.RELATIVECOLORIMETRIC, true, true);
                    //Select the whole canvas
                    openMarkupFile.selection.selectAll();

                    //copy merge
                    openMarkupFile.selection.copy(true);
                    //Create the blank layer in working PSD
                    activeDocument = workingPSD;
                    var workingPSDlayer = workingPSD.artLayers.add();
                    //Rename the layer
                    workingPSDlayer.name = "markups";
                    //paste the markups onto the markups layer
                    workingPSD.paste();


                    //close the markup file
                    openMarkupFile.close(SaveOptions.DONOTSAVECHANGES);

                }
            }
        }
    }

    //Save document
workingPSD.save();
//Close the document
workingPSD.close();

}
6
  • 2
    Thanks Sergey for that Dec 6, 2019 at 20:58
  • 1
    Also: if a markup file is a PSD, I think this script will only copy+paste layer 1 Dec 6, 2019 at 21:25
  • Also I found second bug. When markup PSD has only "background" layer, you can't copy layer and script stop. I will try to fix too.
    – Lukkar
    Dec 7, 2019 at 11:26
  • Next possible bug was with ico format in 87 line in "if(markupFilePath.match(/.(jpg|tif|psd|bmp|gif|png|ico)$/))" Photoshop doesn't read ico format.
    – Lukkar
    Dec 7, 2019 at 11:47
  • "Also: if a markup file is a PSD, I think this script will only copy+paste layer 1" No, it copies everything what you see. It meas it marges all layers and copies image as intended, because it has additional value true in statement openMarkupFile.selection.copy(true); This value enables additional merge all layer abillity.
    – Lukkar
    Dec 7, 2019 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.