Photoshop JavaScript如何在脚本运行后恢复原始文档状态

3
我想运行一个Photoshop JavaScript,可以调整大小并保存图像,但我希望在最后将文档恢复到原始状态。在Photoshop中,我只需标记回历史面板,撤消最后的命令即可。那么在脚本中该如何做呢?或者说,最好的方法是将文档恢复到原始状态吗?请注意,不要假设文档最初已保存,它可能是正在进行中的文档。

我从未编写过Photoshop脚本,但是你能否通过API轻松复制文档呢?也许在副本上进行更改并在保存后关闭副本是最好的方法。 - Pekka
我不知道PS JS有多完整,但你有this.naturalHeight / this.naturalWidth吗(其中this指的是img元素)? - David Thomas
4个回答

9
保存活动历史状态:
var savedState = app.activeDocument.activeHistoryState

当你完成你正在做的事情时:

app.activeDocument.activeHistoryState = savedState

1
这比操作 app.activeDocument.historyStates 要可靠得多,后者包括灰色(撤消)状态。 - NateS

5

我避免使用历史状态方法 - 我发现这种方法并不总是有效。相反,如果我想做临时更改,我会复制文档并在副本上进行操作,从未失效。


@anna-forest 你会怎么做呢?我猜 var docCopy = app.activeDocument 只会生成一个引用副本而不是深度复制。 - damirstuhec
3
var docCopy = app.activeDocument.duplicate() 变量 docCopy = app.activeDocument.duplicate() - Anna Forrest
@anna-forest 谢谢,那么如何正确地丢弃重复的文档呢?谢谢。 - DashaLuna
我已经很多年没有积极地进行任何Photoshop脚本编写了,但我相信我只是关闭了文档而没有保存它。 - Anna Forrest

5

从ScriptListener插件还原图像:

var idRvrt = charIDToTypeID( "Rvrt" );
executeAction( idRvrt, undefined, DialogModes.NO );

这将使文档恢复到上次保存的状态...在某些情况下并不安全。ilomambo下面的答案是正确的方法,因为它只会撤消您指定的命令数量。 - sudo
正是我需要的。谢谢。 - NER1808

4
答案是:
  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);
    

您可以跳过第三步,这样您在脚本上执行的命令将保留在历史列表中,并变灰(如果您重复运行脚本,则最后一个实际命令不会被恢复,而是上次运行脚本时的最后一个命令)。
为了避免此问题,您也可以在第一步之前清除历史记录中的灰色命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接