Auto-saving an open document

I’ve been reading the threads here, and I’m starting to think the following is not possible, but I’ll ask.

I have the user select a folder:

var sourceFolder = await require("uxp").storage.localFileSystem.getFolder();

and I store that folder’s path in a variable called folderPath:

var folderPath = sourceFolder.name;

Now I want to use that folderPath variable in batchPlay to save the current open document as a .tif file:

await require('photoshop').action.batchPlay([ {"_obj": "save","as": {"_obj": "TIFF","byteOrder": {"_enum": "platform","_value": "IBMPC"},"layerCompression": {"_enum": "encoding","_value": "RLE"}},"in": {"_path": folderPath + "\\" + documentTitle + ".tif"}}, ], {"synchronousExecution": false, "modalBehavior": "wait"})

I know that in UXP, the path in,

"_path": folderPath + "\\" + documentTitle + ".tif"

needs to be replaced by a token.

But it doesn’t look like there is any way to generate a token using just that “folderPath” variable. It seems like I’d have to at least force the user to selected the folder to save in again, which sort of defeats the purpose, as this is intended to auto-save the user’s document once they create it.

var saveFolder = await require("uxp").storage.localFileSystem.getFolder();
var saveFile = await saveFolder.createFile("fileName.tif");
var saveFileToken = await require("uxp").storage.localFileSystem.createSessionToken(saveFile);
await saveTIF(saveFileToken);

async function saveTIF(fileToken){
    
const batchPlay = require("photoshop").action.batchPlay;

await batchPlay(
[
   {
      "_obj": "save",
      "as": {
         "_obj": "TIFF",
         "byteOrder": {
            "_enum": "platform",
            "_value": "IBMPC"
         },
         "LZWCompression": true,
         "saveTransparency": true,
         "layerCompression": {
            "_enum": "encoding",
            "_value": "RLE"
         }
      },
      "in": {
         "_path": fileToken,
         "_kind": "local"
      },
      "saveStage": {
         "_enum": "saveStageType",
         "_value": "saveBegin"
      },
      "_isCommand": false,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});
      
}

Once the user selects the folder, they don’t need to select the folder again. You just create a file object for each file. Then you create a token for the file object before saving.

Thanks, Damon. I’m going to play with this today and see how far I get.

Success! Once I made the “saveFolder” variable global this worked.