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.
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) {}
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) {}
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
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
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
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.