21 Replies Latest reply on Oct 11, 2018 4:13 PM by Stephen_A_Marsh

    Scripting a Save As command

    StuartLevesque Level 1

      Is there a way to script a Save As dialog?  I need to resize an image multiple times and between each time have it save the file to the original location as a jpeg with a suffix.  I already have the script to resize the image, but I cannot find a Save As script.  Any help would be appreciated.

        • 1. Re: Scripting a Save As command
          Michael L Hale Level 5

          The scripting documenttion is shipped with Photoshop. You should have the guides for javascript, applescript, and vbs if you did a normal install.

           

          The exact syntax will depend on which langauage you use and what format you want to save the file.

           

          But if you just want to script opening the SaveAs dialog so the user can interact with it, here is how to do that in javascript

           

          executeAction( charIDToTypeID( "save" ), undefined, DialogModes.ALL );

           

          And here is a javascript function for saving a copy as jpeg

           

           

          function saveJPEG( doc, saveFile, qty ) {
               var saveOptions = new JPEGSaveOptions( );
               saveOptions.embedColorProfile = true;
               saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
               saveOptions.matte = MatteType.NONE;
               saveOptions.quality = qty; 
               doc.saveAs( saveFile, saveOptions, true );
          }
          saveJPEG( app.activeDocument, new File('~/Desktop/sample.jpg'), 10 );
          
          1 person found this helpful
          • 2. Re: Scripting a Save As command
            StuartLevesque Level 1

            Thank you for your help.  How would I adjust the last line to save the file into the folder it opened from?

            • 3. Re: Scripting a Save As command
              Michael L Hale Level 5
              saveJPEG( app.activeDocument, new File(decodeURI(app.activeDocument.path)+'/sample.jpg'), 10 );

              Note that should throw an error if the file has never been saved. And it needs the saveJPEG function from the post above.

              1 person found this helpful
              • 4. Re: Scripting a Save As command
                StuartLevesque Level 1

                Thank you very much.


                • 5. Re: Scripting a Save As command
                  StuartLevesque Level 1

                  I have one more question.  How would I also save it with the current document name plus a suffix?

                  • 6. Re: Scripting a Save As command
                    Michael L Hale Level 5
                    var doc = app.activeDocument;
                    var docName = doc.name;
                    docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/):docName = [docName, docName];
                    var suffix = '_300';
                    var saveName = new File(decodeURI(doc.path)+'/'+docName[1]+suffix+'.jpg');
                    saveJPEG( app.activeDocument, saveName, 10 );
                    • 7. Re: Scripting a Save As command
                      Paul Riggott Level 6

                      This is one way..

                      var doc = app.activeDocument;
                      var Path = doc.path;
                      var Name = doc.name.replace(/\.[^\.]+$/, ''); 
                      var Suffix = "-Copy";
                      var saveFile = File(Path + "/" + Name + Suffix + ".jpg");
                      SaveJPEG(saveFile, 8);
                      
                      function SaveJPEG(saveFile, jpegQuality){
                      jpgSaveOptions = new JPEGSaveOptions();
                      jpgSaveOptions.embedColorProfile = true;
                      jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                      jpgSaveOptions.matte = MatteType.NONE;
                      jpgSaveOptions.quality = jpegQuality;
                      activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
                      }
                      

                       

                      Edit Mike was much faster than me

                      2 people found this helpful
                      • 8. Re: Scripting a Save As command
                        StuartLevesque Level 1

                        Thank you all for the help.  A job that would have taken days will now take about 30 minutes.

                        You saved my sanity.

                        • 9. Re: Scripting a Save As command
                          red_dirt_studio Level 1

                          This is a great script. Thank you.

                           

                          I do have one question:

                          How would you take this same script and add a subfolder to save the files? I'll say the new folder name is /images just to be generic.

                           

                          I am having issues trying to do this.

                           

                          Thanks

                          • 10. Re: Scripting a Save As command
                            jdjoyner92 Level 1

                            Does anyone have a script to save a jpeg multiple times in one command? I'm working on an art project based on generation compression. I'd really appreciate the help!

                            • 11. Re: Scripting a Save As command
                              JJMack Most Valuable Participant

                              You will not be able to do multiple save as a single command.  You can do multiple save ins a Action or Script.   The Image Processor Pro plug-in script lets you easily do that. You cav dowmload that from the web.. ps-scripts - Browse /Image Processor Pro/v3_2 betas at SourceForge.net  Its dialog looks like this;

                              Capture.jpg

                              • 12. Re: Scripting a Save As command
                                faisalb475296 Level 1

                                Sorry to bother you but do you know if its possible to use a default location or ask for a location if there is no doc.path (Image pasted into a new file)?

                                • 13. Re: Scripting a Save As command
                                  JJMack Most Valuable Participant

                                  Yes

                                   

                                  If you look at the code you can see the script sets the Path and the rest of the saveFile. Path does not have to be set to the path of the origibal backing file.  If the document is new it has no backing file the code would actually fail.

                                   

                                  1. var doc = app.activeDocument; 
                                  2. var Path = doc.path;  <=============== if the document is new there is no doc.path
                                  3. var Name = doc.name.replace(/\.[^\.]+$/, '');  
                                  4. var Suffix = "-Copy"; 
                                  5. var saveFile = File(Path + "/" + Name + Suffix + ".jpg");

                                   

                                  ,

                                  • 14. Re: Scripting a Save As command
                                    faisalb475296 Level 1

                                    Really sorry to bother you again but is there code where I can say (abridged for lack of knowledge on my part) if the variable Path is empty, use folder.myDocuments otherwise use doc.path? This is what I'm using:

                                     

                                    var doc = app.activeDocument;
                                    var Path = Folder.myDocuments;
                                    var w = app.activeDocument.width.toString().replace(' px', '');
                                    var h = app.activeDocument.height.toString().replace(' px', '');
                                    var Name = app.activeDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|\ ]/g, "_");  // '/\:*?"<>| ' -> '_'
                                    var Name = app.activeDocument.activeLayer.name.replace(/[.jpg\ ]/g, "_"); 
                                    var Suffix = "_Device-Desktop_" + w + 'x' + h ;
                                    var saveFile = File(Path + "/" + Name + Suffix + ".jpg"); 
                                    SaveJPEG(saveFile, 8); 
                                    function SaveJPEG(saveFile, jpegQuality){  
                                    jpgSaveOptions = new JPEGSaveOptions(); 
                                    jpgSaveOptions.embedColorProfile = true; 
                                    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
                                    jpgSaveOptions.matte = MatteType.NONE; 
                                    jpgSaveOptions.quality = jpegQuality; 
                                    activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 
                                    • 15. Re: Scripting a Save As command
                                      JJMack Most Valuable Participant

                                      You could make the script interactive and Prompt for a path. If the user does not supply one if the document is not new there will be a doc.path available for you to use.  Scripts can use logic like this.

                                      path = prompt for a path.

                                      If path is not defined or is blank use doc path if one exists else message user you need to save the document or supply a path.

                                       

                                      The thing is you should learn a little scripting if you want to do things like this.  I have a programming background but do not actually know javascript.  However I know how to search forums like this to find code the does something like I want to do and can hack it to do what I want it to do.

                                       

                                      https://forums.adobe.com/search.jspa?place=%2Fplaces%2F1383833&sort=updatedDesc&q=prompt

                                      For some unknown reason the abve search is not pass promt as the search item please type prompt into the search filed

                                       

                                      you may fine code the looks like this:

                                       

                                      if (to=="") {to=prompt("Enter email address",to);}    // Prompt for email address if no default


                                      You may be able to change it to be something like


                                      var path = '';

                                      if (path=="") {path=prompt("Enter folder's path",path);}  // Prompt for path address if no default

                                      if (path=="") {

                                         try {var path = activeDocument.path +"/";}

                                         catch (e) {path=prompt("New Document  enter folder path",path);}    // New doc Path

                                      }

                                      if (path=="") {alert("No Path Supplied not saved"); return; } //exit

                                      • 16. Re: Scripting a Save As command
                                        fannic94655598 Level 1

                                        hello jjmack, sorry for bumping old thread,

                                        i want to use this script,

                                        var doc = app.activeDocument;  
                                        var Path = doc.path;  
                                        var Name = doc.name.replace(/\.[^\.]+$/, '');   
                                        var Suffix = "-Copy";  
                                        var saveFile = File(Path + "/" + Name + Suffix + ".jpg");  
                                        SaveJPEG(saveFile, 8);  
                                          
                                        function SaveJPEG(saveFile, jpegQuality){  
                                        jpgSaveOptions = new JPEGSaveOptions();  
                                        jpgSaveOptions.embedColorProfile = true;  
                                        jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
                                        jpgSaveOptions.matte = MatteType.NONE;  
                                        jpgSaveOptions.quality = jpegQuality;  
                                        activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);  
                                        }  
                                        
                                        

                                        this script always save with "copy" in the end, but when i do with the same file name it will replace with the new one but same filename

                                        how to make the append of "copy" file name,

                                         

                                        what i want is how to make this script append the end of "copy" filename.

                                        for example "copy_1" , "copy_2"  , "copy_3", etc

                                        • 17. Re: Scripting a Save As command
                                          JJMack Most Valuable Participant

                                          You would need to test the existing filenames that exist with the serial suffix and add one tow the serial number for the next filename. This script will save a

                                          PSD file versions and a matching jpeg file.

                                          #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!\nTo establish a save path...");
                                          return
                                              } 
                                          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 saveName = File(savePath + "/" + Name + "_" + Suffix)
                                          SavePSD(saveName);
                                          SaveJPEG(saveName,8);
                                          alert("Versions " + Name + "_" + Suffix + " saved.")
                                          } 
                                          //////////////////////////////////////////////////////////////////////////////////////////////////
                                          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;  
                                          }; 
                                          function SaveJPEG(saveFile, jpegQuality){   
                                          jpgSaveOptions = new JPEGSaveOptions();   
                                          jpgSaveOptions.embedColorProfile = true;   
                                          jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;   
                                          jpgSaveOptions.matte = MatteType.NONE;   
                                          jpgSaveOptions.quality = jpegQuality;    
                                          activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);    
                                          };   
                                          
                                          1 person found this helpful
                                          • 18. Re: Scripting a Save As command
                                            dominiquehs Level 1

                                            You seem to be rocking this, Ive been looking everywhere for a script to save a PSD with layers to a Targa (.tga) + a suffix..for example,

                                            I need to change the copy 15 times on the same PSD and export them quick as TGA.

                                             

                                            Could I use the above script but tweak it for tga files?

                                            Would save me a lot of time if you could help me!

                                             

                                            THANKS!

                                            • 19. Re: Scripting a Save As command
                                              JJMack Most Valuable Participant

                                              Modify it any way you want you do not need my permission.

                                              • 20. Re: Scripting a Save As command
                                                dominiquehs Level 1

                                                Haha thanks JJMack, I don't have your skills thou, so my request would be if you could help me modify this script? to save .tga files in a folder (or same folder) but with a new suffix (ex: image_01.tga , image_02.tga etc..)

                                                Mental karma and mentalbeer from Sweden.

                                                 

                                                THANKS!

                                                • 21. Re: Scripting a Save As command
                                                  Stephen_A_Marsh Adobe Community Professional

                                                  An old thread and I don’t expect that dominiquehs is still around or has a burning need for this anymore… however it is a perfect opportunity for me to practice/learn scripting by making modifications to scripts that I could not currently write from scratch.

                                                   

                                                  //https://forums.adobe.com/message/9934728#9934728
                                                  //https://forums.adobe.com/message/10673464#10673464
                                                  //Save PSD & TGA Numbered Versions.jsx
                                                  #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!\nTo establish a save path...");  
                                                  return  
                                                      }   
                                                  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 saveName = File(savePath + "/" + Name + "_" + Suffix)  
                                                  SavePSD(saveName);  
                                                  SaveTARGA(saveName);  
                                                  alert("Versions " + Name + "_" + Suffix + " saved.")  
                                                  }   
                                                  //////////////////////////////////////////////////////////////////////////////////////////////////  
                                                  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;    
                                                  };   
                                                  function SaveTARGA(saveFile){     
                                                  SaveOptions = new TargaSaveOptions();     
                                                  TargaSaveOptions.alphaChannels = true; //include alpha channels, change to false for none    
                                                  TargaSaveOptions.resolution = TargaBitsPerPixels.TWENTYFOUR; //options of SIXTEEN or THIRTYTWO 
                                                  TargaSaveOptions.rleCompression = true; //false for no compression
                                                  activeDocument.saveAs(saveFile, TargaSaveOptions, true,Extension.LOWERCASE);      
                                                  };