Photoshop javascript How to restore original document state after script
javascript, photoshop
Solution
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.
Problem
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.