I want to run a photoshop javascript that resizes and saves images, but I want, at the end, to restore the document to its original state.
In photoshop I would just mark back the History pane, undoing the last commands.
How should I do it in a script?
Or, alternatively, what is the best way to restore the document back to the original state?
Do not assume the document is originally saved, it might be a document in the works.
I want to run a photoshop javascript that resizes and saves images, but I want, at the end, to restore the document to its original state.
In photoshop I would just mark back the History pane, undoing the last commands.
How should I do it in a script?
Or, alternatively, what is the best way to restore the document back to the original state?
Do not assume the document is originally saved, it might be a document in the works.
Share Improve this question asked Mar 9, 2013 at 17:23 ilomamboilomambo 8,35013 gold badges61 silver badges107 bronze badges 2 |4 Answers
Reset to default 9Save the active history state:
var savedState = app.activeDocument.activeHistoryState
Then when you are done with whatever you are doing:
app.activeDocument.activeHistoryState = savedState
I avoid the history state approach - I have found it doesn't always work well. Instead, if I want to do temporary changes I duplicate the document and work on that. Never fails.
Revert Image from the ScriptListener plugin:
var idRvrt = charIDToTypeID( "Rvrt" );
executeAction( idRvrt, undefined, DialogModes.NO );
The answer is:
Save the last history command before you start:
This is tricky and may not always work, because the historyStates list includes also history comands you "undo" and are grayed. But let's assume all the last command is not grayed.history = doc.historyStates.length - 1;
Restore the history state when you finish:
doc.activeHistoryState = doc.historyStates[history];
Delete from history all the commands your script generated:
app.purge (PurgeTarget.HISTORYCACHES);
You may skip #3, in which case the commands you executed on your script will remain in the history list, grayed (and if you repeat the script you will end up not restoring the last actual command, but the last command of the previous script run).
Alternatively to avoid this problem you can also purge the history grayed commands before step #1.
this.naturalHeight
/this.naturalWidth
(wherethis
refers to theimg
element)? – David Thomas Commented Mar 9, 2013 at 17:26