脚本 Photoshop CS6 版本 13 64 位中的计数器问题

Problem with counter in script Photoshop CS6 ver 13 64 bit

我有脚本
PSD (activeDocument) in "3.Working folder
saves jpg
in draft folders in directory "../UserNameJobName/4.WIP/"

问题是一切正常,直到我想创建第三个草稿文件夹 (draft003)。
然后它再次将 activeDocument 保存在 draft002 的同一个 jpg 文件中,而不是创建新文件夹 draft003 并分别保存在那里。

接缝var draftCounter不能高于2值。我不确定错误在哪里。据我所知,在 photoshop CC 中它可以正常工作。

[Link 到文件夹结构 zip]
https://www.dropbox.com/s/kbalzsubs2477jy/191130_WARGAMES_LUKKAR.zip?dl=0

Main file directory pic
Draft directory pic


代码

#target photoshop;

var wipCounter = 1;
var fileNotFound = true;
main();

function main(){
    //Get file information
    var doc = app.activeDocument;
    var docName = doc.name;
    var docPath = doc.path;

    //Get the percentage to scale by
    var scaleValue = prompt("Enter the WIP JPEG scale percentage", "100");

    //Figure out the location of the WIP folder
    var savePath = docPath.toString();
    savePath = savePath.slice(0,-10);
    savePath = savePath + "/4.WIP/";
    var currDoc = docName.toString();
    currDoc = currDoc.slice(0,-4);

    //global variables are defined here

    //Look at the WIP folder and run a search for anything containing the word "draft"
    //Define the location of the WIP folder 
    var inWIPFolder = new Folder(savePath);
    //Look in each folder and create an array containing all of the information
    var fileList = inWIPFolder.getFiles();
    // Look at each array and sort the info out 
    for(var a=0; a<fileList.length; a++) {
            //Exclude anything that is not a folder
            if(fileList[a] instanceof Folder) {
                //convert each foldername to string for editing
                var fileName = fileList[a].toString();
                var draftSearch = fileName.search("draft");
                if(draftSearch > -1) {
                    var draftCounter  = fileName.slice(draftSearch);
                    if(fileNotFound == true) {
                        searchDraftFolder(draftCounter, savePath, currDoc);
                        }
                }
            }
        }


        //Formatting the numbers
        if(wipCounter < 10){
            wipCounter = "00" + wipCounter.toString();
         }else if(wipCounter >= 10 && wipCounter < 100) {
             wipCounter = "0" + wipCounter.toString();  
         }

        //Define the path of the draft folders
        savePath = Folder(savePath +"/draft" + wipCounter);
        //Check to see if the draft folders exists. If not, make one
        if(!savePath.exists){
            savePath.create();
            }

        //Define the WIP file name with the incremental counter
        var wipFileName =savePath + "/" + currDoc + "_WIP" + wipCounter + ".jpg";

        //Resize the file
        doc.selection.selectAll();
        var blankLayer = doc.artLayers.add();
        blankLayer.name = "blankLayer";
        doc.selection.copy(true);
        doc.layers.getByName("blankLayer").remove();

        //Make new doc to paste into
        app.preferences.rulerUnits = Units.PIXELS;
        //arguments are: Width, Height, resolution, filename, colourspce, documentfill
        app.documents.add(UnitValue(doc.width, "PX"), UnitValue(doc.height, "PX"), doc.resolution, wipFileName, NewDocumentMode.RGB, DocumentFill.TRANSPARENT,1);
        app.activeDocument.paste();
        app.activeDocument.resizeImage(UnitValue(scaleValue, "PERCENT"), null, null, ResampleMethod.BICUBICSHARPER);
        //Save a RGBJpeg
        jpegFileSaver(app.activeDocument, new File(wipFileName), 10);
        //Close the new document down
        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        //Deselect
        doc.selection.deselect();
        alert(currDoc + "WIP" + wipCounter + " saved.");
}


function searchDraftFolder(draftCounter, savePath, currDoc){
   var fileSearch = -1;
   //Define the location of each draft folder  
   var internalFolder = new Folder(savePath + "/" + draftCounter);
   //Look in each folder and create an array containing all of the information
   var internalFileList = internalFolder.getFiles();
   // Look at each array and sort the info out 
   for(var a=0; a<internalFileList.length; a++) {
        //Exclude anything that is not a file
       if(internalFileList[a] instanceof File) {
           // convert each filename to string for editing
            var fileName = internalFileList[a].toString();
            //Searching the current document name and replacing spaces with %20
            var currDocStripped = currDoc.replace(/ /g, '%20');
            //Checking files until a result greater than 0 appears
            if(fileSearch === -1){
                //search for the current file name
                fileSearch = fileName.search(currDocStripped);
            }
           }
       }
   //if the filesearch does not return a result:
    if(fileSearch === -1){
        fileNotFound = true;
     //if it does return a result:
    } else {
        //Slice the number off the draft folder
        draftCounter = draftCounter.slice(-3);
        //convert the string into an integer
        draftCounter = parseInt(Number(draftCounter));
        //increment the number up
        wipCounter++;
        //terminate the main loop
        fileNotFound = false;
        }   
    }


