4 Replies Latest reply on Aug 17, 2010 2:56 AM by Muppet Mark-QAl63s

    Process files in a directory?

    Ghoulfool Level 1

      I've successfully written a script which is actually in two parts (process files via one script and then process them again via another) The plan is to join these two scripts into one powerful überscript. However I don't know how to process files in a directory - running them as a batch process is no good as I need to control the whole process rather then lots of repeat and forget loops in a batch file. At the moment it works because I proces the files via batch file and then manually feed the second script a text file with all the files to be processed. Klunky, I know, but it works. But now I want to make the whole script more efficient. Are there any (simple) script examples of how to process more than one file at a time. Cheers.

        • 1. Re: Process files in a directory?
          Muppet Mark-QAl63s Level 4

          You have a 'folder object' that you can either have a user select at runtime or hard code a path to. From that there is a function getFiles() to which you can pass a 'mask' to filter the results for files of a specific type etc. Once you have your list you should just be able to iterate thru the array of files in a loop… Other than that I think you will need to be a little more specific on what it is that you are trying to process…

           

          #target photoshop
          
          app.bringToFront();
          // A hard coded path to a directory 'mac style'
          var processFolder = Folder('~/Desktop/Process Images/');
          // Use folder object get files function with mask 'a reg ex'
          var fileList = processFolder.getFiles(/\.(jpg|tif|psd|eps|png)$/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 
               if (fileList[i] instanceof File) {
                    open(fileList[i]);
               }
          }
          

           

          Just added you a very basic example…

          • 2. Re: Process files in a directory?
            Michael L Hale Level 5

            To add to Muppet Mark's post. Using a file mask in getFiles() is optional. Using one let's you process only the format types you select with the selection being done by getFiles(). That is something that Automate Batch can not do.

             

            If you don't use a file mask getFiles() returns everything in the folder. Images, system, and hidden files as well as subfolders. So if you don't use a file mask you will also need some test to make sure that the file can be opened in Photoshop before the open(fileList[i]); line.

            • 3. Re: Process files in a directory?
              Ghoulfool Level 1

              This works great. This is just the first part.

               

               

              //pref pixels
              app.preferences.rulerUnits = Units.PIXELS;

               

              var nameArray = new Array ();

               

              var inFolder = Folder.selectDialog("Please select folder to process");
              if(inFolder != null){
              var fileList = inFolder.getFiles(/\.(png)$/i);

               

              for(var a = 0 ;a < fileList.length; a++){
              var doc= open(fileList[a]);

               

              //name that file
              fileName = app.activeDocument.name;

               

              nameArray[a] = fileName +"\n"

               

              //trip image to width
              app.activeDocument.trim(TrimType.TOPLEFT, false, true, false, true);

               

              var srcDoc = activeDocument;
              var docWidth = srcDoc.width.value;
              var docHeight = srcDoc.height.value;

               

              srcDoc.resizeCanvas(docWidth,docHeight,AnchorPosition.MIDDLECENTER);

               


              // Set filePath and fileName to source path
              filePath = srcDoc.path + '/' + app.activeDocument.name;

               

              // save out the image
              var pngFile = new File(filePath);
              pngSaveOptions = new PNGSaveOptions();
              pngSaveOptions.embedColorProfile = true;
              pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
              pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1;

               

              srcDoc.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);

               

              app.activeDocument.close()

               

              What code would I need if I wanted tro replace the select folder with an ordinary open dialogue box which allows you to select multiple files (masked by png or gif)

               

              Cheers.

              • 4. Re: Process files in a directory?
                Muppet Mark-QAl63s Level 4

                If my understanding is right then what you want is NOT possible from the standard JavaScript dialogs…? The 'Folder Object' will get you a directory or null and the 'File Object' will return you a single file object or null. I see NO options to make multiple selections in the File open dialog window…? or at least I've NOT be able to do this… AppleScript has this as an option. You can use options to lock down the choices to certain file types etc. bit these differ dependent on the OS that the script will be run on. If you want your script to work on both then you need to test the OS and offer 2 methods of file selection. Here is a File open dialog but I can't find any key combo that allows it to select more than one file…

                 

                var oneFile = File.openDialog('Please select a File to open…');