# Using BatchPlay to save a PNG in API 2

**URL:** <https://forums.creativeclouddeveloper.com/t/using-batchplay-to-save-a-png-in-api-2/4831>\
**Category:** UXP Plugin API\
**Tags:** uxp, javascript\
**Created:** [June 22, 2022, 4:18am UTC](https://forums.creativeclouddeveloper.com/t/using-batchplay-to-save-a-png-in-api-2/4831 "2022-06-22T04:18:38Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![HughesMDflyer4](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/hughesmdflyer4/32/2219_2.png) [@HughesMDflyer4](https://forums.creativeclouddeveloper.com/u/HughesMDflyer4)\
**Post date:** [June 23, 2022, 4:13am UTC](https://forums.creativeclouddeveloper.com/t/using-batchplay-to-save-a-png-in-api-2/4831/3 "2022-06-23T04:13:43Z")

</div>

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:

> [@How to save a file](http://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678):
>
> I’m trying to save a file, either to TIF or JPG. I went through few threads on this board trying to piece the code together, but can’t get it to work. Can you look at the following code and tell me what I need to change. When I run it I get the folder picker dialog, select folder, but no file gets saved. Debugger doesn’t show any errors. const persistentFolder = await fs.getFolder(); const newFile = await persistentFolder.createFile("newfile.tif", {overwrite: true}); const saveFile = await …

> [@Saving a PNG file with UXP?](http://forums.creativeclouddeveloper.com/t/saving-a-png-file-with-uxp/3698):
>
> Is there a simple way to save a .png file of the active document in UXP? const uxp = require('uxp'); const lfs = uxp.storage.localFileSystem; const userFolder = lfs.getFolder(); const file = userFolder.createFile("test.png"); My problem boils down to the code above. I correctly get a dialog to select the folder, select eg. the desktop or c: drive, after that all I get is “createFile is not a function”. Is getFolder failing for some reason or has something changed about create…

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:

```auto
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"
        });
}

```

---

_[View the full topic](https://forums.creativeclouddeveloper.com/t/using-batchplay-to-save-a-png-in-api-2/4831)._
