如果 PSD 文件中已经存在具有特定名称的文件夹,是否可以添加条件?

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

我的规格:CS6 64 位版本 13

我在快捷脚本中添加了一个文件夹中具有相同名称的图像副本 markupFiles,并将具有相同名称的 PSD workingPSD 添加到另一个文件夹中。

它运行良好,我对结果很满意,但我想将每个 PSD 中的所有现有标记层添加到一个名为 "markups" 的文件夹中。

为此,我需要条件检查名称为 "markups" markupFolder 的文件夹是否存在于 workingPSD 中。
如果是 true 我将图层 workingPSDlayer 添加到之前添加的文件夹 workingPSDFolder 中。
如果是 false 我只是将层 workingPSDlayer 添加到已经存在的 "markup" 文件夹 markupFolder

那么,如果某个 PSD 有某个文件夹,是否有可能添加条件?
在此示例中,如果 workingPSD 已经有任何名为 "markup"

的文件夹 不幸的是,这个声明不起作用:

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

在此上下文中:

    //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]

整个代码:

#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();

}

提前致谢。代码作者的额外学分 jamesmcdonald3d.com

好的,我找到了解决方案。它不太优雅,但我工作。如果当前工作的 PSD 有任何相应的标记文件,我从 //Adding markup layers into working PSD 复制循环以获取逻辑值 var ifPSDHasMatchedMarkups = true;
当此值为 true 时,我 运行 条件 //Adding markups folder to working PSD 添加名为 "markups"

的文件夹 唯一的问题是,当您再次 运行 编写脚本时,您无法检查您是否已经在名为 "markups" 的工作 PSD 文件夹中。您每次都创建一个新的。

#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();

}