Search code examples
javascriptphotoshop-script

Is it possible to add condition if folder with particular name exists already in PSD file?


My specs: CS6 64bit ver 13

I have in shortcut script which add copies of images markupFiles with the same names from one folder, to PSDs workingPSD with the same names into another folder.

It worked fine and I was glad with the outcome but I wanted to add all existing markup layers in each PSD into one folder called "markups".

To do that I need condition checking if folder with name "markups" markupFolder exists in workingPSD.
If it is true I add layer workingPSDlayer into folder previously added workingPSDFolder.
If it is false I just add layer workingPSDlayer into already existing "markup" folder markupFolder

So is there any possibility to add condition if certain PSD has certain folder?
In this example if workingPSDhas already any folder named "markup"

Unfortunately this statement doesn't work:

if(workingPSD.LayerSets.getByName("markups") === markupFolder)

In this context:

    //Markups folder search outcome
    var markupFolder = workingPSD.LayerSets.getByName("markups");
    //Checking if folder markups already exists
    if(workingPSD.LayerSets.getByName("markups") === markupFolder) {
          //adding working PSD layer into folder
          var workingPSDlayer = markupFolder.artLayers.add();
    //If there is no markup folder yet
    } else {
          //Creating new markup folder
          var workingPSDFolder = workingPSD.layerSets.add();
          //Naming it
          workingPSDFolder.name = "markups";
          //Adding layer into it.
          var workingPSDlayer = workingPSDFolder.artLayers.add();

[Link to folder structure]

the whole code:

#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)$/)) {
                //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);
                    //Adding layer to PSD file to anable coping when only background layer exists
                    if(markupFilePath.match(/.(psd)$/)) {openMarkupFile.artLayers.add();}
                    //Select the whole canvas
                    openMarkupFile.selection.selectAll();

                    //copy merge
                    openMarkupFile.selection.copy(true);

                //Adding copy of markup image into working PSD
                    //Create the blank layer in working PSD
                    activeDocument = workingPSD;
                    //Markups folder search outcome
                    var markupFolder = workingPSD.LayerSets.getByName("markups");
                    //Checking if folder markups already exists
                    if(workingPSD.LayerSets.getByName("markups") === markupFolder) {
                        //adding working PSD layer into folder
                        var workingPSDlayer = markupFolder.artLayers.add();
                    //If there is no markup folder yet
                    } else {
                        //Creating new markup folder
                        var workingPSDFolder = workingPSD.layerSets.add();
                        //Naming it
                        workingPSDFolder.name = "markups";
                        //Adding layer into it.
                        var workingPSDlayer = workingPSDFolder.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();

}

Thanks in advance. Additional credits to code author jamesmcdonald3d.com


Solution

  • Ok I found solution. It is less elegant, but i works. I copied loop from //Adding markup layers into working PSD to get logic value if current working PSD has any corresponding markup files var ifPSDHasMatchedMarkups = true;
    When this value is truethen I run condition //Adding markups folder to working PSDto add folder called "markups"

    The only problem is when you run script again you have no possibility to check if you have already in working PSD folder called "markups". You create new one each time.

    #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 = [];
    
        ///Defining condition for adding markups folder into working PSD
        //Define and empty array to store the file names in
        var markupFiles = globals.markupFolder.getFiles();
        for(var a = 0; a < markupFiles.length; a++){
            //check 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)$/)) {
                    //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){
                        var ifPSDHasMatchedMarkups = true;
                    }}}}
    
        //Adding markups folder to working PSD
        if(ifPSDHasMatchedMarkups === true) {
            activeDocument = workingPSD;
            var workingPSDFolder = workingPSD.layerSets.add();
            workingPSDFolder.name = "markups";
        }
    
        //Adding markup layers into working PSD
        for(var a = 0; a < markupFiles.length; a++){
            //check 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)$/)) {
                    //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);
                        //Adding layer to PSD file to anable coping when only background layer exists
                        if(markupFilePath.match(/.(psd)$/)) {openMarkupFile.artLayers.add();}
                        //Select the whole canvas
                        openMarkupFile.selection.selectAll();
    
                        //copy merge
                        openMarkupFile.selection.copy(true);
                        //Create the blank layer in working PSD
                        activeDocument = workingPSD;
                        var workingPSDlayer = workingPSDFolder.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();
    
    }