Copy link to clipboard
Copied
I'm trying to batch process naming exported png files from InDesign. I don't want to do using a paragraph style, I want to feed Indesign a list using a script e.g.
I have 5 designs in one document:
1. I love blue
2. I love red
3. I love green
4. I love yellow
5. I love pink
I have a text list
blue
red
green
yellow
pink
I want the file names to be
blue.png
red.png
green.png
yellow.png
pink.png
I've tried using chaptgpt but I keep getting stuck in loops where it's producing pngs but it produces 25 of them! It's stuck in a weird sort of loop and files look like this:
blue.png with deisgn I love blue
blue.2png with deisgn I love red
blue3.png with deisgn I love green
blue4.png with deisgn I love yellow
blue5.png with deisgn I love pink
red.png with deisgn I love blue
red2.png with deisgn I love red
red3.png with deisgn I love green
red4.png with deisgn I love yellow
red5.png with deisgn I love pink
green.png with deisgn I love blue
green2.png with deisgn I love red
green3.png with deisgn I love green
green4.png with deisgn I love yellow
green5.png with deisgn I love pink
yellow.png with deisgn I love blue
yellow2.png with deisgn I love red
yellow3.png with deisgn I love green
yellow4.png with deisgn I love yellow
yellow5.png with deisgn I love pink
pink.png with deisgn I love blue
pink2.png with deisgn I love red
pink3.png with deisgn I love green
pink4.png with deisgn I love yellow
pink5.png with deisgn I love pink
Can anyone help me? Is there a script that already exists that I can use?
Script below as .js not supported file type for upload in this forum
// Function to read names from a text file
function readNamesFromFile(filePath) {
var file = new File(filePath);
var names = [];
if (file.exists) {
file.open("r");
while (!file.eof) {
var line = file.readln();
if (line.length > 0) {
names.push(line.replace(/^\s+|\s+$/g, '')); // Trim leading and trailing whitespace
}
}
file.close();
} else {
alert("The file does not exist.");
exit();
}
return names;
}
// Ensure the document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
// Prompt the user to select a text file containing the list of names
var file = File.openDialog("Select a text file with the list of names");
if (file === null) {
alert("No file was selected. The script will now exit.");
exit();
}
var names = readNamesFromFile(file);
// Ensure the document has the right number of pages
if (doc.pages.length != names.length) {
alert("The number of pages in the document does not match the number of names provided.");
exit();
}
// Prompt the user to select a destination folder
var destinationFolder = Folder.selectDialog("Select a folder to save the PNG files");
if (destinationFolder === null) {
alert("No folder was selected. The script will now exit.");
exit();
}
// Debugging: Log the number of pages and names
$.writeln("Number of pages: " + doc.pages.length);
$.writeln("Number of names: " + names.length);
// Loop through each page and export it as PNG
for (var i = 0; i < doc.pages.length; i++) {
var page = doc.pages[i];
var fileName = names[i] + ".png";
var filePath = new File(destinationFolder + "/" + fileName);
// Debugging: Log the page number and filename being exported
$.writeln("Exporting page " + (i + 1) + " as " + fileName);
try {
// Set PNG export preferences
app.pngExportPreferences.pngQuality = PNGQualityEnum.MAXIMUM;
app.pngExportPreferences.exportResolution = 300;
app.pngExportPreferences.pngColorSpace = PNGColorSpaceEnum.RGB;
app.pngExportPreferences.pageString = page.name;
// Export the page as PNG
doc.exportFile(ExportFormat.PNG_FORMAT, filePath);
} catch (e) {
alert("Error exporting page " + (i + 1) + ": " + e.message);
exit();
}
}
alert("Export completed successfully.");
} else {
alert("No document is open.");
}
Copy link to clipboard
Copied
Don't see anything on its face that could be causing that. What does the txt file look like? How many pages are in the InDesign doc? What do your $.writelns indicate?
Copy link to clipboard
Copied