Skip to main content
Mohamed Hameed21513110
Inspiring
August 20, 2023
Answered

delete the last three history state

  • August 20, 2023
  • 2 replies
  • 1024 views

welcome everybody

- I want a code that deletes the last three history state

 

This topic has been closed for replies.
Correct answer Stephen Marsh

@Mohamed Hameed21513110 

 

Try the following code, it has not been exhaustively tested, however, it would appear to do what you have requested:

 

/*
Delete The Last 3 History States.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/delete-the-last-three-history-state/td-p/14022026
v1.0 - 21st August 2023, Stephen Marsh
*/

#target photoshop

var theStateLength = activeDocument.historyStates.length - 1;
if (theStateLength >= 4) {
    // Perform the tasks 3 times
    for (i = 0; i < 3; i++) {
        // Select the last history step
        activeDocument.activeHistoryState = activeDocument.historyStates[activeDocument.historyStates.length - 1];
        // Delete the active history step
        removeHistoryStep();
    }
    alert("The last 3 history states have been deleted!")
} else {
    alert("Script cancelled as there are only 3 or less history states!")
}

function removeHistoryStep() {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putProperty(s2t("historyState"), s2t("currentHistoryState"));
    descriptor.putReference(s2t("null"), reference);
    executeAction(s2t("delete"), descriptor, DialogModes.NO);
}

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
August 21, 2023

@Mohamed Hameed21513110 

 

Try the following code, it has not been exhaustively tested, however, it would appear to do what you have requested:

 

/*
Delete The Last 3 History States.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/delete-the-last-three-history-state/td-p/14022026
v1.0 - 21st August 2023, Stephen Marsh
*/

#target photoshop

var theStateLength = activeDocument.historyStates.length - 1;
if (theStateLength >= 4) {
    // Perform the tasks 3 times
    for (i = 0; i < 3; i++) {
        // Select the last history step
        activeDocument.activeHistoryState = activeDocument.historyStates[activeDocument.historyStates.length - 1];
        // Delete the active history step
        removeHistoryStep();
    }
    alert("The last 3 history states have been deleted!")
} else {
    alert("Script cancelled as there are only 3 or less history states!")
}

function removeHistoryStep() {
    function s2t(s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putProperty(s2t("historyState"), s2t("currentHistoryState"));
    descriptor.putReference(s2t("null"), reference);
    executeAction(s2t("delete"), descriptor, DialogModes.NO);
}

 

Mohamed Hameed21513110
Inspiring
August 21, 2023

@Stephen Marsh 

Thank you very much for your help
The code has already worked very well
Thank you again sir

Stephen Marsh
Community Expert
Community Expert
August 21, 2023

@Mohamed Hameed21513110 

You're welcome, and it was constructive that you did post the code to select the last snapshot, it shows that you are at least trying.

c.pfaffenbichler
Community Expert
Community Expert
August 20, 2023

DOM appears to offer no Methods for HistoryState so please record the operation with ScriptingListener.plugin yourself. 

 

Mohamed Hameed21513110
Inspiring
August 20, 2023

@c.pfaffenbichler 

Aha ok sir
You mean that there is no direct command to delete history satate
Is this what you mean sir??

 

I found this code works to set the last history state
How can I delete the last specified record

var doc = app.activeDocument;  
	doc.activeHistoryState = doc.historyStates[doc.historyStates.length-1]; 

 

c.pfaffenbichler
Community Expert
Community Expert
August 20, 2023
quote

I found this code works to set the last history state
How can I delete the last specified record

var doc = app.activeDocument;  
	doc.activeHistoryState = doc.historyStates[doc.historyStates.length-1]; 

That code does not delete a HistoryState it selects one (makes it the active one). 

 

quote

@c.pfaffenbichler 

Aha ok sir
You mean that there is no direct command to delete history satate
Is this what you mean sir??

I mean you should use ScriptingListener.plusing to record deleting the last HistoryState.