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

问题描述 投票:0回答:1

我的规格:CS6 64bit ver 13

我在快捷方式脚本中将一个文件夹中具有相同名称的图像markupFiles的副本添加到另一个文件夹中具有相同名称的PSD workingPSD中。它运行良好,我对结果感到满意,但我想将每个PSD中的所有现有标记层添加到一个称为“标记”的文件夹中。为此,我需要条件检查markupFolder中是否存在名称为“ markups” workingPSD的文件夹。如果是true,则将图层workingPSDlayer添加到先前添加的workingPSDFolder文件夹中。如果是false,我只需将图层workingPSDlayer添加到已经存在的“标记”文件夹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

javascript photoshop-script
1个回答
0
投票

好,我找到了解决方案。它不太优雅,但我可以工作。如果当前工作的PSD有任何对应的标记文件,我从//Adding markup layers into working PSD复制了循环以获取逻辑值var ifPSDHasMatchedMarkups = true;当此值为true时,我运行条件//Adding markups folder to working 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();

}
© www.soinside.com 2019 - 2024. All rights reserved.