[Script] Exporting PDF and JPG with custom name from Data merge
Hi,
First of all - sorry for a long thread. Secondly, I'm not (at all) well versed in coding, so I tip my hat to those who have put together the scripts (linked/mentioned below) that were the starting point of this. I've been trying to combine/modify these scripts to perform the tasks I require but I can't seem to get it to work.
What I'm trying to do: I have a multi page document that consist of about 20 pages that range from a bunch of products; double sided business card, a couple of different posters/signs and material for online marketing. These different products contain a persons name and contact details (phone number and email) along with different types of photos of the person depending on the product.
Some pages need to be exported as JPG and some as PDF using different presets.
My current solution: I'm batching/using a bunch of scripts in a folder. These are each dedicated to a number of pages. For example (PDF), if page 1-2 would be a business card then I would have a minimalistic script/file called "page 1 business card.jsx":
with(app.pdfExportPreferences){
pageRange = "1-2"
}
var myPDFExportPreset = app.pdfExportPresets.item("Business card preset")
app.activeDocument.exportFile(ExportFormat.pdfType, File("/c/temp/XXX business card.pdf"),
false, myPDFExportPreset);
For example (JPG), if page 15 needs to be exported as a high quality JPG I would save this in the batch/export script folder as "page 15 HQ jpg.jsx":
var doc = app.activeDocument;
app.jpegExportPreferences.properties = {
antiAlias: true,
embedColorProfile: true,
exportResolution: 300,
// exportingSpread: true,
jpegColorSpace: JpegColorSpaceEnum.rgb,
jpegExportRange: ExportRangeOrAllPages.exportRange,
jpegQuality: JPEGOptionsQuality.maximum,
jpegRenderingStyle: JPEGOptionsFormat.baselineEncoding,
useDocumentBleeds: false,
simulateOverprint: false,
pageString: "15"
}
var tempFile = File("/c/temp/XXX product.jpg");
doc.exportFile(ExportFormat.jpg, tempFile);
Then I would run all the scripts using this lovely script by Kasyan.
http://kasyan.ho.ua/indesign/batch_process_scripts/batch_process_scripts.html
After all the scripts have been run I would have a number of files, like this:
- XXX business card.pdf
- XXX Instagram 1.jpg
- XXX Instagram 2.jpg
- XXX Sign.pdf
- XXX Facebook.jpg
- XXX Poster.pdf
Then I would have to rename the XXX part manually to First name + Last name, ie "John Doe business card.pdf"
What I would like to do: I would like to use a script that allows me to specify the name using a paragraph style. Then I could make a non printing layer and, in this layer, create a text box in which I'd use a specific paragraph style applied to the two data merge entries "<first_name> <last name>". Then during the export, a script would find that specific paragraph style and replace the XXX.
My starting point is this script from: https://indesignsecrets.com/topic/export-pages-to-jpg-with-custom-filenames
if (app.documents.length != 0){
var myDoc = app.activeDocument;
MakeJPEGfile();
} else {
alert("Please open a document and try again.");
}
function myPS() {
try {
return myDoc.selection[0].appliedParagraphStyle;
} catch (e) {
alert("Place cursor to text with paragraph style for filenames");
exit();
}
}
function MakeJPEGfile() {
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
app.jpegExportPreferences.exportResolution = 150;
app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myPS();
var f = myDoc.findGrep();
for (var myCounter = 0; myCounter < f.length; myCounter++) {
try {
var curPage = f[myCounter].parentTextFrames[0].parentPage;
if (curPage.appliedSection.name != "") {
curPage.appliedSection.name = "";
}
var objName = f[myCounter].contents;
app.jpegExportPreferences.pageString = curPage.name;
var myFilePath = myDoc.filePath + "/" + objName + ".jpg"; //export to a folder of the current document
var myFile = new File(myFilePath);
myDoc.exportFile(ExportFormat.jpg, myFile, false);
} catch(e) {
//pasteboard?
}
I made a bastardization of this script by excluding the last part of the JPG export and inserting/modifying the simple PDF script above. And this actually works with PDF:
if (app.documents.length != 0){
var myDoc = app.activeDocument;
MakeJPEGfile();
} else {
alert("Please open a document and try again.");
}
function myPS() {
try {
return myDoc.selection[0].appliedParagraphStyle;
} catch (e) {
alert("Place cursor to text with paragraph style for filenames");
exit();
}
}
function MakeJPEGfile() {
app.jpegExportPreferences.properties = {
antiAlias: true,
embedColorProfile: true,
exportResolution: 150,
// exportingSpread: true, // Uncomment if spreads
jpegColorSpace: JpegColorSpaceEnum.rgb,
jpegExportRange: ExportRangeOrAllPages.exportRange,
jpegQuality: JPEGOptionsQuality.high,
jpegRenderingStyle: JPEGOptionsFormat.baselineEncoding,
useDocumentBleeds: false,
simulateOverprint: false,
pageString: "1-2"
}
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myPS();
var f = myDoc.findGrep();
for (var myCounter = 0; myCounter < f.length; myCounter++) {
try {
var curPage = f[myCounter].parentTextFrames[0].parentPage;
if (curPage.appliedSection.name != "") {
curPage.appliedSection.name = "";
}
var objName = f[myCounter].contents;
app.jpegExportPreferences.pageString = curPage.name;
var myFilePath = myDoc.filePath + "/" + objName + ".jpg";
var myFile = new File(myFilePath);
//myDoc.exportFile(ExportFormat.jpg, myFile, false);
with(app.pdfExportPreferences){
pageRange = ("1-2")
}
var myPDFExportPreset = app.pdfExportPresets.item("business card preset");
app.activeDocument.exportFile(ExportFormat.pdfType, File(myDoc.filePath + "/" + objName + " business card.pdf"),false, myPDFExportPreset);
} catch(e) {
//pasteboard?
}
}
}
Problem: This doesn't work with JPG as the script only exports the pages where the specified "file name" paragraph style is present, in my case the first page. So, hereby kindly requesting that someone could help out in solving the matter of exporting a specified page as JPG.
I guess I could modify this part (below) in each case to get it to link to a paragraph style that is specific to the page/product ie. Page "Facebook 1" gets a paragraph style called "Facebook 1".
app.findGrepPreferences.appliedParagraphStyle = myPS();
Ideas? Solutions? Plz halp! Thanks in advance! 🙂
