Copy link to clipboard
Copied
I'm very new to scripting, only able to recognize the basics at this point.
I've been browsing trough the forums looking for a script which will export single pages from my document, taking a prefix from a text box on the page with a specific paragraph style. This script is doing it pretty much as I'd like it: Script Export Single Pages with Custom Filename and Paragraph Style? Except I need it to export pdf's instead of jpegs.
This is my (failing) attempt, changing the jpg output to pdf:
if (app.documents.length != 0) {
var myDoc = app.activeDocument;
MakePdffile();
}
else{
alert("Please open a document and try again.");
}
function MakePdffile() {
for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++) {
if (myDoc.pages.item(myCounter).appliedSection.name != "") {
myDoc.pages.item(myCounter).appliedSection.name = "";
}
var myPageName = myDoc.pages.item(myCounter).name;
var myPdfPrefix = "";
var isStyleExist = true;
//Checking the existing of the paragraph style (filename)
try {
myDoc.paragraphStyles.item("title");
}
catch (myError) {
isStyleExist = false;
}
if (isStyleExist)
myPdfPrefix = getParagraphContent(myDoc.paragraphStyles.item("title"), myDoc.pages.item(myCounter)) + myPageName;
var myFilePath = "C:/Temp/" + myPdfPrefix + ".pdf";
var myFile = new File(myFilePath);
var myPdfExportPreset = app.PdfExportPresets.item("test setting v1");//Set pdf preset
myDoc.exportFile(ExportFormat.PdfType, myFile, myPdfExportPreset);
}
}
// The new function, but needs to add checking the file name rules.
function getParagraphContent (paragraphStyle, currentPage)
{
var paragraphContent = null;
for (var c = 0; c < currentPage.textFrames.length; c++)
{
for (var i = 0; i < currentPage.textFrames.item(c).paragraphs.length; i++)
{
if (currentPage.textFrames.item(c).paragraphs.item(i).appliedParagraphStyle == paragraphStyle)
{
paragraphContent = currentPage.textFrames.item(c).paragraphs.item(i).contents;
// Remove spaces and returns at the end:
paragraphContent = paragraphContent.replace(/\s+$/, '');
// Replace remaining spaces with hyphen:
paragraphContent = paragraphContent.replace(/\s+/g, '-');
// Make lowercase:
paragraphContent = paragraphContent.toLowerCase();
return paragraphContent;
}
}
}
return paragraphContent;
}
Can somebody point me out in the right direction? Many thanks in advance!
Try this code...
...// Check if document is open
if(app.documents.length >0){
// Ask for target folder
var myFolder = Folder.selectDialog("Please select a target folder...");
// Check if a target folder was selected
if (myFolder != null){
var myDoc = app.activeDocument;
var fileName = myDoc.name;
Copy link to clipboard
Copied
Ho Jojakeem,
Try this one...
// Check if document is open
if(app.documents.length >0){
// Ask for target folder
var myFolder = Folder.selectDialog("Please select a target folder...");
// Check if a target folder was selected
if (myFolder != null){
var myDoc = app.activeDocument;
var fileName = myDoc.name;
// Do for each page in active document
for(x = 0; x < myDoc.pages.length; x++){
// Get current page name
var myPageName = myDoc.pages
.name;
// Set the PDF export page range to the page name
app.pdfExportPreferences.pageRange = myPageName;
// Create new filename
var myFileName = fileName.replace(".indd", "_" + myPageName + ".pdf");
// Create new filepath
var myFilePath = myFolder.fullName + "/" + myFileName;
// Create new file object
var myFile = File(myFilePath);
// Check if PDF-file allready exists
if (myFile.exists == false){
// Export the file
myDoc.exportFile(ExportFormat.pdfType, myFile, false, "Press Quality");
}else{
alert("Error: PDF-file allready exists !");
}
}
alert("Done !");
}else{
alert("Error: Script aborted !");
}
}else{
alert("Error: No documents are opened !");
}
Copy link to clipboard
Copied
Hi Tmmls,
Thanks for the reply! I guess it is a somewhat similar script. It generates seperate pdf's and also the promt for target folde is useful.
Now if that could be combined with getting input from the 'title' paragraph styled text and use that as a file prefix, it would be it!
The eventual idea is to put a title from a data merge source in the slug of a page and let it be the prefix of a single page pdf file name. For instance 'Business-card_John-Doe.pdf'
It should be something like this bit of code to add I believe:
Copy link to clipboard
Copied
Try this code...
// Check if document is open
if(app.documents.length >0){
// Ask for target folder
var myFolder = Folder.selectDialog("Please select a target folder...");
// Check if a target folder was selected
if (myFolder != null){
var myDoc = app.activeDocument;
var fileName = myDoc.name;
// Do for each page in active document
for(x = 0; x < myDoc.pages.length; x++){
// Get current page name
var myPageName = myDoc.pages
.name;
// Set the PDF export page range to the page name
app.pdfExportPreferences.pageRange = myPageName;
// Get the text from the page
var myText = getTextFromPageUsingParagraphStyle(myDoc.pages
);
// Create new filename
var myFileName = fileName.replace(".indd", "_" + myText + ".pdf");
// Create new filepath
var myFilePath = myFolder.fullName + "/" + myFileName;
// Create new file object
var myFile = File(myFilePath);
// Check if PDF-file allready exists
if (myFile.exists == false){
// Export the file
myDoc.exportFile(ExportFormat.pdfType, myFile, false, "Press Quality");
}else{
alert("Error: PDF-file allready exists !");
}
}
alert("Done !");
}else{
alert("Error: Script aborted !");
}
}else{
alert("Error: No documents are opened !");
}
function getTextFromPageUsingParagraphStyle(myPage){
// Set the find options
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.appliedParagraphStyle = "title";
// Get all textframes from the page
var myTextframes = myPage.textFrames;
// Set the default text
var myText = "";
// Search every textframe using the find options
for (y=0; y < myTextframes.length; y++){
var mySearchResult = myTextframes
.findText(); if (mySearchResult.length > 0){
myText = mySearchResult[0].contents;
break;
}
}
return myText;
}
Copy link to clipboard
Copied
Perfect!
I'm going to study on what you've done here. This is going to be a great tool for me to build upon.
Thank you a lot!!
Copy link to clipboard
Copied
Your script seems perfect for what i'm trining to do, but I've this error :
"Object does not support the proprerty or method 'name' "
Do you undestand this error ? (I'm on Indesign cc 2021)
Thanks
Copy link to clipboard
Copied
Hi guys,
I have written something like that. Maybe will be useful.
https://drive.google.com/drive/folders/1HZpmtdt5YknRD6ITCH-k4k-nVKMZx1C7
It works in 3 independent ways for user to choose:
1) can map paragraph text with assigned paragraph style to insert this paragraph text as a pdf file name
2) you can type few letters of paragraph you'd like to use as a file name and script matching whole paragraph as a pdf file name
3) without choosing any of options above it using document file name and adding number at the end
I was using it with text on slug so I had only one paragraph but discovered few limitations:
- if you'll not use "return" sign at the end of paragraph script will cut last letter of text from this paragraph as it automatically cutting "return" sign from the end (couldn't make it work with grep)
- if you're using matching with paragraph style it's recommended to use this style only for paragraph you want to mach as a name (script taking 1st paragraph with selected paragraph style to mach name and ignoring other on the same page - first doesn't mean one on the very top. it depends of textframes creation order, will not explaining that"
- text to match can't be on locked layer.
- there's no progress bar, and if you'll think it's frozen...be patience...it's working
there's a dropdown with pdf settings - this settings are definitions from indesign so create your own pdf export preset if you'd like different than listed
additional feature is you can type range of pages to export. ex: if you have 100pages document and you'll type 3 pages per pdf it will make you a pdfs with 3 pages each and from each 1st it'll match wanted text as a file name.
pdfs will be saved in your indd file location in folder called "single_PDFs"
help yourselves...hope it will help
Copy link to clipboard
Copied
"1) can map paragraph text with assigned paragraph style to insert this paragraph text as a pdf file name"
OMG: I have been looking for this, all the while trying to modify a script that does this to JPGs - without success.
Your Google Drive link does, to my despair, not work. Would it be possible for you to re-post the this script in text form here or a link elsewere?
Many thanks in advance! : )
Copy link to clipboard
Copied
hey _Philip_,
I haven't been here for a while. If you or anyone else still need this then:
https://drive.google.com/file/d/1Hxka3tIIbjOv2cqF80cAYs7ffqvrXQr9/view?usp=sharing