Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Change scripts to run through multiple documents in a book, rather than just the open document

Explorer ,
Jan 09, 2014 Jan 09, 2014

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.

TOPICS
Scripting
4.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jan 10, 2014 Jan 10, 2014

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

...
Translate
Enthusiast ,
Jan 10, 2014 Jan 10, 2014

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 10, 2014 Jan 10, 2014

That's perfect, and is exactly what I needed. Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 11, 2014 Jan 11, 2014

@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:

BookInBooksPanel-SomeWarningsAndErrors.png

"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)

1Document1-CS5.5.inddOK
2Document2-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."))
3Document3-CS5.5.indd(new Error ("Die Anforderung konnte nicht ausgeführt werden, da die Ebene gesperrt ist."))
4Document4-CS5.5.inddCannot be seen by the Book file.

Uwe

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 12, 2014 Jun 12, 2014
LATEST

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"); 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines