6 Replies Latest reply on Sep 5, 2016 12:19 AM by Wakizashi

    How to run Photoshop jsx on all files in folder

    Wakizashi Level 1

      Hello everyone.

      I have script that looks like this (btw, using Photoshop CS5)

      #target photoshop

          // ========== set units to pixels ==========//

          app.preferences.rulerUnits = Units.PIXELS;

         

          //========== set background to white ==========//

          var backgroundC = new SolidColor();

          backgroundC.rgb.red = 255;

          backgroundC.rgb.green = 255;

          backgroundC.rgb.blue = 255;

          backgroundColor = backgroundC;

         

          //========== set var to active document ==========//

         

          var doc = activeDocument;

         

          //========== main() ==========//

          function main(){ 

          if(!documents.length) return; 

          var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

          var CurrentPath = activeDocument.path;

          var saveFolder = Folder(CurrentPath + '/' + 'Shop');

          if(!saveFolder.exists) saveFolder.create();

          var saveFile = File(saveFolder + "/" + Name + ".jpg"); 

         

          if(saveFile.exists){ 

             if(!confirm("Overwrite existing document?")) return; 

              saveFile.remove(); 

              } 

          SaveForWeb(saveFile,70); //change to 60 for 60% 

          } 

          

          function SaveForWeb(saveFile,jpegQuality) { 

          var sfwOptions = new ExportOptionsSaveForWeb();  

             sfwOptions.format = SaveDocumentType.JPEG;  

             sfwOptions.includeProfile = false;  

             sfwOptions.interlaced = 0;  

             sfwOptions.optimized = true;  

             sfwOptions.quality = jpegQuality; //0-100  

          activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions); 

          } 

      //========== Set model to RGB ==========//

      doc.changeMode ( ChangeMode.RGB );

      //========== set canvas depending on initial width and height ==========//

      doc.resizeCanvas(Math.max(doc.width,doc.height),Math.max(doc.width,doc.height))

       

       

      //========== check size and do actions acording to size of document ==========//

      if (doc.width > 750){

              doc.resizeImage(750,750);

              main();

              doc.close(SaveOptions.DONOTSAVECHANGES);

          } else {

              doc.resizeCanvas(750,750);

              main ();

              doc.close(SaveOptions.DONOTSAVECHANGES);

      }

      Now, I am using it as scripts event manager, on document open trigger, and then I drag and drop images, and script do its job. However, if I drag and drop 200 images, PS will load ALL of them, hog my PC, and probably crash.

       

      My question is, how can I set this JSX to run on folder, similar to batch dialog, or image processor, but on those two I can only select Action, no option to select existing jsx script. (guys from Photoshop suggested to record script as Action, but was looking for something more "elegant" so to speak)

       

      Thanks in advance

        • 1. Re: How to run Photoshop jsx on all files in folder
          matias.kiviniemi Level 3

          A folder object has method .getFiles() that'll give you all files/folders in it as an array, then just start recursively parsing

          • 2. Re: How to run Photoshop jsx on all files in folder
            Wakizashi Level 1

            that worked,

            #target photoshop

            var theFolder = Folder.selectDialog("select folder");

            if (theFolder) {

                var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);

                for (var m = 0; m < theFiles.length; m++) {

                    app.open(File(theFiles[m]))

                   

             

                    app.preferences.rulerUnits = Units.PIXELS;

             

                    var backgroundC = new SolidColor();

                    backgroundC.rgb.red = 255;

                    backgroundC.rgb.green = 255;

                    backgroundC.rgb.blue = 255;

                    backgroundColor = backgroundC;

             

                    var doc = activeDocument;

             

                    function main() {

                        if (!documents.length) return;

                        var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

                        var CurrentPath = activeDocument.path;

                        var saveFolder = Folder(CurrentPath + '/' + 'Shop');

                        if (!saveFolder.exists) saveFolder.create();

                        var saveFile = File(saveFolder + "/" + Name + ".jpg");

             

             

                        if (saveFile.exists) {

                            if (!confirm("Overwrite existing document?")) return;

                            saveFile.remove();

                        }

                        SaveForWeb(saveFile, 70); //change to 60 for 60% 

                    }

             

             

                    function SaveForWeb(saveFile, jpegQuality) {

                        var sfwOptions = new ExportOptionsSaveForWeb();

                        sfwOptions.format = SaveDocumentType.JPEG;

                        sfwOptions.includeProfile = false;

                        sfwOptions.interlaced = 0;

                        sfwOptions.optimized = true;

                        sfwOptions.quality = jpegQuality; //0-100  

                        activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);

                    }

             

                    doc.changeMode(ChangeMode.RGB);

             

                    doc.resizeCanvas(Math.max(doc.width, doc.height), Math.max(doc.width, doc.height))

             

             

             

                    if (doc.width > 800) {

                        doc.resizeImage(800, 800);

                        main();

                        doc.close(SaveOptions.DONOTSAVECHANGES);

                    } else {

                        doc.resizeCanvas(800, 800);

                        main();

                        doc.close(SaveOptions.DONOTSAVECHANGES);

                    }

             

             

                }

            };

            Now, while I am at it, is there anything I can optimize, since I have just moved whole script inside for each loop?

            Also, would you be so kind to point me where to read on how to ask for specified dimensions for last part doc.resizeImage(800,800), and desired JPG quality?

            Like I pick folder, I get dialog box to ask about save quality and dimensions?

            Thanks

            • 3. Re: How to run Photoshop jsx on all files in folder
              c.pfaffenbichler Level 9
              how to ask for specified dimensions for last part doc.resizeImage(800,800), and desired JPG quality?

              Aren’t you pretty much trying to replicate functionality that already is available in File > Scripts > Image Processor?

               

              Edit: Not that recreating procedures that already are automated in an existing Script needs to be a bad thing, but are you trying to do this as a learning experience or is it just about the functionality?

              • 4. Re: How to run Photoshop jsx on all files in folder
                Chuck Uebele Adobe Community Professional & MVP

                You don't want to put both dimensions in your resizeImage, or it will distort the aspect ratio.

                 

                var doc= activeDocument;
                doc.resizeImage (800, undefined, undefined, ResampleMethod.AUTOMATIC);
                var jpgOptions = new JPEGSaveOptions();
                jpgOptions.quality = 10;
                doc.saveAs (new File(doc.path +'/' + 'newDocName.jpg'), jpgOptions)
                
                2 people found this helpful
                • 5. Re: How to run Photoshop jsx on all files in folder
                  Wakizashi Level 1

                  Yeah, I am trying to learn in process... I am aware that there is image resize in Image processor, but, as you can see in the script, I am processing loads of images in various sizes. from 4300x1234 upto 1000x4950... Since I need 1:1 aspect ratio, within the script I am scaling canvas to larger value (to make it initially square one), and then resize it to 800x800 since it will always be in aspect 1:1 by that time of the script.

                  So, basically, I want to create pretty much, if possible standalone script which I can send to a guy who is not too much experienced in Photoshop and say, "here, run this script and follow the instructions", rather to explain to him how to use Batch processing...

                  • 6. Re: How to run Photoshop jsx on all files in folder
                    Wakizashi Level 1

                    Not really matter since:

                    doc.resizeCanvas(Math.max(doc.width, doc.height), Math.max(doc.width, doc.height))

                    so basically, I will resize canvas depending on its initial values to square, therefore, I can afford to give both width and height, but yeah this looks more cleaner anyway, thanks