Skip to main content
Stephen Marsh
Community Expert
Community Expert
December 9, 2015
Answered

Save all history snapshots as files

  • December 9, 2015
  • 2 replies
  • 6991 views

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

This topic has been closed for replies.
Correct answer SuperMerlin

‌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.


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);

    var saveFile = File(outputFolder + "/" + Snaps.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.snapshot) {

       doc.activeHistoryState = hs;

       Name.push(hs.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 );

};

Stephen Marsh
Community Expert
Community Expert
April 19, 2024

As the forum software change broke the code, here it is again:

 

/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/save-all-history-snapshots-as-files/m-p/7857534 */
#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;
psdSaveOptions.annotations = true;
psdSaveOptions.spotColors = 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 );  
};  
JJMack
Community Expert
Community Expert
December 10, 2015

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;

   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

JJMack
Stephen Marsh
Community Expert
Community Expert
January 2, 2016

‌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.

JJMack
Community Expert
Community Expert
January 2, 2016

Adobe Photoshop CC 2015 JavaScript Reference

 

Code for Save can be found in this forum and in the above manual.

 

Scripting a Save As command

JJMack