6 Replies Latest reply on Sep 28, 2017 10:09 PM by cat-bird

    New to scripting... trying to convert script to save JPG instead of PSD

    cat-bird

      Hi there!

       

      I am trying to create an action to create a sequential WIP file. I found this script, which works. My only issue is that it creates a PSD file - I need a JPG. It seemed like a simple enough edit.

      ORIGINAL SCRIPT:

      #target photoshop 

      main(); 

      function main(){ 

      if(!documents.length) return; 

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

      Name = Name.replace(/\d+$/,''); 

      try{ 

      var savePath = activeDocument.path; 

      }catch(e){ 

          alert("You must save this document first!"); 

          } 

      var fileList= savePath.getFiles(Name +"*.psd").sort().reverse(); 

      var Suffix = 0; 

      if(fileList.length){ 

          Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/)); 

      Suffix= zeroPad(Suffix + 1, 4); 

      var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".psd"); 

      SavePSD(saveFile); 

      function SavePSD(saveFile){  

      psdSaveOptions = new PhotoshopSaveOptions();  

      psdSaveOptions.embedColorProfile = true;  

      psdSaveOptions.alphaChannels = true;   

      psdSaveOptions.layers = true;   

      activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);  

      }; 

      function zeroPad(n, s) {  

         n = n.toString();  

         while (n.length < s)  n = '0' + n;  

         return n;  

      }; 

       

      I tried to modify it to create a JPG image instead of a PSD.

      MODIFIED SCRIPT:

      #target photoshop 

      main(); 

      function main(){ 

      if(!documents.length) return; 

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

      Name = Name.replace(/\d+$/,''); 

      try{ 

      var savePath = activeDocument.path; 

      }catch(e){ 

          alert("You must save this document first!"); 

          } 

      var fileList= savePath.getFiles(Name +"*.psd").sort().reverse(); 

      var Suffix = 0; 

      if(fileList.length){ 

          Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/)); 

      Suffix= zeroPad(Suffix + 1, 4); 

      var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".jpg"); 

      SaveJPEG(saveFile, 8); 

      function SaveJPEG(saveFile, jpegQuality){ 

      var doc = activeDocument; 

      if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT; 

      jpgSaveOptions = new JPEGSaveOptions(); 

      jpgSaveOptions.embedColorProfile = true; 

      jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

      jpgSaveOptions.matte = MatteType.NONE; 

      jpgSaveOptions.quality = jpegQuality;  

      activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);  

      }; 

      function zeroPad(n, s) {  

         n = n.toString();  

         while (n.length < s)  n = '0' + n;  

         return n;  

      }; 

      When I use the second code, it overwrites my file. I've skimmed through this over and over but I'm super new at this so I don't know why it overwrites with JPG but not the PSD. Thanks in advance for any help.

        • 1. Re: New to scripting... trying to convert script to save JPG instead of PSD
          JJMack Most Valuable Participant

          You coded the save as jpeg just fine.  However, you did not understand the rest of the script.  The original is missing a return in the catch routine and it also processing for some  reason to me the first in a possible list of wild carded psd file look for a number in the name.  I know a little about javascript but  I good a testing things.... The script it trying to change the sequence number by retrieving all the psd files save sorted in reverse order but now your saving Jpg files not psd files. and it not adding 1

           

          I added the return and sone alerts show what is happening.

          #target photoshop 
          main(); 
          function main(){ 
          if(!documents.length) return; 
          var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
          Name = Name.replace(/\d+$/,''); 
          alert("Name=" + Name);
          try{ 
          var savePath = activeDocument.path; 
          }
          catch(e){ 
          alert("You must save this document first!");
          return;
              } 
          alert(savePath);
          var fileList= savePath.getFiles(Name +"*.psd").sort().reverse(); 
          alert("fileList=" + fileList );
          var Suffix = 0; 
          alert("Suffix=" + Suffix )
          if(fileList.length){ 
          Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/)); 
          alert("Look at file for suffix=" + fileList[0] )
          } 
          else alert("No " + Name + "*.psd found" )
          Suffix= zeroPad(Suffix + 1, 4); 
          alert("Suffix=" + Suffix )
          var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".jpg");
          alert("saveFile=" + saveFile )
          SaveJPEG(saveFile, 8); 
          } 
          function SaveJPEG(saveFile, jpegQuality){ 
          var doc = activeDocument; 
          if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT; 
          jpgSaveOptions = new JPEGSaveOptions(); 
          jpgSaveOptions.embedColorProfile = true; 
          jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
          jpgSaveOptions.matte = MatteType.NONE; 
          jpgSaveOptions.quality = jpegQuality;  
          activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);  
          }; 
          function zeroPad(n, s) {  
          n = n.toString();  
          while (n.length < s)  n = '0' + n;  
          return n;  
          }; 
          
          • 2. Re: New to scripting... trying to convert script to save JPG instead of PSD
            cat-bird Level 1

            Hello JJMack,

             

            Thank you for looking into this!

             

            I just attempted using this script but it seems to be doing the same thing. Every time the 'Suffix' alert shows up, it comes up as filename_0001.jpg and keeps saving it as that.

             

            I am trying to get a JPG created of whatever I am working on and I would like to keep a copy of each stage as I go along.

             

            My desired workflow would be like this:

            I would like to do some work on my image, run the script via an action and get file_0001.jpg

            Do some more work, run the same script and get file_0002.jpg

            Do some more work, run the same script and get file_0003.jpg and so on.

             

            The original script does this flawlessly with PSD files (I get 0001.psd, 0002.psd, 0003.psd just fine, but I don't want PSDs) but when I switched it to JPG, it will not count up and keeps overwriting as filename_0001.jpg each time it is run.

             

            You are very correct in that I didn't understand the rest of the code.. ^^;;

             

            EDIT: Fixed confusing typo

            • 3. Re: New to scripting... trying to convert script to save JPG instead of PSD
              JJMack Most Valuable Participant

              As I wrote I do not know javascript but I can hack at it.  I do not know what the syntax of regular expression is so I can not decipher them  I can just observe how they work.  I do not like what I see the statement "Name = Name.replace(/\d+$/,'');" Does with my file names. It strips off my cameras raw image trailing numbers.

               

              I know what the script does and as I wrote the script has an error a return statement is missing.

               

              It would be easy to modify the script to save jpeg versions.   However IMO  I see no good reason to do so.   For you would not have a layered version to go along with it.   There is not much you can do with flattened files.

               

              Also in your Jpeg code you did some steps for setting 8bit.   Jpeg only supports 8bit so the Photoshop save would take care of that you did not have too.

               

              If you really want to do just do Jpeg you would need the file list the script creates in reverse  order to jpeg files not PSD files.  Change the string psd to jpg.   If I were you I would just add saving the jpeg file. So that both the PSD and the jpeg version are saved. I do not think you need to sort the list in reverse order a script can address the last in the list as easily as the first in list.

               

              That script would look like this cleaned up.

              Capture.jpg

               

              I like my images  names intact  large sequence numbers are OK for me.

              Capture.jpg

              • 4. Re: New to scripting... trying to convert script to save JPG instead of PSD
                natrev Level 3

                Hi

                 

                Hope this will help..

                 

                    #target photoshop 
                    main(); 
                    function main(){ 
                        if(!documents.length) return; 
                        var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
                        Name = Name.replace(/\d+$/,''); 
                        try{ 
                            var savePath = activeDocument.path; 
                        }catch(e){ 
                            alert("You must save this document first!"); 
                        } 
                        var fileList= savePath.getFiles(Name +"*.jpg").sort().reverse(); 
                        var Suffix = 0; 
                        if(fileList.length){ 
                            Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/)); 
                        } 
                        Suffix= zeroPad(Suffix + 1, 4); 
                        var saveFile = (savePath + "/" + Name + "_" + Suffix + ".jpg").replace("__","_"); 
                        SaveJPEG(File(saveFile), 8);
                    } 
                    function SaveJPEG(saveFile, jpegQuality){   
                        var doc = activeDocument;   
                        if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;   
                        jpgSaveOptions = new JPEGSaveOptions();   
                        jpgSaveOptions.embedColorProfile = true;   
                        jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;   
                        jpgSaveOptions.matte = MatteType.NONE;   
                        jpgSaveOptions.quality = jpegQuality;    
                        activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);    
                    };  
                    function zeroPad(n, s) {  
                       n = n.toString();  
                       while (n.length < s)  n = '0' + n;  
                       return n;  
                    }; 
                

                 

                 

                -yajiv

                1 person found this helpful
                • 5. Re: New to scripting... trying to convert script to save JPG instead of PSD
                  JJMack Most Valuable Participant

                  This script is also missing the "return;" in the catch. This script will most likely trough an error in line 12 because savepath will be undefined.

                  • 6. Re: New to scripting... trying to convert script to save JPG instead of PSD
                    cat-bird Level 1

                    Works perfectly and exactly how I wanted! Thank you so much!