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

标签 javascript photoshop-script

我有脚本
3.Working 文件夹
中的 PSD (activeDocument) 将 jpg
保存在目录“../UserNameJobName/4.WIP/”的草稿文件夹中

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

这表明 var DraftCounter 不能高于 2 值。我不确定哪里有错误。据我所知,在 Photoshop CC 中它可以正常工作,没有任何问题。

[文件夹结构 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,仅此而已。

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

附:至少在 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);
}

关于javascript - 脚本 Photoshop CS6 版本 13 64 位中的计数器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59212976/

相关文章:

javascript - Power BI iframe窃取页面焦点

javascript - 椭圆选框选择,然后使用 Javascript 在 Photoshop 中填充颜色

photoshop-script - Photoshop Javascript:如何获取/设置当前工具?

javascript - 括号之间的简单 JavaScript 函数

javascript - Photoshop 脚本 - 未正确调整大小

Javascript Photoshop : Syntax for changing text color of an already existing text layer

javascript - 如何在 Photoshop 脚本中设置文件夹?

javascript - 如何检查用户是否可以访问某个 URL?

javascript - 如何在将鼠标悬停在元素类或 id 上时调用 JavaScript?

javascript - 405 方法不允许使用 Tomcat