Using BatchPlay to save a PNG in API 2

I’m currently working on a plugin that duplicates a layer group to a new document and saves as a PNG using the SuperPNG plugin. I’ve gotten lots of the foundations for my plugin up and running, and most things work, except for actually saving the files.

I’ve spent a significant amount of time searching for answers, and I am unable to get a document to save using a BatchPlay action. I’ve even tried simple operations such as resizing, with code copied from Alchemist, but with no success. I’ve searched through the UXP documentation, but it can be a bit confusing and doesn’t always have complete samples it seems.

Are there any comprehensive samples out there that show how to achieve something like this in API 2? I’ve directly copied several different samples from this forum, and I can’t seem to get the action to execute.

Any guidance would be appreciated! I’m a bit new to JavaScript, so I’m sure my problem stems from that. :grin:

Running Photoshop 23.4.1

Could you post some of your code that hasn’t worked - it will help diagnose where you are going wrong

I didn’t include any code as I didn’t want to just post other people’s code (I’ve just been trying code snippets directly from the forums to see if I can get anything to work), however here are the two topics I have referenced and directly tried:

I’ve taken the code as is and modified it with any additions in the replies that supposedly make it work. The code was put in an async function being called on the click event of a button.

From my understanding after doing more research, ExecuteAsModal is required in API 2, though I’ve found the documentation on this a bit hard to follow.

As a place to start though, here’s a sample I’ve put together that doesn’t work based on output from Alchemist:

async function OnExportButtonClick()
{
    const fs = require("uxp").storage.localFileSystem;
    const persistentFolder = await fs.getFolder();
    const newFile = await persistentFolder.createFile("texture.png", { overwrite: true });
    const saveFile = await fs.createSessionToken(newFile);

    const batchPlay = require("photoshop").action.batchPlay;
    const result = await batchPlay(
        [
           {
              _obj: "save",
              as: {
                 _obj: "fnord SuperPNG",
                 $pngC: 9,
                 $pngF: 248,
                 $pngS: 0,
                 $pngQ: false,
                 $pngA: {
                    _enum: "$alfT",
                    _value: "$alfC"
                 },
                 $pngX: false,
                 $pngI: false,
                 $pngM: true
              },
              in: {
                 _path: saveFile,
                 _kind: "local"
              },
              documentID: 219,
              copy: true,
              lowerCase: true,
              alphaChannels: false,
              saveStage: {
                 _enum: "saveStageType",
                 _value: "saveBegin"
              },
              _options: {
                 dialogOptions: "dontDisplay"
              }
           }
        ],{
           synchronousExecution: false,
           modalBehavior: "fail"
        });
}
{
           synchronousExecution: false,
           modalBehavior: "fail"
        }

in API 2 you need to set modalBehavior to “execute” or just leave it empty like this {}
check this

also you need to run the functions “executeAsModal” check this executeAsModal example

Thanks, I’ve wrapped the batchPlay in executeAsModal, and also changed synchronousExecution to execute, but it’s still not running.

async function OnExportButtonClick()
{
    const fs = require("uxp").storage.localFileSystem;
    const persistentFolder = await fs.getFolder();
    const newFile = await persistentFolder.createFile("texture.png", { overwrite: true });
    const saveFile = await fs.createSessionToken(newFile);

    const batchPlay = require("photoshop").action.batchPlay;
    const result = async () => {
        await core.executeAsModal(() => {
            return batchPlay(
                [
                    {
                       _obj: "save",
                       as: {
                          _obj: "fnord SuperPNG",
                          $pngC: 9,
                          $pngF: 248,
                          $pngS: 0,
                          $pngQ: false,
                          $pngA: {
                             _enum: "$alfT",
                             _value: "$alfC"
                          },
                          $pngX: false,
                          $pngI: false,
                          $pngM: true
                       },
                       in: {
                          _path: saveFile,
                          _kind: "local"
                       },
                       documentID: 219,
                       copy: true,
                       lowerCase: true,
                       alphaChannels: false,
                       saveStage: {
                          _enum: "saveStageType",
                          _value: "saveBegin"
                       },
                       _options: {
                          dialogOptions: "dontDisplay"
                       }
                    }
                 ],
                 {
                    synchronousExecution: false,
                    modalBehavior: "execute"
                 }
            )
        })
    };
}