# How to save a file

**URL:** <https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678>\
**Category:** UXP Plugin API\
**Created:** [March 21, 2021, 12:10am UTC](https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678 "2021-03-21T00:10:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkupcik](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@pkupcik](https://forums.creativeclouddeveloper.com/u/pkupcik)\
**Post date:** [March 21, 2021, 12:10am UTC](https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678/1 "2021-03-21T00:10:07Z")

</div>

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 fs.createSessionToken(newFile);

    const result = await batchPlay(
        [
           {
              "_obj": "save",
              "as": 
              {
                 "_obj": "TIFF",
                 "byteOrder": 
                 {
                    "_enum": "platform",
                    "_value": "IBMPC"
                 },
                 "layerCompression": 
                  {
                    "_enum": "encoding",
                    "_value": "RLE"
                  }
              },
              "in": {
                 "_path": saveFile,
                 "_kind": "local"
              }
           }
        ]
        );
```

---

<div class="post-metadata">

**Author:** ![AnthonyK](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AnthonyK](https://forums.creativeclouddeveloper.com/u/AnthonyK)\
**Post date:** [March 21, 2021, 1:27am UTC](https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678/2 "2021-03-21T01:27:07Z")

</div>

It looks like you need to define “fs”:

`const fs = require('uxp').storage.localFileSystem;`

---

<div class="post-metadata">

**Author:** ![pkupcik](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@pkupcik](https://forums.creativeclouddeveloper.com/u/pkupcik)\
**Post date:** [March 21, 2021, 10:54am UTC](https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678/4 "2021-03-21T10:54:25Z")

</div>

I do have that declared outside of the function containing the code I posted. Should have included it for completeness.

```
const batchPlay = require("photoshop").action.batchPlay;
const fs = require("uxp").storage.localFileSystem;
```

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [March 21, 2021, 6:44pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678/5 "2021-03-21T18:44:36Z")

</div>

What’s the return value from `result`? It’s possible `batchPlay` is returning you an error in `result` (`batchPlay` generally doesn’t `throw`).

---

<div class="post-metadata">

**Author:** ![pkupcik](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@pkupcik](https://forums.creativeclouddeveloper.com/u/pkupcik)\
**Post date:** [March 21, 2021, 11:24pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678/6 "2021-03-21T23:24:23Z")

</div>

So this is interesting, it turns out batchPlay is throwing an exception, but the debugger doesn’t catch it. I added try catch block around batchPlay and now can see an exception:

```
e: Error: Argument 2 is missing
code: "999"
number: 999
message: "Argument 2 is missing"
stack: "Error: Argument 2 is missing"
```

---

<div class="post-metadata">

**Author:** ![pkupcik](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@pkupcik](https://forums.creativeclouddeveloper.com/u/pkupcik)\
**Post date:** [March 21, 2021, 11:31pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-save-a-file/2678/7 "2021-03-21T23:31:47Z")

</div>

Never mind, figured it out, not sure how I missed it, but I was missing the 2nd batchPlay argument

```
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});
```
