According to the documentation for the Photopea API, the “script” parameter of the JSON configuration object “should be executed after loading each file (can be long)”
However, when I test the following simple JSON object within the Playground page, it appears that the script is trying to run before the files have fully loaded:
{
"files": [
"https://www.photopea.com/api/img2/pug.png",
"https://www.photopea.com/api/img2/pug.png"
],
"environment": {
},
"script" : "app.documents[1].rotateCanvas(90);"
}
The script attempts to rotate the canvas of the second document, which presumably fails because the script executes before the second file has loaded.
How, using the API, can I get the script to run only after all the documents have loaded?
Edit
I found a solution. Posting as an update since the powers that be closed my question.
The script runs after each file is loaded (not after all files are loaded as I had mistakenly read). So, to ensure that the code runs after all files are loaded we can check to see that both files are loaded first before continuing like so:
{
"files": [
"https://www.photopea.com/api/img2/pug.png",
"https://www.photopea.com/api/img2/pug.png"
],
"environment": {
},
"script" : "if (app.documents.length === 2){ app.documents[1].rotateCanvas(90);}"
}
1