//A resuable JPEG save script
function jpegFileSaver(doc, saveFile, quality){
    //define the save options
    var saveOptions = new JPEGSaveOptions();
    saveOptions.embedColorProfile = true;
    saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    saveOptions.quality = quality;
    //save the files
    doc.saveAs(saveFile, saveOptions, true);
    }

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

问题出在 searchDraftFolder()。当它在 draft001 中找到已经保存的 Great Map_wip 时,fileNotFound 被设置为 false 并且您在第 39 行的循环终止。这就是 wipCounter 卡在 2 上的原因:它递增 1,仅此而已。

这里我稍微重写了这个函数,试图让它更简单:所有必要的循环都在函数内部,我认为它更容易理解。

p.s。至少在 CC 上还有一个问题:app.documents.add() 正在显示界面。那是因为您使用的是完整路径作为新文档名称,而您不能在其中使用 \-符号,因此 Photoshop 建议使用不同的名称并显示它。我为没有路径的文档名称添加了一个单独的变量(第 49 行)以在 .add()

中使用
main();

function main()
{
  //Get file information
  var doc = app.activeDocument;
  var docName = doc.name;
  var docPath = doc.path;
  var wipCounter;

  //Get the percentage to scale by
  var scaleValue = prompt("Enter the WIP JPEG scale percentage", "100");

  //Figure out the location of the WIP folder
  var savePath = docPath.toString();
  savePath = savePath.slice(0, -10);
  savePath = savePath + "/4.WIP/";
  var currDoc = docName.toString();
  currDoc = currDoc.slice(0, -4);

  //global variables are defined here

  //Look at the WIP folder and run a search for anything containing the word "draft"
  //Define the location of the WIP folder 
  var inWIPFolder = new Folder(savePath);

  // !! modifications
  wipCounter = searchDraftFolder(inWIPFolder, currDoc)

  //Formatting the numbers
  if (wipCounter < 10)
  {
    wipCounter = "00" + wipCounter.toString();
  }
  else if (wipCounter >= 10 && wipCounter < 100)
  {
    wipCounter = "0" + wipCounter.toString();
  }

  //Define the path of the draft folders
  savePath = Folder(savePath + "/draft" + wipCounter);
  //Check to see if the draft folders exists. If not, make one
  if (!savePath.exists)
  {
    savePath.create();
  }

  //Define the WIP file name with the incremental counter
  var wipFileName = currDoc + "_WIP" + wipCounter + ".jpg";
  var wipFilePath = savePath + "/" + wipFileName;

  //Resize the file
  doc.selection.selectAll();
  var blankLayer = doc.artLayers.add();
  blankLayer.name = "blankLayer";
  doc.selection.copy(true);
  doc.layers.getByName("blankLayer").remove();

  //Make new doc to paste into
  app.preferences.rulerUnits = Units.PIXELS;
  //arguments are: Width, Height, resolution, filename, colourspce, documentfill
  app.documents.add(UnitValue(doc.width, "PX"), UnitValue(doc.height, "PX"), doc.resolution, wipFileName, NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
  app.activeDocument.paste();
  app.activeDocument.resizeImage(UnitValue(scaleValue, "PERCENT"), null, null, ResampleMethod.BICUBICSHARPER);
  //Save a RGBJpeg
  jpegFileSaver(app.activeDocument, new File(wipFilePath), 10);
  //Close the new document down
  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  //Deselect
  doc.selection.deselect();
  alert(currDoc + "_WIP" + wipCounter + " saved.");
}

// p is WIP path
// n is a name of the active doc without extension
function searchDraftFolder(p, n)
{
  var fileList = p.getFiles(); // folders in wip folder
  var counter = 1;
  var drafts, fileName, i, k;

  // for all the files found..
  for (i = 0; i < fileList.length; i++)
  {
    //if a folder is found and its name has 'draft' in it
    if (fileList[i] instanceof Folder && fileList[i].name.indexOf('draft') != -1)
    {

      //get files inside this draft folder
      drafts = fileList[i].getFiles();

      // for all files inside
      for (k = 0; k < drafts.length; k++)
      {
        //'.name' gives us a URI-name, so replacing %20 with ' '. Probably should add more special symbols here if you use them
        fileName = drafts[k].name.replace(/%20/g, ' ');

        // if there's a file that starts with active doc name counter is incremented
        if (fileName.indexOf(n) != -1)
        {
          counter++
        }
      }
    }
  }

  return counter;
}


//A resuable JPEG save script
function jpegFileSaver(doc, saveFile, quality)
{
  //define the save options
  var saveOptions = new JPEGSaveOptions();
  saveOptions.embedColorProfile = true;
  saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  saveOptions.quality = quality;
  //save the files
  doc.saveAs(saveFile, saveOptions, true);
}