仅向脚本 CS6 ver 13 中的最后一个 PSD 文件添加图层 Bug

Adding layer only to the last PSD file in script CS6 ver 13 Bug

我有代码可以将图像从一个文件夹复制到另一个文件夹并放入它们各自的 PSD 中。
每个具有相同名称的图像都作为新图层粘贴到另一个具有相同名称的 PSD 中。
检查名称是否匹配的条件是

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

一切正常,直到我在之前的 if 语句

中添加了防止将图像添加到错误 PSD 的条件
globals.markupFileNotFound = false; 

以后如果条件为假

else {globals.markupFileNotFound = true;}

以前存在运行脚本并将匹配图像的最后一个副本从源文件夹(标记位置)添加到目标文件夹(工作位置)中所有剩余的 PSD

例如,我们有 001.PSD、002.PSD、003.PSD001.JPG、002.JPG。 没有额外的新条件 003.PSD002.JPG 获取图像的副本。任何下一个 PSD 文件也会得到它。

所以在新条件下,只有 002.PSD 得到图像 002.JPG
但是之前的 001.PSD 没有,即使它分别有 001.JPG。 所需条件奏效 if(fileListString.search(workingDocName) !== -1) = true
globals.markupFileNotFound = false;

所以下面的代码应该可以工作,但不是

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

据我所知,这段代码应该可以在较新的 CC PS 版本中运行。

[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;
                }
            }
        }
    }

}

提前致谢。 额外感谢代码作者 jamesmcdonald3d.com

试试这个代码。请告诉我进展如何。我很确定文件上的 .toString() 会为您提供完整路径,因此请注意您文件夹的名称不包含与文件名相同的字符。我 认为 Photoshop 的 open() 可以接受对文件的引用,但它可能需要一个路径...

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

}