How do I save a file with batchPlay to the temporary folder?

I want to save an image as a Photoshop Raw file to the temporary folder. I seem to have working code, except it fails as requiring a file token - but why should I need this if I’m saving to the plugins temporary folder? The API documentation seems to indicate that tokens should not be required for the temporary folder.

My code, I’m using a try catch as the debugger fails to catch the error message returned

Error: invalid file token used

  const bp = require("photoshop").action.batchPlay;
  const fs = require("uxp").storage.localFileSystem;
  const tempFolder = await fs.getTemporaryFolder(); // temp
  const fileName = Math.random().toString().substr(2) + ".raw";
  const rawFile = tempFolder.nativePath.concat("\\").concat(fileName);
  
  try {
    const p = await bp([{
      "_obj": "save",
      "as": {
          "_obj": "rawFormat",
          "fileCreator": "8BIM",
          "channelsInterleaved": true
      },
      "in": {
          "_path": rawFile,
          "_kind": "local"
      },
      "lowerCase": true,
      "saveStage": {
          "_enum": "saveStageType",
          "_value": "saveBegin"
      },
    }], {
      "synchronousExecution": false,
      "modalBehavior": "fail"
    });
  }
  catch (e) { console.log(e); }

Batch play does not throw an error AFAIK. It return an array of responses for each descriptor. If there’s an error, last item of that response array (matching descriptor index) will hold the error message. If you want it to throw an error, you have to do it yourself.

Check this awesome snippet how to continue BP on error and there you can find how error is handled. And one comment bellow I posted a bit improved part of that snippet.

Sorry, but this won’t solve your initial problem

Aye thanks. BP not throwing errors to the debugger is a bit confusing to start with, but try-catch handles them fine - although a bit more official documentation on that would have been helpful! I found that after digging thorugh posts on here.

However that the BP call fails because it says it has an invalid token when writing to the temporary folder using the above code doesn’t make much sense. This page

https://www.adobe.io/xd/uxp/develop/reference/uxp/storage-index/#getting-access-to-the-local-file-system

Says

You can use the fs object to access a temporary folder or your plugin’s own folder immediately, or request access to user folders by showing a file picker:

// These require no user interaction:
const tempFolder = await fs.getTemporaryFolder();

And yet, BP fails with Error: invalid file token used when I try to do so,

I was in the same situation as you and had to experiment around, because the documentation was not very helpful. In the end I found out that I had to create a session token. Hopefully this is useful for someone:

const tempFolder = await fs.getTemporaryFolder()
const token = fs.createSessionToken(tempFolder);

const edit = [{
  _obj: "save",
  as: {
    _obj: "JPEG",
    extendedQuality: 9,
    matteColor: {
      _enum: "matteColor",
      _value: "none"
    }
  },
  in: {
    _path: token,
    _kind: "local"
  },
  lowerCase: true,
  saveStage: {
    _enum: "saveStageType",
    _value: "saveBegin"
  },
  _isCommand: false
}]

await photoshop.action.batchPlay(edit, {
  synchronousExecution: false
})
3 Likes