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 Save button for saving scripts (they will stay even after closing Photopea). The list of saved scripts is at the bottom of the Script window. Photopea saves these files in a Local Storage, which can be managed through Edit - Local Storage.
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).|
|App.UI||Modify the "viewing state". Has methods:
|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), optional parameters after the colon:
|Layer.selected||Boolean: is layer selected ("highlighted") - read only|
|TextItem.totalTextStyle||String: a JSON object with all style parameters of the text|
|TextItem.transform||String: a JSON array with the affine transform matrix of the text|