Problem with counter in script Photoshop CS6 ver 13 64 bit

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators:xbytor, DavideBarranca, Paul MR

Post Reply
Lukkar
Posts:4
Joined:Fri Dec 06, 2019 12:24 pm
Problem with counter in script Photoshop CS6 ver 13 64 bit

Post by Lukkar » Fri Dec 06, 2019 12:38 pm

I have script which creates jpg copies of activeDocument file in draft folders in directory "../UserNameJobName/4.WIP/"

The original file PSD(activeDocument) to make copies of it, is in "3.Working" directory folder.

The problem is that everything works fine until I want to to create third draft folder (draft003). It saved again activeDocument in the same jpg file in draft002, instead of creating new one folder and saving in that.
It seams that var draftCounter can't be higher than 2 value. I'm not sure where is a bug. What I know in photoshop CC it works without any problems.

[Link to folder structure zip]
https://www.dropbox.com/s/kbalzsubs2477 ... R.zip?dl=0

Main file directory pic

Image

Draft directory pic

Image

Code: Select all

#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);
    }
    
Thanks in advance for any help
Additional credits to code author http://jamesmcdonald3d.com/

Lukkar
Posts:4
Joined:Fri Dec 06, 2019 12:24 pm

Re: Problem with counter in script Photoshop CS6 ver 13 64 bit

Post by Lukkar » Mon Dec 09, 2019 11:16 am


Post Reply