How to create a token and use it in the "path" key to export a GIF?

I’m trying to export my PSD as a GIF using File → Export → SAveForWeb… using Alchemist I get the correct code. My problem is when creating a path to export it. // And the code is long but I just need the “_path” key to work.

// The code is long but I just need the "_path" key to work.
{
   "_obj": "export",
   "using": {
      "_obj": "SaveForWeb",
      "$Op": {
         "_enum": "$SWOp",
         "_value": "$OpSa"
      },
      "$DIDr": true,
      "in": {
         "_path": path,
         "_kind": "local"
      },
      "format": {
         "_enum": "$IRFm",
         "_value": "$GIFf"
      }
// Long code.............I only need the "path" key to work.
}


My question here is: How can I create a ‘path’ for that command? I get to ask the user to get a folder before this process, how I can store that path to use it later for this export command?

Thanks!

Not really sure what you are asking here, how to store the path from a file picker for repeated use, or how to assign a path to that path variable to that code example.

1 Like

Howdy! The ‘path’ argument to pass in the descriptor here unfortunately has a bit of a misleading name. What you need to pass here is a file entry token as described in the UXP docs here and not a file path. There are lots of paradigms how to obtain an entry, e.g. by asking the user to pick a folder/file via getFileForOpening or getFileForSaving (depending on whether you want to read or write a file). You need to make sure to have the right permissions specified in the manifest file. People have actually asked ad nauseam about the different ways to read files from disk in this forum - practically on a weekly basis - so it should be easy to find lots of details about it :grin:

2 Likes

Here is an older post where I mentioned how I was doing it. Where you have “path”, you would put “saveFile” from my example

Ok I tried your suggestion guys and it worked amazing now. Not created a file session token but created a sessionToken for folder item entry only.


// I get users folder manually
const getUserFolder = await fs.getFolder();

// Create a presistent token for the user chosen folder
const token = await fs.createPersistentToken(getUserFolder);

// Store the token in localStorage or whatever this code does
localStorage.setItem("persistent-token", token);

// Get the persistent-token as an item
const item  = localStorage.getItem("persistent-token");

// Get an entry from the persistent token converted into an item
const entry = await fs.getEntryForPersistentToken(item));

// Create a sessionToekn from that entry
const sessionToken = await fs.createSessionToken(entry);


// Use the sessionToken as the "path" in the exportGIF() function;
await exportGIF(sessionToken);

// Function to export an image as GIF.
async function exportGIF(MY_PATH) {

  await batchPlay([
  {
     "_obj": "export",
     "using": {
        "_obj": "SaveForWeb",
        "$DIDr": true,
        "in": {
           "_path": MY_PATH,
           "_kind": "local"
        },
        "format": {
           "_enum": "$IRFm",
           "_value": "$GIFf"
        }
  // Long code.............I only need the "path" key to work.
  }
  ], {});
}

I’m happy I managed to get it to work but this is so complicated hahahaha, we should work together in a solution to make file handling easier for other developers that haven’t understand this well. As feedback for the Adobe developers, this is kind of difficult to understand. I mean once is understood is simple, but that curve can be a little bit step and confusing haha. I know it has its reasons to be this way but I believe we can always make things easier, probably I’ll make a tutorial or a function

Anyway, thank you very much again for your help guys, I really love this forum and the speed of help I can receive and give to others here!

Much love!