Copy link to clipboard
Copied
I have a number of scripts that I run, that run through entire docments. But to apply their changes to an entire book I have to open up each section (or document) in the book and then run the script, save my changes, close the section and open the next. Is there a way that I can edit my scripts so that they traverse every document in a book, where I just open the book, run my script and it applies the changes to every section, one section at a time?
I would like to avoid a scenario where I'd have to open every section because this book is rather large and would slow down my computer/network if I opened the entire thing all in one go.
Thank you in advance for any and all help.
1 Correct answer
Hi Soteriologist
You can loop through the documents in a book like you can with every collection. When you loop trough the book you can open as many ducuments as you like and then close the document when you want. In your case mybe you open a document, work an it and close the document before you open the next.
Give this a try:
*******************************************************
...main();
exit();
function main() {
var myBook = app.activeBook, myDoc;
var myDocs = myBook.bookContents.everyItem
Copy link to clipboard
Copied
Hi Soteriologist
You can loop through the documents in a book like you can with every collection. When you loop trough the book you can open as many ducuments as you like and then close the document when you want. In your case mybe you open a document, work an it and close the document before you open the next.
Give this a try:
*******************************************************
main();
exit();
function main() {
var myBook = app.activeBook, myDoc;
var myDocs = myBook.bookContents.everyItem().getElements();
for (var i=0; i< myDocs.length; i++) {
// must be adapted to your needs
myDoc = app.open(File("/Users…/" + myDocs.name));
// do something
myDoc.close();
}
}
*******************************************************
Kind Regards
Dani (from Switzerland)
Copy link to clipboard
Copied
That's perfect, and is exactly what I needed. Thank you!
Copy link to clipboard
Copied
@Soteriologist – depending on your needs and conditions of the documents inside the book, you'll need a more robust function.
Some question one could ask:
1. Is the document available (= can the book see the file)?
2. Can it be opened at all?
3. What to do if the scripts' action on the opened file will fail?
Instead of document "name" you could use "fullName".
You can set the userInteractionLevel to NEVER_INTERACT while running a script on the document.
Do something like a log function of possible errors.
Here an example:
"Document1" is ok, "Document2" cannot be opened (it's a zip file of the original document where the suffix ".zip" in the file name was deleted), "Document4" is just renamed after adding the file to the book.
And: the active layer of "Document3" is locked.
Code:
//Setting the userInteractionLevel to NEVER_INTERACT
//is the best bet to let the script work unattended
var userInterAct = app.scriptPreferences.userInteractionLevel;
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
var myBook = app.activeBook;
var myBookContents = myBook.bookContents.everyItem().getElements();
var counter = 1;
for(var n=0;n<myBookContents.length;n++){
//Using full name of document:
var myPath = myBookContents
.fullName; var myFile = File(myPath);
//Test, if file does not exist, that is: if the book cannot see the file.
//(document was renamed or put somewhere else after it was added to the book)
if(!myFile.exists){
$.writeln(counter+"\t"+myFile.name+"\t"+"Cannot be seen by the Book file.");
++counter;
continue;
};
//File is not accessible
try{
var myDoc = app.open(myFile);
}catch(e){
$.writeln(counter+"\t"+myFile.name+"\t"+e.toSource());
++counter;
continue;
};
//DOING SOMETHING
//Your function call here
doingSomethingOn(myDoc);
myDoc.save(undefined,undefined,undefined,true);
myDoc.close();
++counter;
};
//Setting back the original value of userInteractionLevel
app.scriptPreferences.userInteractionLevel = userInterAct;
//Function doing something
function doingSomethingOn(myDoc){
//Will throw error, if:
//the active layer is locked
try{
myDoc.rectangles.add().remove();
}catch(e){
$.writeln(counter+"\t"+myDoc.name+"\t"+e.toSource());
return;
};
$.writeln(counter+"\t"+myDoc.name+"\t"+"OK");
return;
};
exit();
Log for this example in the console of my ESTK:
(in German)
1 | Document1-CS5.5.indd | OK |
2 | Document2-CS5.5.indd | (new Error ("Die Datei \"Document2-CS5.5.indd\" kann nicht geöffnet werden. Das Dateiformat wird von Adobe InDesign nicht unterstützt, ein für die Unterstützung notwendiges Zusatzmodul fehlt oder die Datei ist bereits in einer anderen Anwendung geöffnet.")) |
3 | Document3-CS5.5.indd | (new Error ("Die Anforderung konnte nicht ausgeführt werden, da die Ebene gesperrt ist.")) |
4 | Document4-CS5.5.indd | Cannot be seen by the Book file. |
Uwe
Copy link to clipboard
Copied
Hi, I'm needing a similar script to help set the page numbering options to automatic for all pages in an InDesign book file. I've tried to combine what I found in this thread and what I found in another thread on a script to change page numbering on any open files, I've come up with the following, However I'm receiving errors and not sure what I need to do to correct the script since I don't know javascript. Could someone help set me straight? Thanks so much!
main();
exit();
function main() {
var myBook = app.activeBook, myDoc;
var myDocs = myBook.bookContents.everyItem().getElements();
for (var i=0; i< myDocs.length; i++) {
myDoc = app.open(+ myDocs.name));
myDoc[doc].sections[0].continueNumbering = true;
}
myDoc.close();
}
alert("Done");

