Problem with flag validator in code CS6 64bit ver 13

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators:xbytor, DavideBarranca, Paul MR

Post Reply
Lukkar
Posts:4
Joined:Fri Dec 06, 2019 12:24 pm
Problem with flag validator in code CS6 64bit ver 13

Post by Lukkar » Fri Dec 06, 2019 1:57 pm

I have code which makes copies of images from /2.Supplied/ into PSDs' new layer in /3.Working/ folder, with the same names. Both folders you can choose in dialog.
Everything worked fined, until I added flag which prevents adding images(markups) into wrong PSDs.
Previously there was bug which runs script and add last copy of image(markup) from /2.Supplied folder, even if PSD file in /3.Working folder was not matched, to later PSDs.
For example we have 001.PSD, 002.PSD, 003.PSD and 001.JPG, 002.JPG.
Without flag 003.PSD get image from 002.JPG. The flag should prevent this.

But instead of it it cancel any previous pasting markups into PSD, until last matching PSD. Not sure where bug is.
This code supposes to work in newer CC PS versions.

[Link to folder structure zip]
https://www.dropbox.com/s/qup9zemcolcan ... s.zip?dl=0

Main folder

Image

Old working code without flag

Code: Select all

#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
                        //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();
                    //close the document
                    openDoc.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
        }
    }
    
}
Script with flag

Code: Select all

#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
                        var valueCheck = (globals.markupFileNotFound == false);
                        alert("validation if you can paste: " + valueCheck +
                        "\n" + "(globals.markupFileNotFound == false): " + valueCheck +
                        "\n" + "globals.markupFileNotFound: " + globals.markupFileNotFound);
                        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();
                            alert("pasting into file");
                        }
                        //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
                var valueCheck = (fileListString.search(workingDocName) !== -1);
                alert("Inside function searchMarkups\n" + "checking if files are matched: " + valueCheck + "\n" +  "fileListString.search(workingDocName) !== -1: " + valueCheck);
                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);
                    alert("copied");
                    //Remove the blank layer
                    openDoc.layers.getByName("blankLayer").remove();
                    globals.markupFileNotFound = false;
                    alert("If statement: true\nglobals.markupFileNotFound: " + globals.markupFileNotFound);
                    //close the document
                    openDoc.close(SaveOptions.DONOTSAVECHANGES);
                } else {
                    globals.markupFileNotFound = true;
                    alert("If statement: false => else\nglobals.markupFileNotFound: " + globals.markupFileNotFound);
                }
            }
        }
    }
    
}
Thanks in advance.
Additional credits to code author jamesmcdonald3d.com

Lukkar
Posts:4
Joined:Fri Dec 06, 2019 12:24 pm

Re: Problem with flag validator in code CS6 64bit ver 13

Post by Lukkar » Mon Dec 09, 2019 11:18 am


Post Reply