Copy link to clipboard
Copied
I have searched and have not found the correct answer. I am banging my head with this one.
What I am trying to do is export individual pages as separate jpgs after a data merge. I need each file to have a unique name that is populated using a text frame with a data field labeled "FileName" and a style applied to it called FileName. What is happening is, I get the 3 unique files with the correct data for each. However, when it saves it is only pulling the "FileName" from page 1 for all 3 pages.
FileName.jpg
FileName2.jpg
FileName3.jpg
I need each one to have the unique FileName field for identification and distribution sorting. There will be a little over 5k files that I don't want to hand name. Below is my current script. Any help is greatly appreciated. Thank you.
var doc = app.activeDocument;
// Prompt user to select a folder to save the exported files
var mySelectedFolder = Folder.selectDialog("Select a Folder to Save Files");
if (mySelectedFolder != null) {
var fileName = getTextFromDoc(doc, "FileName"); // Get unique name for the document
if (fileName != null) {
var file = new File(mySelectedFolder + "/" + fileName + "_graphic.jpg");
// Set JPEG export preferences
app.jpegExportPreferences.exportResolution = 300; // Set resolution to 300 DPI
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH; // Set quality to High
app.jpegExportPreferences.pageString = "1-" + doc.pages.length; // Export the whole document
// Export as a single JPEG
doc.exportFile(ExportFormat.JPG, file, false);
alert("Exported: " + fileName + "_graphic.jpg");
} else {
alert("No file name found in document.");
}
alert("Done Exporting Files!");
} else {
alert("No folder selected.");
}
// Function to extract text from the document's text frames
function getTextFromDoc(doc, styleName) {
var textFrames = doc.textFrames;
for (var j = 0; j < textFrames.length; j++) {
var textFrame = textFrames[j];
// Ensure the text frame contains text with the specified style
if (textFrame.paragraphs.length > 0) {
if (textFrame.paragraphs[0].appliedParagraphStyle.name == styleName) {
return textFrame.paragraphs[0].contents.replace(/^\s+|\s+$/g, ''); // Trim spaces
}
}
}
return null; // No matching text found
}
Copy link to clipboard
Copied
Because your getTextFromDoc() function always iterate the same list of all TextFrames in the DOCUMENT.
You should iterate TextFrames on the page you want to export.
Or you can check this thread:
Copy link to clipboard
Copied
I appreciate your response. I will dive into that thread and see if I can sort it out. Definately not a coder but I figure I got this far maybe I can sort it out.
Copy link to clipboard
Copied
I made some progress but still not right. I setup a test file with only 4 pages. I am now getting the unique file name. The issue now is the script appears to be exporting the entire document for each file name vs just one page and done. I get 4 files for each file name. File 1 shows data from page 1. File 2 shows data from page 2 and so on. So instead of 4 unique pages I have 16. Hopefully that makes sense. Below is the revised code I used.
var doc = app.activeDocument;
// Prompt user to select a folder to save the exported files
var mySelectedFolder = Folder.selectDialog("Select a Folder to Save Files");
if (mySelectedFolder != null) {
for (var p = 0; p < doc.pages.length; p++) {
var page = doc.pages[p];
var fileName = getTextFromPage(page, "FileName"); // Get unique name for the page
if (fileName != null) {
var file = new File(mySelectedFolder + "/" + fileName + "_CE_FAW.jpg");
// Set JPEG export preferences
app.jpegExportPreferences.exportResolution = 300; // Set resolution to 300 DPI
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH; // Set quality to High
app.jpegExportPreferences.pageString = (p + 1).toString(); // Export the current page
// Export as a single JPEG
doc.exportFile(ExportFormat.JPG, file, false);
alert("Exported: " + fileName + "_CE_FAW.jpg");
} else {
alert("No file name found on page " + (p + 1) + ".");
}
}
alert("Done Exporting Files!");
} else {
alert("No folder selected.");
}
// Function to extract text from the page's text frames
function getTextFromPage(page, styleName) {
var textFrames = page.textFrames;
for (var j = 0; j < textFrames.length; j++) {
var textFrame = textFrames[j];
// Ensure the text frame contains text with the specified style
if (textFrame.paragraphs.length > 0) {
if (textFrame.paragraphs[0].appliedParagraphStyle.name == styleName) {
return textFrame.paragraphs[0].contents.replace(/^\s+|\s+$/g, ''); // Trim spaces
}
}
}
return null; // No matching text found
}
Feels like I am going in cirlces. Previously I had unique data and only 4 exports. The problem was it was applying the first file name to all the exported pages. The data was good, just wrong file name. If I was only exporting 100 pages I would just rename them myself and call it a day. I have to export over 5k pages. Really trying to avoid renaming all those files. I read through the thread that was suggested and can't make sense of it. Any help getting me across the finish line is greatly appreciated.
Copy link to clipboard
Copied
Your code looks OK to me?
Copy link to clipboard
Copied
More progress made. Now I am getting the file exported with the correct name. However, it is also exporting a blank page before the actual file. Not a huge deal. I can go in and delete all the blank pages. However, I would like to understand better why it is spitting out the blank page and if that can be corrected so it is only the file i need.
var doc = app.activeDocument;
// Prompt user to select a folder to save the exported files
var mySelectedFolder = Folder.selectDialog("Select a Folder to Save Files");
if (mySelectedFolder != null) {
for (var p = 0; p < doc.pages.length; p++) {
var page = doc.pages[p];
var fileName = getTextFromPage(page, "FileName"); // Get unique name for the page
if (fileName != null) {
var tempDoc = app.documents.add(); // Create a new temporary document
page.duplicate(LocationOptions.AT_END, tempDoc.pages[0]); // Duplicate the page to the new document
var file = new File(mySelectedFolder + "/" + fileName + "_CE_FAW.jpg");
// Set JPEG export preferences
app.jpegExportPreferences.exportResolution = 300; // Set resolution to 300 DPI
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH; // Set quality to High
app.jpegExportPreferences.pageString = "1"; // Export the first page of the temporary document
// Export as a single JPEG
tempDoc.exportFile(ExportFormat.JPG, file, false);
tempDoc.close(SaveOptions.NO); // Close the temporary document without saving
alert("Exported: " + fileName + "_CE_FAW.jpg");
} else {
alert("No file name found on page " + (p + 1) + ".");
}
}
alert("Done Exporting Files!");
} else {
alert("No folder selected.");
}
// Function to extract text from the page's text frames
function getTextFromPage(page, styleName) {
var textFrames = page.textFrames;
for (var j = 0; j < textFrames.length; j++) {
var textFrame = textFrames[j];
// Ensure the text frame contains text with the specified style
if (textFrame.paragraphs.length > 0) {
if (textFrame.paragraphs[0].appliedParagraphStyle.name == styleName) {
return textFrame.paragraphs[0].contents.replace(/^\s+|\s+$/g, ''); // Trim spaces
}
}
}
return null; // No matching text found
}
Copy link to clipboard
Copied
Fixed it. Here is the clean code in case anyone ever runs into this issue again. Thank you Robert for your guidance.
var doc = app.activeDocument;
// Prompt user to select a folder to save the exported files
var mySelectedFolder = Folder.selectDialog("Select a Folder to Save Files");
if (mySelectedFolder != null) {
for (var p = 0; p < doc.pages.length; p++) {
var page = doc.pages[p];
var fileName = getTextFromPage(page, "FileName"); // Get unique name for the page
if (fileName != null) {
var tempDoc = app.documents.add(); // Create a new temporary document
page.duplicate(LocationOptions.AT_END, tempDoc.pages[0]); // Duplicate the page to the new document
tempDoc.pages[0].remove(); // Remove the original blank page
var file = new File(mySelectedFolder + "/" + fileName + "_CE_FAW.jpg");
// Set JPEG export preferences
app.jpegExportPreferences.exportResolution = 300; // Set resolution to 300 DPI
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH; // Set quality to High
app.jpegExportPreferences.pageString = "1"; // Export the first page of the temporary document
// Export as a single JPEG
tempDoc.exportFile(ExportFormat.JPG, file, false);
tempDoc.close(SaveOptions.NO); // Close the temporary document without saving
alert("Exported: " + fileName + "_CE_FAW.jpg");
} else {
alert("No file name found on page " + (p + 1) + ".");
}
}
alert("Done Exporting Files!");
} else {
alert("No folder selected.");
}
// Function to extract text from the page's text frames
function getTextFromPage(page, styleName) {
var textFrames = page.textFrames;
for (var j = 0; j < textFrames.length; j++) {
var textFrame = textFrames[j];
// Ensure the text frame contains text with the specified style
if (textFrame.paragraphs.length > 0) {
if (textFrame.paragraphs[0].appliedParagraphStyle.name == styleName) {
return textFrame.paragraphs[0].contents.replace(/^\s+|\s+$/g, ''); // Trim spaces
}
}
}
return null; // No matching text found
}
Copy link to clipboard
Copied
You're welcome.
But I don't understand why you had blank pages - and why you need to create a temporary document?
The loop through Pages looked OK to me in the first version.
I'll check your code later, when I get back home.