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.
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.
There is a global object
app, which represents the application.
app.activeDocument lets you access the active document.
A document has properties (
and methods (
A layer also has properties (
and methods (
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()).
If your script creates files in a filesystem (e.g.
Photopea will offer you a ZIP archive with newly created files, after the script finishes.
Photopea extends the model of Photoshop by adding several new functions:
|App.open(url, as, asSmart)||Loads an image from URL. Set
|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.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.convertToSO()||Converts a layer (or a folder) into a Smart Object|
|Layer.selected||Boolean: is layer selected ("highlighted") - read only|