14 Replies Latest reply: Sep 12, 2012 2:54 AM by funreizer RSS

    Script to Open Folder Automatically?

    funreizer

      Good day,

       

      I've been searching the net for 2 days for something probably simple, but can't find it.

       

      Is there a script that can open all files within a folder automatically?

       

      What I want to do is to have Photoshop open a folder full of files, save each one for web. I can automate save for web part with actions. but can't find a way to import a folder.

       

      Image processor is great, except one thing that annoys the hell out of me which is dialog popup. So I'm looking for script that automatically, without any dialog boxes, opens up all files within a folder.

       

      I found this:

       

       

      var dir = Folder('Q:/Images');
      
      var selectedFile = dir.openDlg("Select Image" , "Select:*.nef;*.cr2;*.crw;*.dcs;*.raf;*.arw;*.orf;*.dng;*.psd;*.tif;* .jpg;*.png;*.bmp");
      
      if( selectedFile !=null ){open(File(selectedFile));}
      

       

      but it's a prompt. Is there some script that just loads a folder with files automatically?

       

      thanx

        • 1. Re: Script to Open Folder Automatically?
          JJMack MVP

          I'm sure if you look at a script like the Image Processor that ships with Photoshop you will see code to do that. You can just point it at a folder and it will process all the image files it it. There is also a option to process sub foldere so it may show you how to use recursion.

           

          I quick look I found this in Image Processor

           

          ///////////////////////////////////////////////////////////////////////////////

          // Function: FindAllFiles

          // Usage: Find all the files in the given folder recursively

          // Input: srcFolder is a string to a folder

          //                      destArray is an Array of File objects

          // Return: Array of File objects, same as destArray

          ///////////////////////////////////////////////////////////////////////////////

          function FindAllFiles( srcFolderStr, destArray ) {

                    var fileFolderArray = Folder( srcFolderStr ).getFiles();

           

           

                    for ( var i = 0; i < fileFolderArray.length; i++ ) {

                              var fileFoldObj = fileFolderArray[i];

                              if ( fileFoldObj instanceof File ) {

                                        destArray.push( fileFoldObj );

                              } else { // folder

                                        FindAllFiles( fileFoldObj.toString(), destArray );

                              }

                    }

           

           

                    return destArray;

          }

           

          To do what you want I've used code that looks like this

           

          // Loop Image File Start

                                        var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd|eps|png|bmp)$/i);

                                        // Loop through files

                                        for (var i = 0; i < fileList.length; i++) {

           

          • 2. Re: Script to Open Folder Automatically?
            Muppet Mark Community Member

            The folder object has a getFiles() method which you can pass a regexp to filter otherwise it will return all files… Lots of samples of this here in this forum…

            • 3. Re: Script to Open Folder Automatically?
              funreizer Community Member

              thank You JJMack, but I don't see where I tell the script which folder to open up.

               

              My path is "C:\Users\Admin\Desktop\Output_Files", so I tried putting that instead of srcFolderStr or Folder, but nothing happens. Well probably cause I have no idea what I'm doing. No scripting knowledge here.

              • 4. Re: Script to Open Folder Automatically?
                funreizer Community Member

                thanx, but I just need it to open up all files in a folder. They're all dataset files from photoshop.

                • 5. Re: Script to Open Folder Automatically?
                  JJMack MVP

                  Files can be opened, placed, executed, deleted etc in my loop I was placeing files not opening

                   

                   

                                                // Loop Image File Start

                                                var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd|eps|png|bmp)$/i);

                                                // Loop through files

                                                for (var i = 0; i < fileList.length; i++) {

                                                          // Only process the returned file objects

                                                          // The filter 'should' have missed out any folder objects

                                                          //  get next image file

                                                          if (fileList[i] instanceof File) {

                                                                    //alert(fileList[i]);

                                                                    var layers = activeDocument.layers;

                                                                    activeDocument.activeLayer = layers[layers.length-1-imageNumber+1];           // Target Background Layer or last placed image

                   

                                                                    placeImage(fileList[i]);                                                     // Place in Image   you would open instead of place

                   

                                                                    // Get Smart Object current width and height


                  • 6. Re: Script to Open Folder Automatically?
                    funreizer Community Member

                    heh ok, I thought this would be easier something like removing or changing a string in a code.

                     

                    found another one but not working, prob just piece of code:

                     

                    var inputFolder = Folder.selectDialog( "Please select top level folder to process");
                    var fileList = inputFolder.getFiles();

                     

                    for(i=0;i<fileList.length;i++) var docRef = app.open(fileRef);

                     

                     

                    wonder is there some kind of variable instead of Folder.selectDialog to just open all files within instead of prompoting?

                    • 7. Re: Script to Open Folder Automatically?
                      JJMack MVP

                      If you know the folder you do not need to put up a select dialog just  process the folder you know.  If you keep on loading image you may start boging down the system. Here some code I used to open files flatten then resize them to tile size copy to clipboard close no save then paste them into place into a new document I'm creating to print images on roll paper.

                       

                       

                      var currrow=0; var pasted=0;

                                          for (var i=0;i<file.length;i++) {

                                                    app.load(file[i]); //load it into documents

                                                    var backFile= app.activeDocument; //prepare your image layer as active document

                                                    flatten(); //handle layered images

                                                    if (backFile.width.value<backFile.height.value&&width>height ) { backFile.rotateCanvas(-90.0);  } // Rotate portraits

                                                    if (backFile.height.value<backFile.width.value&&height>width ) { backFile.rotateCanvas(-90.0);  } // Rotate landscapes

                                                    if (backFile.width.value/backFile.height.value > width/height) { backFile.resizeImage(null, height, null, ResampleMethod.BICUBIC); } // wider

                                                    else {backFile.resizeImage(width, null, null, ResampleMethod.BICUBIC);} // same aspect ratio or taller

                                                    backFile.selection.selectAll();

                                                    backFile.selection.copy(); //copy resized image into clipboard

                                                    backFile.close(SaveOptions.DONOTSAVECHANGES); //close image without saving changes

                       

                                                    for (var n=0;n<copies;n++) {

                       

                       

                                                              var x =pasted*width;

                                                              var y =currrow*height;

                                                              var selectedRegion = Array(Array(x,y), Array(x+width,y), Array(x+width,y+height), Array(x,y+height));

                       

                       

                                                              doc.selection.select(selectedRegion);

                                                              doc.paste(true); //paste image into masked layer your document

                                                              doc.selection.select(selectedRegion);

                                                              align('AdCH'); align('AdCV');

                                                              doc.selection.deselect();

                                                              pasted++

                                                              if ( pasted==cols ) { pasted=0; currrow++; }

                                                              }

                                                    }

                       

                       

                      var inputFolder = "~/My Documents/My Pictures/";

                      var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd|eps|png|bmp)$/i);

                       

                      • 8. Re: Script to Open Folder Automatically?
                        funreizer Community Member

                        thanx JJMack,

                         

                        I grabbed your code and ut function at the top to look like:

                         

                        function FindAllFiles(){

                        var inputFolder = "~/My Documents/My Pictures"

                        var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd| eps|png|bmp)$/i);

                        }

                         

                        it doesn't show errors, but also does nothing. Is that what it's supposed to do, load all files from a folder?

                        • 9. Re: Script to Open Folder Automatically?
                          Muppet Mark Community Member

                          Try this:

                           

                          var inputFolder = Folder( Folder.desktop + '/Output_Files' ); // Make sure this is correct for your hard coded path???
                          
                          var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd| eps|png|bmp)$/i);
                          
                          alert( fileList.length );
                          
                          for ( var i = 0; i < fileList.length; i++ ) {
                              
                              app.open( fileList[i] );
                              
                          };
                          
                          • 10. Re: Script to Open Folder Automatically?
                            JJMack MVP

                            Yes I was to fast with the fingers heet to be a folder object use code like  Muppet Mark  does not have to ne on the desktop and you maght want to test ir see if it exists

                            var testFolder = new Folder("~/My Documents/My Pictures");

                                                if (!testFolder.exists) {

                            • 11. Re: Script to Open Folder Automatically?
                              funreizer Community Member

                              fantastic, after editing it I got exactly what I wanted. I knew this has to be short script Thanx a lot Muppet Mark!

                               

                              Here's my final script in case anyone needs:

                               

                               

                               

                              var inputFolder = Folder( Folder.desktop + '/Output_Files' ); // Make sure this is correct for your hard coded path???

                               

                              var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|tif|psd| eps|png|bmp)$/i);

                               

                              for ( var i = 0; i < fileList.length; i++ ) {

                                 

                                  app.open( fileList[i] );

                                 

                              };

                               

                              One thing I didn't realise is I can't automate save for web for each opened image in a sequence of actions, so image processor pro, as someone suggested in this forum is perfect for what I needed to get accomplished. But appreciate your help, this sure will be useful.

                              • 12. Re: Script to Open Folder Automatically?
                                funreizer Community Member

                                thank You JJMack, I got it now!

                                • 13. Re: Script to Open Folder Automatically?
                                  JJMack MVP

                                  Remember if there are a great number of large images in the folder that script will tax your system....

                                  • 14. Re: Script to Open Folder Automatically?
                                    funreizer Community Member

                                    I'll keep that in mind