search files give warning if folder contains more than one

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

lukasz

search files give warning if folder contains more than one

Post by lukasz »

I have this script searching a dir for psd and would like it to warn with a message if one of the sub directories has more than one file. any ideas?

I use Xbytor's xtools earlier in the script if that helps: http://sourceforge.net/project/showfile ... _id=207620 ... _id=207620

Code: Select all
...

var templateFile = File.openDialog("Select a template to use",'*.psd',true);
            var inputFolder = Folder.selectDialog("Select a folder of documents to process");
            var outputFolder2 = Folder.selectDialog("Select OUTPUT directory");
            var files = inputFolder.findFiles(/.psd$/i);

for( var d = 0; d < files.length; d++ ){

   app.open( files[d] );
   var sourceDoc = app.activeDocument;
   var format = getFormat();
   if( format == 'Photoshop' ){
        processPSD();
   }
}

...


Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

xbytor

search files give warning if folder contains more than one

Post by xbytor »

Put this

Code: Select allif (files.length > 1) {
  alert('There is more than one PSD file in folder ' + files.absoluteURI);
}

after the 'findFiles()' call.
lukasz

search files give warning if folder contains more than one

Post by lukasz »

Seems to give an undefined for the files.absoluteURI. when I alert files it gives me the files which sort of works, but I'm looking for the folder that it is contained in, assuming that's what absoluteURI does. can't seem to find anything in scripting guide or js reference manual on absoluteURI, but files seems to return an array. I did try to loop through the array and alert files.absoluteURI and that returns undefined as well. so not sure what to do.

also I have this really shotty fix for this script I have, it goes through opens PSD files and asks user if file needs to be inverted. but when it opens it shows the canvas blank, assuming it hasn't rendered the psd yet, so you can't even tell if it needs to be inverted.

my solution was to create a transform that they have to apply. but this is a really terrible solution because it causes them to have to do an extra unnecessary step. any ideas on how to fix this?



Code: Select all#target photoshop
app.bringToFront()   
// Helper functions for using Action Manager
cTID = function( s ) { return app.charIDToTypeID( s ); };// from Xbytor
sTID = function( s ) { return app.stringIDToTypeID( s ); };
Stdlib = function Stdlib() {};// from Xbytor's xtools: http://sourceforge.net/project/showfile ... _id=207620
Stdlib.convertFptr = function(fptr) {
  var f;
  if (fptr.constructor == String) {
    f = File(fptr);

  } else if (fptr instanceof File || fptr instanceof Folder) {
    f = fptr;

  } else {
    Error.runtimeError(19, "fptr");
  }
  return f;
};
Stdlib.getFolders = function(folder) {
  if (folder.alias) {
    folder = folder.resolve();
  }
  var folders = Stdlib.getFiles(folder,
                                function(f) { return f instanceof Folder; });
  return folders;
};
Stdlib.getFiles = function(folder, mask) {
   var files = [];

   folder = Stdlib.convertFptr(folder);

  if (folder.alias) {
    folder = folder.resolve();
  }
  var getF;
  if (Folder.prototype._getFiles) {
    getF = function(f, m) { return f._getFiles(m); }
  } else {
    getF = function(f, m) { return f.getFiles(m); }
  }

  if (mask instanceof RegExp) {
    var allFiles = getF(folder);
    for (var i = 0; i < allFiles.length; i = i + 1) {
      var f = allFiles;
      if (decodeURI(f.absoluteURI).match(mask)) {
        files.push(f);
      }
    }
  } else if (typeof mask == "function") {
    var allFiles = getF(folder);
    for (var i = 0; i < allFiles.length; i = i + 1) {
      var f = allFiles;
      if (mask(f)) {
        files.push(f);
      }
    }
  } else {
    files = getF(folder, mask);
  }

  return files;
};
Stdlib.findFiles = function(folder, mask) {
     var files = Stdlib.getFiles(folder, mask);
     var folders = Stdlib.getFolders(folder);

     for (var i = 0; i < folders.length; i++) {
       var f = folders;
       var ffs = Stdlib.findFiles(f, mask);
       // files.concat(ffs); This occasionally fails for some unknown reason (aka
       // interpreter Bug) so we do it manually instead
       while (ffs.length > 0) {
         files.push(ffs.shift());
         
         
     
           
         
         
         
       }
     }
     return files;
   };



Folder.prototype.findFiles = function(mask) {
     return Stdlib.findFiles(this, mask);

};
Stdlib.createFolder = function(fptr, interactive) {
  if (!fptr) {
    Error.runtimeError(19, "fptr");  // Bad Argument
  }

  if (fptr.constructor == String) {
    fptr = new Folder(fptr);
  }

  // XXX this needs testing
  if ((!fptr.exists || (fptr.parent && !fptr.parent.exists)) && interactive) {
    var f = (fptr instanceof File) ? fptr.parent : fptr;
    if (!confirm(f.toUIString() + " does not exist. Create?")) {
      return false;
    }
  }

  if (fptr instanceof File) {
    return Stdlib.createFolder(fptr.parent);
  }
  if (fptr.exists) {
    return true;
  }
  if (fptr.parent && !fptr.parent.exists) {
    if (!Stdlib.createFolder(fptr.parent)) {
      return false;
    }
  }
  return fptr.create();
};



