Skip to main content
Inspiring
August 10, 2016
Answered

Creating history snapshots - code not working

  • August 10, 2016
  • 1 reply
  • 513 views

I got this code from the forums here - just did some minor cleanup (using stringIDToTypeID instead of charIDToTypID which isn't as readable).

But this code doesn't execute. It runs fine up till the point where I call executeAction() - what is wrong? JSX code below!

Create

function sTID(s) { return stringIDToTypeID(s); };

function createSnapshot(name){

// Creates a history snapshot. Not possible via DOM - have to use Action Manager

    alert("inside createSnapshot");

    var desc = new ActionDescriptor();

    var ref1 = new ActionReference();

    var ref2 = new ActionReference();

    ref1.putClass( sTID("Snapshot") );

    desc.putReference( sTID("Null"), ref1 );

    ref2.putProperty( sTID("HistoryState"), sTID("CurrentHistoryState") );

    desc.putReference( sTID("From"), ref2 );

    desc.putString( sTID("Name"), name );

    desc.putEnumerated( sTID("Using"), sTID("HistoryStateSource"), sTID("FullDocument") );

    executeAction( sTID("Make"), desc, DialogModes.NO ); // Script halts here

}

HistoryState and HistoryStateSource has a conflicting CharID "HstS" so naturally I've tested all 4 possible setups there - neither one works!

Other functions (untested):

Set

function setSnapshot(name){

// Sets the history to a previous snapshot. Not possible via DOM - have to use Action Manager

    alert("inside setSnapshot");

    var desc = new ActionDescriptor();

    var ref = new ActionReference();

    ref.putName( sTID("Snapshot"), name );

    desc.putReference( sTID("Null"), ref );

    executeAction( sTID("Select"), desc, DialogModes.NO );

    alert("reverted snapshot");

}

Delete

function removeSnapshot(name){

// Removes a history snapshot. Not possible via DOM - have to use Action Manager

    alert("inside removeSnapshot");

    var desc = new ActionDescriptor();

    var ref = new ActionReference();

    ref.putName( sTID("Snapshot"), name );

    desc.putReference( sTID("Null"), ref );

    executeAction( sTID("Delete"), desc, DialogModes.NO );

    alert("removed snapshot");

}

This topic has been closed for replies.
Correct answer SuperMerlin

You have all the names wrong.

function createSnapshot(name){ 

function sTID(s) { return stringIDToTypeID(s); };

    var desc = new ActionDescriptor(); 

    var ref1 = new ActionReference(); 

    var ref2 = new ActionReference(); 

    ref1.putClass( sTID("snapshotClass") ); 

    desc.putReference( sTID("null"), ref1 ); 

    ref2.putProperty( sTID("historyState"), sTID("currentHistoryState") ); 

    desc.putReference( sTID("from"), ref2 ); 

    desc.putString( sTID("name"), name ); 

    desc.putEnumerated( sTID("using"), sTID("historyState"), sTID("fullDocument") ); 

    executeAction( sTID("make"), desc, DialogModes.NO );

};

Allways check what the stringId of a charId is by using:-

$.writeln(typeIDToStringID(charIDToTypeID('From')));

1 reply

SuperMerlin
SuperMerlinCorrect answer
Inspiring
August 10, 2016

You have all the names wrong.

function createSnapshot(name){ 

function sTID(s) { return stringIDToTypeID(s); };

    var desc = new ActionDescriptor(); 

    var ref1 = new ActionReference(); 

    var ref2 = new ActionReference(); 

    ref1.putClass( sTID("snapshotClass") ); 

    desc.putReference( sTID("null"), ref1 ); 

    ref2.putProperty( sTID("historyState"), sTID("currentHistoryState") ); 

    desc.putReference( sTID("from"), ref2 ); 

    desc.putString( sTID("name"), name ); 

    desc.putEnumerated( sTID("using"), sTID("historyState"), sTID("fullDocument") ); 

    executeAction( sTID("make"), desc, DialogModes.NO );

};

Allways check what the stringId of a charId is by using:-

$.writeln(typeIDToStringID(charIDToTypeID('From')));

HeimdaalAuthor
Inspiring
August 11, 2016

Thanks a bunch. That certainly helps - especially that last line of code.
I can't get this to work with my setSnapshot -function though. I've changed the stringID's to lowercase and I'm using "snapshotClass":

function sTID(s) { return stringIDToTypeID(s); };

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putName( sTID("snapshotClass"), name ); // I've also tried the stringID "snapshot". The variable name is just a string with the snapshot name

desc.putReference( sTID("null"), ref );

executeAction( sTID("select"), desc, DialogModes.NO );

SuperMerlin
Inspiring
August 11, 2016

This works for me..

function setSnapshot(name){ 

        function sTID(s) { return stringIDToTypeID(s); };

        var desc = new ActionDescriptor(); 

        var ref = new ActionReference(); 

        ref.putName( sTID("snapshotClass"), name ); 

        desc.putReference( sTID("null"), ref );      

        executeAction( sTID("select"), desc, DialogModes.NO );  

};