最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Photoshop javascript How to restore original document state after script - Stack Overflow

programmeradmin2浏览0评论

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
  • I've never scripted Photoshop, but can you easily duplicate a document through the API? Maybe doing the changes on a duplicate and closing that after saving is the best way to go – Pekka Commented Mar 9, 2013 at 17:25
  • I don't know how complete PS JS is, but do you have this.naturalHeight/this.naturalWidth (where this refers to the img element)? – David Thomas Commented Mar 9, 2013 at 17:26
Add a comment  | 

4 Answers 4

Reset to default 9

Save 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:

  1. 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;
    
  2. Restore the history state when you finish:

    doc.activeHistoryState = doc.historyStates[history];
    
  3. 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.

发布评论

评论列表(0)

  1. 暂无评论