How to save a file

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

It looks like you need to define “fs”:

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

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;

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

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"

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

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