11 Replies Latest reply on Jan 3, 2016 1:51 AM by Stephen_A_Marsh

    Save all history snapshots as files

    Stephen_A_Marsh Adobe Community Professional

      I must confess, I like saving history snapshots! Sometimes I wish to save them as a file for later reference once the current image has been closed.

       

      I have written an action to save the current 10 snapshots out as files. It works fine with the following limitations:

       

      1) It only saves 10 snapshots (this is easily expanded upon if required)

      2) The snapshots have to be named  “Snapshot 1”, “Snapshot 2” etc.

       

      This action does not work if the snapshot has a different name.

       

      Does anybody know of a script that does similar?

       

      Here is a sample of the action steps saved as text:

       

      Set: Save First 10 History Snapshots to File
        Action: Save First 10 History Snapshots to File
        Select snapshot “Snapshot 1”
        Make document
        Using: Current History State
        Save
        Close
        Select snapshot “Snapshot 2”
        Make document
        Using: Current History State
        Save
        Close
      
        • 1. Re: Save all history snapshots as files
          JJMack Most Valuable Participant

          It would be very easy to run through the history state and act upon snapshot,  You will have the snapshot name and you can select them.  You will most likely need to use Action manager scriptlisener cot to do the "Make Document Using Current History state.

           

          saveCurrentHistoryState= app.activeDocument.activeHistoryState; // Remember current

           

          for (var i=0,len=app.activeDocument.historyStates.length;i<len;i++) {

             var historyStateRef = app.activeDocument.historyStates[i];

             if (historyStateRef.snapshot) {

                alert("Index " + i + " " + historyStateRef.typename + "' for document '" + historyStateRef.parent + " is a snapshop with a name '" + historyStateRef.name );

                app.activeDocument.activeHistoryState = historyStateRef; //select this history state

               // add code to make document usind current histort state then save and close here

             }

          };

           

          app.activeDocument.activeHistoryState = saveCurrentHistoryState; //select this history state

          • 2. Re: Save all history snapshots as files
            Stephen_A_Marsh Adobe Community Professional

            Thanks JJMack, I can't script so I can't do anything with your snippet. I was hoping that somebody knew of a script for this, I presumed that this would be a common enough task.

            • 4. Re: Save all history snapshots as files
              Stephen_A_Marsh Adobe Community Professional

              It would be just as useful to provide me with a foreign language dictionary and grammar guide... Hmmm, you just did! :]

               

              Coding comes easy to some. I am not one of those, which is why I was looking for an existing script, there are many out there.

              • 5. Re: Save all history snapshots as files
                SuperMerlin Level 5

                This script will save all snapshots in a folder called snapshots off the documents path, each file is saved as a jpg with the name of the snapshot.

                 

                #target photoshop;
                app.bringToFront();
                main();
                function main(){
                if(!documents.length) return;
                try{
                    var Path = activeDocument.path;
                    }catch(err){
                        alert("You save your document before running this script!");
                        return;
                        }
                var doc = app.activeDocument;
                var now = doc.activeHistoryState;
                var Snaps = snapShotList();
                var outputFolder = Folder(Path + "/snapshots");
                if(!outputFolder.exists) outputFolder.create();
                for (var z in Snaps){
                    revertNamedSnapshot(Snaps[z]);
                    var saveFile = File(outputFolder + "/" + Snaps[z].toString().replace(/\./g,'_') + ".jpg");
                    SaveForWeb(saveFile,80);
                    }
                doc.activeHistoryState = now;
                };
                
                function SaveForWeb(saveFile,jpegQuality) {
                var sfwOptions = new ExportOptionsSaveForWeb(); 
                   sfwOptions.format = SaveDocumentType.JPEG; 
                   sfwOptions.includeProfile = false; 
                   sfwOptions.interlaced = 0; 
                   sfwOptions.optimized = true; 
                   sfwOptions.quality = jpegQuality;
                activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
                };
                function snapShotList(){
                var doc = app.activeDocument;
                var hs = doc.historyStates;
                var now = doc.activeHistoryState;
                var Name = new Array();
                for(var a =0;a <hs.length;a++){
                   if(hs[a].snapshot) {
                       doc.activeHistoryState = hs[a];
                       Name.push(hs[a].name);
                       }
                    }
                doc.activeHistoryState = now;
                return Name;
                };
                function revertNamedSnapshot(name) {
                var desc = new ActionDescriptor();
                var ref = new ActionReference();
                ref.putName( charIDToTypeID('SnpS'), name );
                desc.putReference( charIDToTypeID('null'), ref );
                executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
                };
                
                
                
                • 6. Re: Save all history snapshots as files
                  JJMack Most Valuable Participant

                  Coding is not easy like everything it must be learned.  I just hack at it.  If I want something I will work at it.  If you want a custom script you should try learning a little about scripting like you learned a bit about Actions and other Photoshop features.  You most likely have edit and action or to know the Action Palette is as editor as well as a recorder.   You also know how to use a text editor.  That is what you edit scripts with.

                  • 7. Re: Save all history snapshots as files
                    Stephen_A_Marsh Adobe Community Professional

                    Thank you SuperMerlin, that is fantastic – greatly appreciated! I should be able to edit this script to use a lossless format like TIFF or PSD. I’ll post back if I run into problems.

                     

                    JJMack, I hear what you are saying and as I have been using Photoshop since v2.0 I do take a lot of past learning for granted. That being said, I am pretty much a GUI user, that is how I think. Learning text based languages are simply just not as easy for me. I have tried, I struggle with GREP/RegEx!

                    • 8. Re: Save all history snapshots as files
                      JJMack Most Valuable Participant

                      I will never be able to do regular expressions.  Decoding readable code is hard for me. Code like regular expressions, reverse polish notation one line APL programs are for machine not humans. .

                      • 9. Re: Save all history snapshots as files
                        Stephen_A_Marsh Adobe Community Professional

                        OK, so my Frankenstein’s monster code is below, I have commented out the JPEG options and transplanted some code for saving PSD, however I think that I am going wrong somewhere on line 23?

                         

                        I have tried different combinations of code from the commented lines 02 and 03.

                         

                        I have commented out the original JPEG options for reference (lines 24, 38-46).

                         

                        I am like a bull in a china shop when it comes to code!

                         

                        // https://forums.adobe.com/thread/2034807
                        // http://stackoverflow.com/questions/5796764/from-photoshop-actions-to-photoshop-scripting
                        // https://github.com/joonaspaakko/Photoshop-Auto-Save-PSD-script/blob/master/Auto%20Save%20PSD.jsx
                        #target photoshop; 
                        app.bringToFront(); 
                        main(); 
                        function main(){ 
                        if(!documents.length) return; 
                        try{ 
                            var Path = activeDocument.path; 
                            }catch(err){ 
                                alert("You save your document before running this script!"); 
                                return; 
                                } 
                        var doc = app.activeDocument; 
                        var now = doc.activeHistoryState; 
                        var Snaps = snapShotList(); 
                        var outputFolder = Folder(Path + "/History Snapshots as PSD"); 
                        if(!outputFolder.exists) outputFolder.create(); 
                        for (var z in Snaps){ 
                            revertNamedSnapshot(Snaps[z]); 
                            var saveFile = File(outputFolder + "/" + Snaps[z].toString().replace(/\./g,'_') + ".psd"); 
                            PhotoshopSaveOptions(psdOptions, true);
                            // SaveForWeb(saveFile,80); 
                            } 
                        doc.activeHistoryState = now; 
                        }; 
                        
                        //Save as PSD
                                var psdOptions = new PhotoshopSaveOptions();
                                psdOptions.alphaChannels = true;
                                psdOptions.annotations = true;
                                psdOptions.embedColorProfile = true;
                                psdOptions.layers = true;
                                psdOptions.spotColors = true;
                        
                        
                        // function SaveForWeb(saveFile,jpegQuality) { 
                        // var sfwOptions = new ExportOptionsSaveForWeb(); 
                        //  sfwOptions.format = SaveDocumentType.JPEG; 
                        //  sfwOptions.includeProfile = false; 
                        //  sfwOptions.interlaced = 0; 
                        //  sfwOptions.optimized = true; 
                        //  sfwOptions.quality = jpegQuality; 
                        // activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions); 
                        // }; 
                        function snapShotList(){ 
                        var doc = app.activeDocument; 
                        var hs = doc.historyStates; 
                        var now = doc.activeHistoryState; 
                        var Name = new Array(); 
                        for(var a =0;a <hs.length;a++){ 
                          if(hs[a].snapshot) { 
                              doc.activeHistoryState = hs[a]; 
                              Name.push(hs[a].name); 
                              } 
                            } 
                        doc.activeHistoryState = now; 
                        return Name; 
                        }; 
                        function revertNamedSnapshot(name) { 
                        var desc = new ActionDescriptor(); 
                        var ref = new ActionReference(); 
                        ref.putName( charIDToTypeID('SnpS'), name ); 
                        desc.putReference( charIDToTypeID('null'), ref ); 
                        executeAction( charIDToTypeID('slct'), desc, DialogModes.NO ); 
                        }; 
                        
                        • 10. Re: Save all history snapshots as files
                          SuperMerlin Level 5

                          This might help you..

                           

                          #target photoshop;
                          app.bringToFront();
                          main();
                          function main(){
                          if(!documents.length) return;
                          try{
                              var Path = activeDocument.path;
                              }catch(err){
                                  alert("You save your document before running this script!");
                                  return;
                                  }
                          var doc = app.activeDocument;
                          var now = doc.activeHistoryState;
                          var Snaps = snapShotList();
                          var outputFolder = Folder(Path + "/snapshots");
                          if(!outputFolder.exists) outputFolder.create();
                          for (var z in Snaps){
                              revertNamedSnapshot(Snaps[z]);
                              //var saveFile = File(outputFolder + "/" + Snaps[z].toString().replace(/\./g,'_') + ".jpg");
                              var saveFile = File(outputFolder + "/" + Snaps[z].toString().replace(/\./g,'_') + ".psd");
                              //var saveFile = File(outputFolder + "/" + Snaps[z].toString().replace(/\./g,'_') + ".tif");
                          
                              //SaveForWeb(saveFile,80);
                              SavePSD(saveFile);
                              //SaveTIFF(saveFile);
                              }
                          doc.activeHistoryState = now;
                          };
                          function SaveTIFF(saveFile){
                          tiffSaveOptions = new TiffSaveOptions(); 
                          tiffSaveOptions.embedColorProfile = true;
                          tiffSaveOptions.byteOrder = ByteOrder.IBM;
                          tiffSaveOptions.transparency=true;
                          tiffSaveOptions.interleaveChannels=true;
                          tiffSaveOptions.alphaChannels = false; 
                          tiffSaveOptions.imageCompression = TIFFEncoding.NONE; 
                          activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE); 
                          };
                          function SavePSD(saveFile){ 
                          psdSaveOptions = new PhotoshopSaveOptions(); 
                          psdSaveOptions.embedColorProfile = true; 
                          psdSaveOptions.alphaChannels = true;  
                          psdSaveOptions.layers = true;  
                          activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE); 
                          };
                          function SaveForWeb(saveFile,jpegQuality) {
                          var sfwOptions = new ExportOptionsSaveForWeb(); 
                             sfwOptions.format = SaveDocumentType.JPEG; 
                             sfwOptions.includeProfile = false; 
                             sfwOptions.interlaced = 0; 
                             sfwOptions.optimized = true; 
                             sfwOptions.quality = jpegQuality;
                          activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
                          };
                          function snapShotList(){
                          var doc = app.activeDocument;
                          var hs = doc.historyStates;
                          var now = doc.activeHistoryState;
                          var Name = new Array();
                          for(var a =0;a <hs.length;a++){
                             if(hs[a].snapshot) {
                                 doc.activeHistoryState = hs[a];
                                 Name.push(hs[a].name);
                                 }
                              }
                          doc.activeHistoryState = now;
                          return Name;
                          };
                          function revertNamedSnapshot(name) {
                          var desc = new ActionDescriptor();
                          var ref = new ActionReference();
                          ref.putName( charIDToTypeID('SnpS'), name );
                          desc.putReference( charIDToTypeID('null'), ref );
                          executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
                          };
                          
                          
                          
                          • 11. Re: Save all history snapshots as files
                            Stephen_A_Marsh Adobe Community Professional

                            Thanks again SuperMerlin, this is exactly what I was looking for and it is within my scripting “skills” to enable/disable existing code if I would like TIFF instead of PSD.