var inputFolder = Folder.selectDialog("Select a folder of documents to process");
var files = inputFolder.findFiles(/.psd$/i);// get all psd files in this folder and subfolders

    ////////////////////////
    // if more than one file in folder or subfolders
    ////////////////////////


  if (files.length > 1) {
       
      alert('There is more than one PSD file in folder, please put them in separate folders ' + files.absoluteURI);
      var csv = new File(inputFolder+"/folders.txt");
      csv.open("w");
      csv.writeln(files.absoluteURI);
      // close the file
      csv.close();
     
      }



   
    for( var d = 0; d < files.length; d++ ){

   app.open( files[d] );
 
     
   var sourceDoc = app.activeDocument;
   var format = getFormat();
   //app.activeDocument = documents[0]; // close up doc
   var closedoc = app.activeDocument;
         doAction("view", "KKS");
 
  //////////////
  // Dialog box
  //////////////
  function goforit() {
         try{
               
                function ftn1() {
                    var desc766 = new ActionDescriptor();
                        var ref499 = new ActionReference();
                        ref499.putEnumerated( charIDToTypeID('Path'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
                    desc766.putReference( charIDToTypeID('null'), ref499 );
                    desc766.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
                        var desc767 = new ActionDescriptor();
                        desc767.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), 0 );
                        desc767.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), 0 );
                    desc766.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc767 );
                    desc766.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 100 );
                    desc766.putUnitDouble( charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 100 );
                    desc766.putBoolean( charIDToTypeID('Lnkd'), true );
                    executeAction( charIDToTypeID('Trnf'), desc766, DialogModes.ALL );
                }; ftn1();               
               
               
                }
         catch(e){return;} 
 
  } goforit();


  function menu() {
     
    var maindialog = new Window( 'dialog', 'KKS' );
    var choices = ['Good','Invert'];


            var panel = maindialog.add('panel');
                panel.text = '          Lets get this bad boy rollin     ';
                panel.margins = 30;

        var dropdown = panel.add("dropdownlist", undefined, choices);
        dropdown.items[0].selected = true;

        var buttons = panel.add('group');
            buttons.orientation = 'row';
            buttons.ok = buttons.add('button',undefined,'ok');




    maindialog.show();

    //setting up cases for the choices above
    switch(Number(dropdown.selection)){
        case 0 : good(); break;
        case 1 : invert(); break;
        default: break;
    }
    //defining actions for each case
    function good() {
            closedoc.close(SaveOptions.DONOTSAVECHANGES);
    }
    function invert() {
       doAction("invert", "KKS");
          closedoc.close(SaveOptions.SAVECHANGES);
    }
  }
  menu();

}



////////////////////////////////////
//  get format
////////////////////////////////////   
function getFormat(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   if(executeActionGet(ref).hasKey(stringIDToTypeID("format"))){
      return  executeActionGet(ref).getString(stringIDToTypeID("format"));
   }
}
xbytor

search files give warning if folder contains more than one

Post by xbytor »

Code: Select allif (files.length > 1) {
  alert('There is more than one PSD file in folder ' + files.absoluteURI);
}

should have been

Code: Select allif (files.length > 1) {
  alert('There is more than one PSD file in folder ' + inputFolder.absoluteURI);
}
lukasz

search files give warning if folder contains more than one

Post by lukasz »

I'm confused because this just returns the folder they choose? Can't see how that would help.
they choose parent (inputFolder), then I need list of folders to alert that have multiple psd files.

structure is:

Code: Select all         Parent
         /    \   
    child    child 2
     /       /    \
1.psd      2.psd   3.psd

I need it to get path of child 2 because it has 2 psd's
Paul MR

search files give warning if folder contains more than one

Post by Paul MR »

I wonder if this is something like?
Code: Select allvar folders =[];
var topLevel = Folder.selectDialog("Please select top level folder");   
folders = FindAllFolders(topLevel, folders);
folders.unshift(topLevel);

for(var a in folders){
    folders[a][1]= folders[a].getFiles("*.psd").length;
    }
alert("Total Folders Found = " +folders.length);
for(var a in folders){
    if(Number(folders[a][1]) > 1){
        alert('There is more than one PSD file in folder\n' + decodeURI(folders[a])+"\nNo Of Files = "+Number(folders[a][1]));
        }
    }
function FindAllFolders( srcFolderStr, destArray) {
   var fileFolderArray = Folder( srcFolderStr ).getFiles();
   for ( var i = 0; i < fileFolderArray.length; i++ ) {
      var fileFoldObj = fileFolderArray;
      if ( fileFoldObj instanceof File ) {         
      } else {
         destArray.push( Folder(fileFoldObj) );
      FindAllFolders( fileFoldObj.toString(), destArray );
      }
   }
   return destArray;
}