Batch process multi images with images Titles

Hello guys, I have been reading a lot in the forum and found a lot of useful info. I have this problem that I don’t seem to understand how to solve. I have this code which is not the nicest looking, to select a bunch of images from a folder and batch process them. my issue is how to get the file name or title.

here is the code:

const { app, core } = require("photoshop");
const fileTypes = require("uxp").storage;
const fs = fileTypes.localFileSystem;
const entries = await fs.getFileForOpening({
  allowMultiple: true,
  types: fileTypes.images,
});
const persistentFolder = await fs.getFolder();

for (let i = 0; i < entries.length; i++) {
  try {
    await app.open(entries[i]);
    entries.forEach((entry) => saveFileToFolder(entry));
    async function saveFileToFolder() {
      const docName = app.activeDocument[0].title;
      let newFile = await persistentFolder.createFile(docName, {
        overwrite: false,
      });
      let saveFile = await fs.createSessionToken(newFile);
      const activeDocument = app.activeDocument;
      await saveAs(saveFile); // save as with batchPlay
      app.activeDocument.closeWithoutSaving();
    }
  } catch (error) {}
}

my issue with this line with docName, if I try to get the title of the image and use it the script won’t work but if I use a string it will work but overwrite the file, I tried to wrap the docName using backticks `` but the script won’t work either.

   const docName = app.activeDocument[0].title;
      let newFile = await persistentFolder.createFile(docName, {
        overwrite: false,
      });

any help is much appreciated.
Thanks in advance

Maybe it’s an issue related to forEach + await?:

1 Like

As the reply in Simons link suggests, I personally use for() whenever possible.

Also I noticed you’re looping twice the same entries (basically for 5 entries you try to process them 25 times - each 5 times). May I ask why? Could it be the issue, that after the first loop your documents are already closed by app.activeDocument.closeWithoutSaving()?

I believe this should be enough in your for() loop:

  try {
    await app.open(entries[i]);

      const docName = app.activeDocument[0].title;
      let newFile = await persistentFolder.createFile(docName, {
        overwrite: false,
      });
      let saveFile = await fs.createSessionToken(newFile);
      const activeDocument = app.activeDocument;
      await saveAs(saveFile); // save as with batchPlay
      app.activeDocument.closeWithoutSaving();

  } catch (error) {}
1 Like

Does that work?

If not, maybe this would help:

const docName = entries[i].name;

That should give you the file name and the extension but you always drop the extension using various methods like in this thread.

I’ve tested this in a different context, so not sure if it would work with your code or not.

1 Like

Thanks for the info, honestly I am still learning Javascript and don’t know yet what’s happening behind the scene @simonhenke I took that foreach out from my code for now.

[type or paste code here](http://forums.creativeclouddeveloper.com/t/batch-process-multi-images-with-images-titles/4778/2?u=wanokuni)

@Karmalakas As the reply in Simons link suggests, I personally use for() whenever possible. Also I noticed you’re looping twice the same entries (basically for 5 entries you try to process them 25 times - each 5 times). May I ask why?
I believe this should be enough in your for() loop:

thanks for the heads I took that out and I place it with this to process all the opened entries:

try {
  await app.open(entries[i]);
  saveFileToFolder(entries[i]);

    const docName =  entries[i].name.replace(/\.[^/.]+$/, "");
    saveFileToFolder() {
    let newFile = await persistentFolder.createFile(docName, {
      overwrite: false,
    });
    let saveFile = await fs.createSessionToken(newFile);
    const activeDocument = app.activeDocument;
    await saveAs(saveFile); // save as with batchPlay
    app.activeDocument.closeWithoutSaving();
  }

} catch (error) {}

@AnthonyK const docName = entries[i].name;

Thanks this solve the problem not sure why one works and the other don’t.

now I have a new problem I don’t know what I am doing wrong :smile:
I tested the script to process and save those jpegs as PDF files but names comes wrong, could be a an order issue? doesn’t anybody have an idea :upside_down_face:

Probably because app.activeDocument is not an array. You can have only one active document, but you try to access first of the array with app.activeDocument[0]


Your saveFileToFolder() does not accept any args, but you call it with saveFileToFolder(entries[i]). Was there any intention of using the argument? And what’s the point having this funtion inside a loop? Either you should move it out or make it not as function at all.


You set docName after the function call, so it’s always one step behing. Set name before the call. Or, as suggested, ditch the func overall and leave just steps you need

1 Like

app.activeDocument[0]

ditch the func overall and leave just steps you need

Thank you so much those solutions solved the problem :smile:

This batch script allows you to select files in the folder and then run the batch on the selected files,
if I place files I would like to select the folder and process all the files inside it, as it should be done.

Check this documentation about file system, files and folder, go through this it may give you want you need: https://developer.adobe.com/photoshop/uxp/2022/uxp-api/reference-js/Modules/uxp/Persistent%20File%20Storage/Folder/#getentries

cheeers

1 Like

Wanokuni Now I’ll look at it and see if I can do what I have in mind. Thank you