skip to Main Content

I Would like to create a script for photoshop who allow me to search some files with a specific name (for example : 300x250_F1.jpg, 300x250_F2.jpg, 300x600_F1.jpg, etc… ) in differents subfolders (all in the same parent folder) and after load them in my active document. The problem is names of subfolders will be everytime differents.

I definitely need some help 🙂

2

Answers


  1. Chosen as BEST ANSWER

    i found a code which almost do what i want (thank you).

    I'm almost good but i have a problem: if the variable "mask" have only one value, it works. But with few values, it doesn't work anymore.

    I think it's because i made an array with the mask variable and i have to update the script...

    var topFolder = Folder.selectDialog("");
    
    //var topFolder = new Folder('~/Desktop/PS_TEST');
    var fileandfolderAr = scanSubFolders(topFolder, /.(jpg)$/i);
    //var fileandfolderAr = scanSubFolders(topFolder, /.(jpg|tif|psd|bmp|gif|png|)$/i);
    var fileList = fileandfolderAr[0];
    
    var nom = decodeURI(fileList);
    
    //all file paths found and amount of files found
    //alert("fileList: " + nom + "nnFile Amount: " + fileList.length);
    //alert(allFiles);
    
    for (var a = 0; a < fileList.length; a++) {
       var docRef = open(fileList[a]);
       //do things here
    
    }
    
    function scanSubFolders(tFolder, mask) { // folder object, RegExp or string
       var sFolders = [];
       var allFiles = [];
    
       var mask = ["300x250_F1", "300x250_F2"];
       
       sFolders[0] = tFolder;
       for (var j = 0; j < sFolders.length; j++) { // loop through folders
          var procFiles = sFolders[j].getFiles();
    
          for (var i = 0; i < procFiles.length; i++) { // loop through this folder contents
             if (procFiles[i] instanceof File) {
                if (mask == undefined) {
                   allFiles.push(procFiles); // if no search mask collect all files
                }
                if (procFiles[i].fullName.search(mask) != -1) {
                   allFiles.push(procFiles[i]); // otherwise only those that match mask
                }
             }
             else if (procFiles[i] instanceof Folder) {
                sFolders.push(procFiles[i]); // store the subfolder
                scanSubFolders(procFiles[i], mask); // search the subfolder
             }
          }
       }
    
       return [allFiles, sFolders];
    
    }
    

  2. There are several ways you can accomplish this without reassigning mask within the scanSubFolders function.

    Solution 1: use a regex

    The function is already set up to accept a regex or string as a mask. You just need to use one that would match the pattern of the files you’re targeting.

    var fileandfolderAr = scanSubFolders(topFolder, /300x250_F(1|2)/gi);
    

    Solution 2: call the function within a loop

    If regex isn’t your thing, you could still utilize an array of strings, but do it outside the function. Loop the array of masks and call the function with each one, then execute your primary logic on the results of each call.

    var topFolder = Folder.selectDialog("");
    var myMasks = ["300x250_F1", "300x250_F2"];
    
    for (var index in myMasks) {
      var mask = myMasks[index]
      var fileandfolderAr = scanSubFolders(topFolder, mask);
      var fileList = fileandfolderAr[0];
    
      for (var a = 0; a < fileList.length; a++) {
        var docRef = open(fileList[a]);
        //do things here
    
      }
    }
    

    Don’t forget to remove var mask = ["300x250_F1", "300x250_F2"]; from within the scanSubFolders function or else these won’t work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search