8 Replies Latest reply on Feb 2, 2016 4:01 PM by JJMack

    Script to save previously unsaved, newly created files?

    Martin_Moore Level 1

      I'm looking for a script that can save an open, previously unsaved, image as a TIFF file.

       

      It's important that it's not needed to enter a new file name every single time the script is executed, I need the script to do that for me, at least once a file name is pre-defined in the script.

       

      The script must also automatically "number" the files like file0001.tif, file0002.tif,, file0003.tif, etc., in order to not overwrite an already existing file.

       

      Is there a script that fits my description?

       

      Cheers!

      Martin

        • 1. Re: Script to save previously unsaved, newly created files?
          Paul Riggott Level 6

          Please try this, you will have to edit the script to meet your needs.

          It will save ALL open documents an close them once saved.

           

           

          #target photoshop
          app.bringToFront();
          main();
          function main(){
          if(!documents.length) return;
          //amend to suit
          var folderPath = Folder("/c/folderName");
          if(!folderPath.exists) {
              alert("Output folder does not exist!");
              return;
              }
          //this should be the first part of the filename 
          //I.E. if file name = Picture0001.tif it needs to be Picture
          //N.B. it must be the same case!
          var fileName  = "filename";
          var fileType = "tif";
          while (documents.length) {
          var fileList = new Array();
          var newNumber=0;
          var saveFile='';
          fileList = folderPath.getFiles((fileName + "*." + fileType));
          fileList.sort().reverse();
          if(fileList.length == 0){
          saveFile=File(folderPath + "/" + fileName + "0001." +fileType);
          SaveTIFF(saveFile);
          activeDocument.close(SaveOptions.DONOTSAVECHANGES); 
          }else{
          newNumber  = Number(fileList[0].toString().replace(/\....$/,'').match(/\d+$/)) +1;
          saveFile=File(folderPath + "/" + fileName + zeroPad(newNumber, 4) + "." +fileType);
          SaveTIFF(saveFile);
          activeDocument.close(SaveOptions.DONOTSAVECHANGES); 
              }
          }
          };
          function zeroPad(n, s) { 
             n = n.toString(); 
             while (n.length < s)  n = '0' + n; 
             return n; 
          };
          function SaveTIFF(saveFile){
          tiffSaveOptions = new TiffSaveOptions(); 
          tiffSaveOptions.embedColorProfile = true; 
          tiffSaveOptions.alphaChannels = true; 
          tiffSaveOptions.layers = true;
          tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW; 
          activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE); 
          };
          
          
           
          1 person found this helpful
          • 2. Re: Script to save previously unsaved, newly created files?
            Martin_Moore Level 1

            Thanks for the reply and the script Paul!

             

            I just tried it and it does indeed save all open documents and closing them.

             

            Is it possible to make the script only save the document that is being working on, the one that's selected at the bottom in the "Window" menu?

             

            Also, it is possible to not closing all documents, not even the one it has saved? Because the closing command can easily be added with actions and I rather do it that way, so I can chose to either close it or not.

             

            Martin

            • 3. Re: Script to save previously unsaved, newly created files?
              Paul Riggott Level 6

              I think this should do it (not tested)....

               

               

              #target photoshop
              app.bringToFront();
              main();
              function main(){
              if(!documents.length) return;
              //amend to suit
              var folderPath = Folder("/c/folderName");
              if(!folderPath.exists) {
                  alert("Output folder does not exist!");
                  return;
                  }
              //this should be the first part of the filename 
              //I.E. if file name = Picture0001.tif it needs to be Picture
              //N.B. it must be the same case!
              var fileName  = "filename";
              var fileType = "tif";
              var fileList = new Array();
              var newNumber=0;
              var saveFile='';
              fileList = folderPath.getFiles((fileName + "*." + fileType));
              fileList.sort().reverse();
              if(fileList.length == 0){
              saveFile=File(folderPath + "/" + fileName + "0001." +fileType);
              SaveTIFF(saveFile);
              }else{
              newNumber  = Number(fileList[0].toString().replace(/\....$/,'').match(/\d+$/)) +1;
              saveFile=File(folderPath + "/" + fileName + zeroPad(newNumber, 4) + "." +fileType);
              SaveTIFF(saveFile);
                  }
              };
              function zeroPad(n, s) { 
                 n = n.toString(); 
                 while (n.length < s)  n = '0' + n; 
                 return n; 
              };
              function SaveTIFF(saveFile){
              tiffSaveOptions = new TiffSaveOptions(); 
              tiffSaveOptions.embedColorProfile = true; 
              tiffSaveOptions.alphaChannels = true; 
              tiffSaveOptions.layers = true;
              tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW; 
              activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE); 
              };
              
              
               
              • 4. Re: Script to save previously unsaved, newly created files?
                Martin_Moore Level 1

                Wow! Paul, you're a genius! Haha, you should see me smiling now.

                 

                Thanks sooo much for the script! It is perfect (full score!) and it is greatly appreciated. It just made my life easier, I assure you that!

                 

                About 700-900 files to save - here I come!

                 

                Regards

                 

                Martin

                • 5. Re: Script to save previously unsaved, newly created files?
                  busterX Level 1

                  I know this is a old thread, but is there a way to "prompt" the user for the folder location to save the file to (using the save as dialog box)?

                  • 6. Re: Script to save previously unsaved, newly created files?
                    Philip Cord Level 4

                    Change line 7 From

                    var folderPath = Folder("/c/folderName");

                     

                    To

                    var folderPath =  Folder.selectDialog("Please select the output folder");

                    • 7. Re: Script to save previously unsaved, newly created files?
                      wshuebner

                      Paul, your script is just what I am looking for except I want to save the files as jpg files.  What lines do I need to change.

                       

                      Thank you.