脚本
在某些情况下,自动编辑图像非常有用。例如,我们可能需要30个图层的副本,并将它们全部放入一个规则的网格中,5乘以6,然后逐渐降低它们的不透明度。或者如果我们需要每层旋转90度,它的名称中包含"rotate"
Photopea可以执行用Javascript编写的脚本。了解Javascript的编程和语法是非常有用的,然而,即使是初学者也可以编写简单的脚本,这将完成工作。您可以从可用的演示中学习如何编写脚本
使用"脚本"窗口使用脚本。在这里,您可以键入(或粘贴)一个新脚本,然后单击"运行"来执行它。脚本窗口顶部提供了几个演示。脚本也可以通过photopea api执行
有一个保存脚本的按钮(即使在关闭Photopea之后,它们也会保持不变)。保存的脚本列表位于"脚本"窗口的底部。Photopea将这些文件保存在本地存储中,可以通过"编辑-本地存储"进行管理
文档模型
脚本允许您通过Javascript代码访问文档内容。Photopea没有描述新的文档模型,而是提供了一个类似于adobe脚本界面的界面。这意味着,你之前为Adobe Photoshop编写的相同脚本,可以在Photopea中用于完成相同的任务
有一个全局对象app
,它表示应用程序。app.active document允许您访问活动文档。文档具有属性(宽度
,高度
,层
,当前层
,…)和方法(resizeImage()
,resizeCanvas()
,…)。层还具有属性(名称
,可见
,不透明度
,…)和方法(rotate()
,translate()
,…)。您可以在官方参考或演示中了解更多信息。注意,许多属性是只读的,您不能直接重写它们(例如,文档的宽度-必须调用resizeCanvas())
如果脚本在文件系统中创建文件(例如,Document.exportDocument()
),Photopea将在脚本完成后为您提供一个包含新创建文件的压缩存档
Photopea通过添加几个新功能扩展了Photoshop的模型:
App.open(url, as, asSmart) | Loads an image from URL. Set asSmart to true to paste an image into a current document as a smart object. as is ignored. |
App.echoToOE(string) | Sends a string to an outer environment (See Live Messaging API). |
App.UI | Modify the "viewing state". Has methods: zoomIn(), zoomOut(), fitTheArea(), pixelToPixel() . |
Document.source | read / write: a string, that identifies the document. Initially, it is the URL for files loaded from a server, or "local,X,NAME" for other files. |
Document.name | read / write: a String to be displayed as a "label" of a document. Setting name does not add a step into History. |
Document.saveToOE("png") | Converts the document into a binary file and sends the file to an outer environment (See Live Messaging API). JPG and WEBP can have a quallity (0 to 1) after the colon (e.g. "jpg:0.8"). PSD can have a "true" after the colon: "psd:true", to produce minified PSDs (up to 4x smaller, but take 4x longer to generate). |
Layer.selected | Boolean: is layer selected ("highlighted") - read only |