Copy link to clipboard
Copied
Can someone please help with the script below??
What I have is a multi page InDesign doc with the 'UAVC-IHD' paragraph style applied on the odd # pages, the style is used to rename the pdfs on export.
I'am trying to export pages 1-2, 3-4, 5-6 7-8, 9-10, and so on...............as separate two page pdfs twice, each named slightly different one for the hi res and one for the low res output to specific folders within a project.
The end result now is each set of pdf files are exported named correctly based on the paragraph style/suffix and placed in the correct folders, but are only single page pdfs (the odd# page) and i'am also getting extra pdfs of the last odd# page named "null".
I need to be able to export the odd/even pages as a two page pdf, named by the applied paragraph style.
Thanks in advance!
for(var p = 0; p < app.documents[0].pages.length; p++) {
var frames = app.documents[0].pages
.textFrames;
var pdf_name = null;
for(var i = 0; i < frames.length; i++) {
if(frames.paragraphs[0].appliedParagraphStyle.name == 'UAVC-IHD') {
pdf_name = frames.paragraphs[0].contents;
break;
}
}
if(pdf_name != null) {
app.pdfExportPreferences.pageRange = app.documents[1-2].pages
.name;
var export_preset1 = app.pdfExportPresets.item("[High Quality Print]");
var export_preset2 = app.pdfExportPresets.item("[Smallest File Size]");
var myFolder = app.activeDocument.filePath.parent + "/Final_Deliverables/";
var myDoc = app.activeDocument;
}
myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Publish/' + pdf_name + "HR.pdf"), false, export_preset1);
myDoc.exportFile(ExportFormat.PDF_TYPE, File(myFolder+'/Proofs/' + pdf_name + "-00.pdf"), false, export_preset2);
}
alert("Done Exporting Pdf's!");
Hi Mike,
A quick look at your code:
app.documents[1-2]
defaults to:
app.documents[-1]
that refers to the last document in your open documents:
app.documents.everyItem()
It's only the active document, if you have one document open.
Is that intended?
For the problem with looping, if I understood correctly what you want:
Loop through every other page:
for(var p = 0; p < app.documents[0].pages.length; p=p+2)
Define the page range to export best in absolute values:
...app.pdfExportPreferences.pageRange =
"+"+( app.d
Copy link to clipboard
Copied
Hi Mike,
A quick look at your code:
app.documents[1-2]
defaults to:
app.documents[-1]
that refers to the last document in your open documents:
app.documents.everyItem()
It's only the active document, if you have one document open.
Is that intended?
For the problem with looping, if I understood correctly what you want:
Loop through every other page:
for(var p = 0; p < app.documents[0].pages.length; p=p+2)
Define the page range to export best in absolute values:
app.pdfExportPreferences.pageRange =
"+"+( app.documents[1-2].pages
.documentOffset+1 ) + "," +
"+"+( app.documents[1-2].pages[p+1].documentOffset+1 );
The result for the first two pages would be:
"+1,+2"
In case you have an odd number of pages
starting on a right hand page side you have to check if:
p+1
is less than
app.documents[0].pages.length-1
Regards,
Uwe
Copy link to clipboard
Copied
FWIW: You can refer to the active document with:
app.activeDocument
or:
app.documents[0]
The difference:
activeDocument cannot refer to windowless documents.
E.g. when used with InDesign Server.
Or if you add a document with method add() of the documents class and set its first argument to false.
Regards,
Uwe
Copy link to clipboard
Copied
That's correct, it's intended to run only on one opened document.
I modified the code with your inputs for the looping and it's working perfectly!!!!
Thank you very much!!!!
Copy link to clipboard
Copied
Uwe,
I've been trying to make a slight change for a additional format but I cant seem to be able to figure it out.
I would like to be able to loop through every set of four pages and export........1-4, 5-8, 7-12, 13-16, 17-20.........and so on.
Can you please help with this?
Thanks in advance!!!
Copy link to clipboard
Copied
Hi Mike,
a small correction for answer 1.
I hope you spot the important difference:
At the start of the script declare a variable for the active document you want to run the script at:
var doc = app.activeDocument;
Then use that variable whenever you want to refer to the active document.
Like when you want to define the value for pageRange which is a String in the for-loop that goes through the pages:
app.pdfExportPreferences.pageRange =
"+"+( doc.pages
.documentOffset+1 ) + "," +
"+"+( doc.pages[p+1].documentOffset+1 );
FWIW: In a more abstract sense, the purpose of that loop is to generate strings.
It can be written without addressing a page at all.
The only thing we need is the number of pages in a document:
var pageLength = doc.pages.length;
This forum is about scripting for scripters. Code samples are discussed, trying to make things work if they are not working.
To change code from a working sample means to understand some basic things how to write code and how things in InDesign are represented in code. But you cannot expect that someone is doing all your work for you.
If you want to learn ExtendScript (JavaScript) for InDesign to get some basic understanding what's going on by reading code, doing some small changes to code or even writing your own code, here some links to get started:
If you want a reference for all available stuff in nearly every version of InDesign see links below.
Compiled by Jongware:
Compiled by Gregor Fellenz:
InDesign ExtendScript API (12.0)
Regards,
Uwe
Copy link to clipboard
Copied
I give you an example how loops are working. Also do a search on the net. Read on what loops are doing. How for-loops are constructed.
What a string is, what an array is and what mathematical operators are and how they work.
Example:
An array is a structure that can store elements. It can be defined enclosing elements in square brackets separated by commas.
Here we have an array of three elements, three individual strings "A", "B" and "C":
var a =
[
"A",
"B",
"C"
];
If you want to alert every element of the array you could do it by using the index of the element in that array:
var a = ["A","B","C"];
var firstElement = a[0];
alert(firstElement);
var secondElement = a[1];
alert(secondElement);
var thirdElement = a[2];
alert(thirdElement);
Counting begins with number 0 in JavaScript.
Note, that an array has a length property.
Now imagine an array of more elements than three. Or a collection of pages of your document.
To make that easier we can do a loop through all the elements using e.g. a for-loop.
We use the value of a.length that is 3 with this example to delimit the number of iterations the loop is doing.
Or all index numbers of the positions of all pages in an InDesign document stored in an array:
var a = app.activeDocument.pages.everyItem().documentOffset;
var allIndexNumbers = a.length;
Back to our little example:
We start with 0, we stop if n < a.length which is 3 is not true anymore, we do baby steps and iterate every integer number from 0 on.
Then we stop. 2 < 3, that's true, but the next step, 3 < 3 is false. And 3 is the length of the array.
var a = ["A","B","C"];
for(var n = 0; n < a.length; n = n+1)
{
alert(a
); };
Just fire up the ESTK (ExtendScript Toolkit app) that is installed with every install of InDesign.
To work with the following code you need not to connect the ESTK with InDesign.
Just watch what the JavaScript Console will show when running the code.
To output something to the console we need method $.writeln() . That's more convenient that memorizing alerts.
Do we need an array we could loop through to get output?
No. We just need numbers for the start, the stop condition and the iteration steps:
// Example 1:
for(var n=0;n<13;n=n+1)
{
// Just show the numbers the loop is doing in the Console:
$.writeln(n);
};
// Needed to prevent any output to the Console at the end of a script that returns something
exit();
Output can be expected as:
0
1
2
…
12
Now we are doing some changes.
1. We introduce a counter.
2. We make more clear what the step mechanism does in a for-loop.
For that we declare one variable each and work with the values of that variables.
// Example 2:
// Assign the value 2 to a variable named steps:
var steps = 2;
// Assign the value 1 to a variable named counter:
var counter = 1;
// Here I substituded the last number in the third part
// of the control structure of the loop with the variable:for(var n=0; n<13; n=n+steps )
{
//Output will be the word "loop: " plus the value of counter:
$.writeln("loop:"+counter);
//Just write the value of n to the console:
$.writeln(n);
//Build a sum with value n and number 1 and write that to the console:
$.writeln(n+1);
//Build a sum with value of n and the value of steps and write that to the console:
$.writeln(n+steps);
// Changing the counter for every loop that ran.
// Count it up by 1:
counter = counter+1;
};
// Needed to prevent any output to the Console
exit();
Just test if you do changes to the initial value of variable steps. Use 3 as value. Use 4 as value.
Substitute the stop condition, that 13, with the number of pages in your document:
var doc = app.activeDocument;
var numOfPages = doc.pages.length;
Note:
You can concatenate things with the + operator. But you also can build a sum with the same + operator.
Concatenating numbers to get a string:
Adding two items to an empty string will result in a string. Item one is a number, item 2 is a number.
""+1+2 => "12"
Building a sum first and then do a string of it.
JavaScript will first do the mathematical operation and then conactenates the number to the empty string if the operation is enclosed in round brackets:
""+(1+2) => "3"
$.writeln( something );
is a method to write strings to the JavaScript Console of the ESTK.
But beware, that $.writeln() is clever.
It will take everything that is defined and writes a string of it to the console.
Example:
$.writeln(app.activeDocument);
will return "[object Document]"
$.writeln(1+1);
will return "2". It will do the mathematical operation first and then writes a string to the console.
Hope, that helps you getting started.
Regards,
Uwe
PS: If that all is too much work for you changing your script that is working with 2 pages of each export to a script that is working with 4 pages of each export you could always hire a script writer with experience InDesign scripting.