I’m trying to open an image file using a persistent token so that I can open it from repeated Photoshop sessions.
First, I create and save a persistent token using the information here: https://www.adobe.io/photoshop/uxp/uxp/reference-js/Modules/uxp/Persistent%20File%20Storage/FileSystemProvider/#createpersistenttokenentry
document.getElementById("spanCHOOSEFILE").addEventListener("click", async() => {
const fs = require('uxp').storage.localFileSystem;
let entry = await fs.getFileForOpening();
let token = await fs.createPersistentToken(entry).then(result => {return result;});
console.log(token) //persistent-token/37cf4db1-7097-4551-b002-2dad2696e386
localStorage.setItem("persistent-file", token);
});
Now I want to open the file using the token and the information here: https://www.adobe.io/photoshop/uxp/uxp/reference-js/Modules/uxp/Persistent%20File%20Storage/FileSystemProvider/#getentryforpersistenttokentoken
document.getElementById("spanOPENFILE").addEventListener("click", async() => {
const fs = require('uxp').storage.localFileSystem;
let entry, contents, tries = 3, success = false;
while (tries > 0) {
try {
entry = await fs.getEntryForPersistentToken(localStorage.getItem("persistent-file"));
contents = await entry.read(); //image does not open
tries = 0;
success = true;
console.log("here") //here --so this "try" executes successfully, but but the image doesn't open
} catch (err) {
entry = await fs.getFileForOpening();
localStorage.setItem("persistent-token", await fs.createPersistentToken(entry));
tries--;
}
}
if (!success) {
// fail gracefully somehow
}
//Since image didn't open, try the token in batchPlay from Alchemist:
const token = localStorage.getItem("persistent-file")
console.log(token) //persistent-token/37cf4db1-7097-4551-b002-2dad2696e386 (matches what was saved)
await require('photoshop').action.batchPlay([
//Open file with token
{"_obj": "open","dontRecord": false,"forceNotify": true,"null": {"_path": token,"_kind": "local"},"as": {"_obj": "photoshop35Format","maximizeCompatibility": false }}
], {"synchronousExecution": false, "modalBehavior": "wait"})
.then(result => {console.log(result)}, error => console.log(error)) //Error: invalid file token used
});
Not sure what I’m doing wrong. The token seems to be passing through localStorage all right, but I’m not able to use it to actually open an image file, in this case a .jpg file.