Open all files in a folder using persistent token

can i open all files in a folder using persistent token? I created a persistent token for a folder, can I now ask to open all files in that same folder?

  const fs = require('uxp').storage.localFileSystem;
  let entry = await fs.getFolder();
  let token = fs.createPersistentToken(entry);
  localStorage.setItem("persistentFolder", token);

  const thePersistentFolderToken = localStorage.getItem("persistentFolder");
  const thePersistentFolder = await fs.getEntryForPersistentToken(thePersistentFolderToken);
  // show all files
  let files = await thePersistentFolder.getEntries();
  let filesArray = [];
  for (let i = 0; i < files.length; i++) {
    filesArray.push(files[i].name);
  }
  app.open = filesArray;

Can someone help me?

You just need to open each file individually within your loop rather than pushing to an array and passing that to app.open().
You’ll need to use .getEntry() and pass the returned value to app.open(). You’ll also need to use executeAsModal to perform the file open.
See here for more detail.

1 Like

Right, but how do I use a token already obtained in a persistent token inside .getEntry() ? This is my biggest doubt…

I already got the token, now to get this getEntry() would I need some formula?

Sorry, I was being dumb, you don’t need to use getEntry() because you already did with getEntries() :upside_down_face:

You have multiple await calls but they’re not wrapped in an async function and so won’t work. You want something more like this (I changed the for loop to a nicer for…of loop and changed the let declarations to const):

const { app, core } = require("photoshop");
const fs = require('uxp').storage.localFileSystem;

const openFiles = async () => {
  await core.executeAsModal( async () => {
    const entry = await fs.getFolder();
    const token = await fs.createPersistentToken(entry);
    localStorage.setItem("persistentFolder", token);
    
    const thePersistentFolderToken = localStorage.getItem("persistentFolder");
    const thePersistentFolder = await fs.getEntryForPersistentToken(thePersistentFolderToken);
    // show all files
    const files = await thePersistentFolder.getEntries();
  
    for (const file of files) {
      await app.open(file)
    }    
  })
}

openFiles();
1 Like

thanks!!! this is what i needed!!!

1 Like

My pleasure, glad to help!

1 Like