Open an image file using a persistent token

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.

Wondering if I need to somehow specify the file format in the

contents = await entry.read();

line since it says:

If a format is not supplied, the file is assumed to be a text file using UTF8 encoding.

Here: https://www.adobe.io/photoshop/uxp/uxp/reference-js/Modules/uxp/Persistent%20File%20Storage/File/#readoptions

If so, what’s the proper format for jpg, psd, png, etc?

Entry.read() is for getting the text content of a file, it won’t trigger any Photoshop operation like opening it.

You could try app.open(entry): https://www.adobe.io/photoshop/uxp/ps_reference/classes/photoshop/#open

However, the batchPlay looks fine, too, so I’m wondering why it doesn’t work. The error tells that there’s definitely something wrong with the token.

I haven’t used persistent tokens yet, but maybe you could try to:

  1. get entry for the persistent token
  2. create session token
  3. open with the session token

That doesn’t really make sense I assume but you could check if the persistent token is the problem.

Also, in the batchPlay you can remove the part
"as": {"_obj": "photoshop35Format","maximizeCompatibility": false } to make Photoshop use the default settings for any given file format. (Removing one more source for errors)

You got it right twice, @simonhenke!

app.open(entry)

works and so does

without the "as": {"_obj": "photoshop35Format","maximizeCompatibility": false }

I had tried the session token, but removing

"as": {"_obj": "photoshop35Format","maximizeCompatibility": false }

appears to be necessary.

Thank you.