Save Batch Images with Dynamic Name in Photoshop UXP

HI everyone, its be 2 days i am banging my head with this, kindly help me in this

  1. I want to save the files in a batch while the output folder is given by User and name of the files are going to be dynamic with each save.

  2. I know that i have to provide a session token to save the files on user given location.

  3. The problem is that user folder object is fixed but file name is changing, how i can cast these two different in a single sessionTokken and call the save method.

  4. for the record i have tried each example given in this community for jpeg save and also tried alchemist method.

you don’t need tokens to use save as, you just need an entry.
however changing between the two is no issue.

lets say you have an entry to a folder by one of two ways:

  1. user picked:
let myEntry = await fs.getFolder(fs.supportedDomains[0]);
  1. -OR- using url without user interaction:
let myEntry = await fs.getEntryWithUrl("file:\\D:\\myFolder");

now you can use that entry to create files

newFileName = "whateverName.jpg";
var myFile = await myEntry.createFile(newFileName, { overwrite: true });
let jpegOptions = {
    quality: 100,
    embedColorProfile: true
}
app.activeDocument.saveAs.jpg(myFile, jpegOptions, true);
2 Likes

thankyou so much, i will try this solution
just a small favor , i have two question

  1. can you please share some insight in short words , that what is the difference between entry and token?
  2. should i use persistent session for this type of save
    Regards

Entry is an object, it is generated by file/folder pickers (among others) or getEntryWithUrl(url) and used with (save, open, read, write… etc) aka normal functions

Tokens area strings generated from entries (for session or persistent) and used in batchPlay action descriptors in place of _path

  1. should i use persistent session for this type of save
    Regards

depends on your design choices

  1. you can request an entry every time
  2. request once, create token, store token, restore when needed
  3. add fullAccess permissions in manifest and generate entry from url if you know the path or have it selected/stored
1 Like

@Maher you are a great citizen, humble and nice, thanks for being here in this community, you brought us the best explanation so far on how to use the long awaited feature “fs.getEntryWithUrl(“xxx”)”
Now we can even save the active document in a subfolder inside the main folder directly. Thanks for sharing.

  try {
      const path = app.activeDocument.path.replace(/\\[^\\]*$/, "")
      const Entrada = await fs.getEntryWithUrl(`file:${path}`); 

      const entries = await Entrada.getEntries();   
      let subfolder = entries.filter((entry) => entry.isFolder).find((subFolder) => subFolder.name === "SubFolder");
      if (!subfolder) {subfolder = await Entrada.createFolder("SubFolder");}
      
      newFileName = app.activeDocument.name;
      var myFile = await subfolder.createFile(newFileName, { overwrite: true });
      let jpegOptions = {
          quality: 100,
          embedColorProfile: true
      }
      app.activeDocument.saveAs.jpg(myFile, jpegOptions, true);
  } catch (err) {alert(err)}
1 Like

@PS-fxrios Thankyou sir for adding more detail, @Maher 's quick reply and effective solution was really helpful.

@PS-fxrios about your solution , i have a question since i have been using UXP , try catch is not working properly, code does not handle error like it should handle with normal try catch, it did prompt the error to photoshop. Can you please guide about this,
about your code i have a ques