Scripts

In some cases, it is very useful to automate image editing. For example, we may need to have 30 copies of the layer, and place them all into a regular grid, 5 times 6, and gradually decrease their opacity. Or if we need to rotate by 90 degrees each layer, that contains "rotate" in its name.

Photopea can execute scripts written in Javascript. It is very useful to know programming and the syntax of Javascript, however, even beginners can make simple scripts, which will do the job. You can learn how to write scripts from available demos.

Use the Script window (File - Script) to work with scripts. Here you can type (or paste) a new script, and then click Run to execute it. Several demos are available in the top of the Script window. Scripts can be also executed through the Photopea API.

Document Model

Scripts allow you to access the content of the document through Javascript code. Instead of describing a new Document model, Photopea provides an interface similar to Adobe's scripting interface. It means, that the same scripts, that you wrote previously for Adobe Photoshop, could be used in Photopea to do the same task.

There is a global object app, which represents the application. app.activeDocument lets you access the active document. A document has properties (width, height, layers, currentLayer, ...) and methods (resizeImage(), resizeCanvas(), ...). A layer also has properties (name, visible, opacity, ...) and methods (rotate(), translate(), ...). You can learn more in the official reference or in demos. Note, that many properties are read-only, you can not rewrite them directly (e.g. the width of the document - you must call resizeCanvas()).

Photopea extends the model of Photoshop by adding several new functions:

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).
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.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.convertToSO() Converts a layer (or a folder) into a Smart Object

Comments