Skip to main content
Participating Frequently
February 16, 2010
Question

creating snapshots

  • February 16, 2010
  • 2 replies
  • 3830 views

In javascript, what's the command to add snapshots of a document's current history state? 

This topic has been closed for replies.

2 replies

Inspiring
April 19, 2023

This is an iteration on the existing idea. Instead of reverting to the last snapshot, we can save the snapshot to a variable when we make it. This allows us more freedom to manage multiple snapshots at the same time.

Paul Riggott
Inspiring
February 16, 2010

takeSnapshot();

function takeSnapshot () {
   var desc = new ActionDescriptor();
   var sref = new ActionReference();
   sref.putClass(charIDToTypeID("SnpS"));
   desc.putReference(charIDToTypeID("null"), sref);
   var fref = new ActionReference();
   fref.putProperty(charIDToTypeID("HstS"), charIDToTypeID("CrnH"));
   desc.putReference(charIDToTypeID("From"), fref );
   executeAction(charIDToTypeID("Mk  "), desc, DialogModes.NO );
}

function revertToLastSnapshot() {
   var docRef = app.activeDocument;
   var hsObj = docRef.historyStates;
   var hsLength = hsObj.length;
   for (var i=hsLength - 1;i>-1;i--) {
     if (hsObj.snapshot) {
       docRef.activeHistoryState =
docRef.historyStates.getByName('Snapshot ' + i);
       break;
     }
   }
}

Participating Frequently
February 19, 2010

Is the function revertToLastSnapshot needed since it doesn't seem to be called anywhere?

Also, where can I find the complete set of Event ID Codes?  I was looking at the Photoshop CS4 JavaScript Ref and they don't seem to have all of them listed.

Paul Riggott
Inspiring
February 20, 2010

revertToLastSnapshot is not used in the example, it's there to show how to revert if needed.

There isn't a complete list of event id's but if you install the ScriptListner plugin you can look at the output and see the event id's that it has generated.