5 Replies Latest reply on Apr 8, 2016 4:00 AM by testr3456456546

    Script to "Optimize to file size" (automatically) in "Save for Web"?

    auwebio Level 1

      Hello, all,

       

      I'm a novice when it comes to scripts in Photoshop, but I'm really coming to the conclusion that this is the only way to automate the actions I need to do, whithout having to do it one-by-one.

       

      Is it possible to write a Script to automatically choose the option (and settings) to "Optimize to file size" in the "Save for Web" dialog?

       

      Is it also possible to save the file in the document's original folder, instead of a single specific folder, using "Save for Web"?

       

      I have a really big number of different files (some .JPEG, some .PNG, some .TIFF) and I need to generate a single "reference.jpg" in each one of those original folders. It's quite easy to use whatever method to separate all those files in some lists and later open them in Photoshop in small subgroups of files.

       

      I only have a restriction of size (60Kb) for those "reference.jpg" files, and I would like to save all of them, in each document original folder, with the best possible quality within that size constraint.

       

      Here's my problem:
      I can easily do that with a very simple action, but, that specific way, I have no control whatsoever over the resulting filesize of those "reference.jpg" files generated. Moreover, I also find the "save for web" command create a more "slim" file, allowing for better quality within my size constraint.

        • 1. Re: Script to "Optimize to file size" (automatically) in "Save for Web"?
          c.pfaffenbichler Level 9

          Is it also possible to save the file in the document's original folder, instead of a singlespecific folder, using "Save for Web"?

          That’s no problem.

           

          Is it possible to write a Script to automatically choose the option (and settings) to "Optimize to file size" in the "Save for Web"dialog?

          I can’t make that out in the ScriptingListener code, but maybe someone else knows better or has a Script for the task already.

          I seem to remember a thread where it was discussed to save a copy, check its size and overwrite it with different settings if it was too big … that should be possible, but may run longer.

           

          As for the slimmer files Save for Web and Devices can strip out metadata for example.

          1 person found this helpful
          • 2. Re: Script to "Optimize to file size" (automatically) in "Save for Web"?
            auwebio Level 1

            c.pfaffenbichler wrote:
            "I can’t make that out in the ScriptingListener code, but maybe someone else knows better or has a Script for the task already.
            I seem to remember a thread where it was discussed to save a copy, check its size and overwrite it with different settings if it was too big … that should be possible, but may run longer."

             

             

             

            Many, many thanks for your reply, c.pfaffenbichler!    I spend the whole day (and night) yesterday learning (or rather trying to learn) about javascript, OOP, and how it should be applied in Photoshop's Scripts.

             

             

             

            I was able to modify some snippets of code I found on the web to suit my needs (temporarily):

             

             

             

            //SaveforWebSP.jsx
            //Save a fixed width image with a size constraint in its original folder
            
            var docRef = activeDocument;
            var outputFolder = docRef.path;
            
            NamesaveRef = new File( outputFolder + "/reference.jpg" );
            var NewfileRef = new File( NamesaveRef )
            
            // quality/size constraints
            var w = 300; // new file width
            var MaxSz =  61440; // max. 60Kb
            var Qlt = 100; // initial quality 100
            var x = 1; // decreasing step
            
            // resize the image to the right width
            docRef.resizeImage(UnitValue(w,"px"),null,100,ResampleMethod.BICUBICSHARPER);
            
            // Perform the first SaveForWeb Operation
            ExpWeb(NewfileRef, Qlt);
            
            // Keep trying to save the file with max. Qlt, but under MaxSz    
            while (NewfileRef.length > MaxSz)
            {
                  Qlt = Qlt - x;
                  NewfileRef = new File( NewfileRef );   
                  NewfileRef.remove();
                  ExpWeb(NewfileRef, Qlt);  // Perform a new SaveForWeb Operation, with slightly lower Qlt
                     if (Qlt <= 50) {
                       alert("The file can't be saved with the desired size AND quality.");
                       break  // break the loop whenever the quality is as low as 50 (this shouldn't need to happen)
                     }
            }
            
            var FileSz = NewfileRef.length/1024;
            FileSz = Math.round(FileSz);
            
            // close the original file without saving
            activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            
            // SaveForWeb Export, with the desired constraints and parameters
            function ExpWeb(FileNm, Qlt)
            {
                  var options = new ExportOptionsSaveForWeb();
                  options.quality = Qlt;   // Start with highest quality (biggest file).
                  options.format = SaveDocumentType.JPEG;   // Save Format for the file
                  docRef.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);
            }
            
            

             

             

             

             

            This works, but it takes a long time and keep the HD disk "going crazy" during the time it is processing the images.

             

             

             

            This is too bad, because the "Optimize to file size" dialog in the "Save for Web" would have been so much faster, efficient and much less "disk-intensive"...

            1 person found this helpful
            • 3. Re: Script to "Optimize to file size" (automatically) in "Save for Web"?
              ophello Level 1

              I know this is an old thread, but I hope you're still around. How easily could this process be implemented in AppleScript?

              • 4. Re: Script to "Optimize to file size" (automatically) in "Save for Web"?
                c.pfaffenbichler Level 9

                As far as I can tell very few of the regulars here concern themselves with AppleScript.

                In the context of Photoshop Scripting it generally makes little sense to invest time in a platform specific language.

                • 5. Re: Script to "Optimize to file size" (automatically) in "Save for Web"?
                  testr3456456546

                  Hi, how save document with the same name? Now it's always save as a "reference.jpg".