6 Replies Latest reply on Jan 13, 2011 6:12 PM by mkeatley17

    Search For Files using scripting??

    mkeatley17

      Is it possible to search for files using Photoshop Scripting?

       

      Say I'm looking for "cat_picture.jpg" but I don't know exactly which folder it is in.  Are there any commands that will search and return the path of that file to me??

       

      Thanks

        • 1. Re: Search For Files using scripting??
          xbytor2 Level 4

          Something like this should work. 'mask' is used like in Folder.getFiles().

           

          function getFolders(folde, mask) {
            if (folder.alias) {
              folder = folder.resolve();
            }
            return folder.getFiles(folders,  function(f) { return f instanceof Folder; });
          };
          
          function findFiles(folder, mask) {
            if (folder.alias) {
              folder = folder.resolve();
            }
            var files = folder.getFiles(mask);
            var folders = getFolders(folder);
          
            for (var i = 0; i < folders.length; i++) {
              var f = folders[i];
              var ffs = 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;
          };
          
          • 2. Re: Search For Files using scripting??
            mkeatley17 Level 1

            hello,  I appreciate your help and think this is just what I'm looking for.  Unfortunately I can't seem to get it to work, even after a lot of messing around.  I am stuck on an error saying folder.getFiles(mask); is not a function.

             

            were you able to get this to run on your end?

             

            Thanks

            • 3. Re: Search For Files using scripting??
              xbytor2 Level 4

              Maybe a typo. Change this:

               

              function getFolders(folde, mask) {

               

              to this:

              function getFolders(folder, mask) {
              • 4. Re: Search For Files using scripting??
                mkeatley17 Level 1

                no. caught that.  it's something about the way the folder gets passed between the functions and it isn't define dproperly at the time it uses the getFiles().  I'll keep messing around with it.  Let me know if anything jumps out. thanks!!

                • 5. Re: Search For Files using scripting??
                  xbytor2 Level 4

                  I had copied the functions from xtools/xlib/stdlib.js and munged some of the edits. Here is a tested version that works:

                   

                  function getFolders(folder) {
                    if (folder.alias) {
                      folder = folder.resolve();
                    }
                    return folder.getFiles(function(f) { return f instanceof Folder; });
                  };
                   
                  function findFiles(folder, mask) {
                    if (folder.alias) {
                      folder = folder.resolve();
                    }
                    var files = folder.getFiles(mask);
                    var folders = getFolders(folder);
                   
                    for (var i = 0; i < folders.length; i++) {
                      var f = folders[i];
                      var ffs = 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;
                  };
                  

                   

                  Usage:

                   

                  var f = findFiles(Folder('~/Desktop/PSE9_CIB'), '*.psd');
                  $.writeln(f);
                  
                  • 6. Re: Search For Files using scripting??
                    mkeatley17 Level 1

                    that works!!! thank you!