13 Replies Latest reply on Jun 29, 2009 10:54 AM by Dave Saunders

    Is it possible to run a script on an entire book file?

    marykulp

      Hi everyone.

       

      Loving the scripting stuff. Jus wondering, is there a way to tell a script to run on an entire indb file?

       

      Thanks,

      Mary

        • 1. Re: Is it possible to run a script on an entire book file?
          Dave Saunders Level 4

          Some operations can address a book while for others you need to open each document that is associated with the book -- you do that using the book's bookContents property. Here's a script I recently wrote that exports a PDF of a book. But before doing it, it processes all the documents in the book either showing or hiding a layer (the teacher's annotations layer) in each document:

           

          //DESCRIPTION: Export PDF of Student or Teacher book

           

          (function() {
              if (app.books.length !== 1) {
                  alert("Please have open exactly one book document.");
                  return;
              }
              var book = app.books[0];
              var name = book.name;
              if (name.indexOf("Student") !== 0 && name.indexOf("Teacher") !== 0) {
                  alert("Please open a Student or Teacher book.");
                  return;
              }
              var teachVis = name.indexOf("Student") === 0
                  ? false
                  : true;
              var bookComps = book.bookContents
              for (var j = 0; bookComps.length > j; j++) {
                  myDoc = app.open(bookComps[j].fullName);
                  myDoc.layers.item("Teacher").visible = teachVis;
              }
              var pdfPref = app.pdfExportPresets.item("Client Proof");
              var pdfFile = File(book.filePath + "/" + book.name.replace(/\.indb/,".pdf"));
              if (bookComps.length === 1) {
                  bookComps = [bookComps];
              }
              book.exportFile(pdfFile, false, pdfPref, bookComps);
          }())

           

          Of course the names "Student" and "Teacher" are conventions that I imposed on the project so that this script could do its thing. The job in question has 22 chapters and there are seven document files associated with each chapter, so imposing some order made the job manageable.

           

          Dave

          1 person found this helpful
          • 2. Re: Is it possible to run a script on an entire book file?
            marykulp Level 1

            thanks Dave for your prompt reply. I've just been adding lines and crossing my fingers. maybe some this will help.

             

            I'm fairly new to scripting but have muddled together a few simple scripts that do work (to my suprise ;o).

             

            I'd like to add a simple command line to the "label graphics" script that comes precanned (to run it on my indb file), and I have another one that ungroups everything and puts all of the text on it's own layer that I believe could be applied to the whole book as well.

             

            Thanks again,

            Mary

            • 3. Re: Is it possible to run a script on an entire book file?
              marykulp Level 1

              looking at this again...

               

              so I have:

               

              function main(){
                  if(app.documents.length != 0){
                       if(app.documents.item(0).allGraphics.length != 0){
                           myDisplayDialog();
                       }

               

              I would just make it:

               

              function main(){
                  if(app.books.length != 1){
                       if(app.documents.item(0).allGraphics.length != 0){
                           myDisplayDialog();
                       }

               

              or something similar... I will try it out.

               

              And with applescript instead of

               

              tell document 1

               

              I'd say

               

              tell book 1

               

              ?

              • 4. Re: Is it possible to run a script on an entire book file?
                Harbs. Level 6

                Yes.

                 

                Look up the Book object in the OMV.

                 

                Harbs

                • 5. Re: Is it possible to run a script on an entire book file?
                  marykulp Level 1

                  Thanks.

                   

                  I've looked this up, still trying to figure out proper syntax. I'm sure you guys are shaking your heads.

                   

                  I'm using javascript- I guess I am adding in the book object?

                   

                  i.e.

                   

                  function myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myLabelStyleName){
                  var myBook = app.activeBook(0);   
                      var myDocument = app.documents.item(0);
                      var myGraphics = myDocument.allGraphics;
                      myLabelStyle = myDocument.paragraphStyles.item(myLabelStyleName);
                      for(var myCounter = 0; myCounter < myGraphics.length; myCounter++){
                          myAddLabel(myDocument, myGraphics[myCounter], myLabelType, myLabelHeight, myLabelOffset, myLabelStyle);
                      }
                  }
                  function myAddLabel(myBook, myDocument, myGraphic, myLabelType, myLabelHeight, myLabelOffset, myLabelStyle){
                      var myLabel;
                      var myLink = myGraphic.itemLink;
                      //Create the label layer if it does not already exist.
                      var myLabelLayer = myDocument.layers.item("labels");
                      try{
                           myLabelLayer.name;
                      }
                       catch (myError){
                            myLabelLayer = myDocument.layers.add({name:"labels"});
                      }

                   

                   

                  sorry for having you guys revisit preschool- any help would be appreciated though...

                  • 6. Re: Is it possible to run a script on an entire book file?
                    marykulp Level 1

                    my applescript is returning a "can't make class layer" error...

                     

                    tell application "Adobe InDesign CS4"
                        activate
                        tell book "Book 1.indb"
                            tell document 1
                                try
                                    set grouped_items to every group
                                    repeat with ungroup_repeater from 1 to count grouped_items
                                        ungroup grouped_items
                                    end repeat
                                on error
                                    --no grouped items found
                                end try
                               
                                set Text_Layer to make layer
                                try
                                    set name of Text_Layer to "live copy"
                                on error
                                    --name already exists
                                end try
                               
                               
                               
                                try
                                    set text_frames to every text frame
                                    repeat with textframe_repeater from 1 to count text_frames
                                        move text_frames to layer "live copy"
                                    end repeat
                                   
                                end try
                               
                                try
                                    set text_paths to every text path
                                    repeat with text_path_repeater from 1 to count text_paths
                                        move text_paths to layer "live copy"
                                    end repeat
                                end try
                            end tell
                        end tell
                    end tell

                     

                     

                    thoughts?

                    • 7. Re: Is it possible to run a script on an entire book file?
                      sstanleyau Level 4

                      You have a couple of problems there. First, you should just be addressing a document, not a book. You can also ungroup a little more easily, to include any groups within groups. And this is probably a better way to handle making your layer. Finally, you can't "move" page items to layers; you have to set their item layer property. Try something like this:

                       

                      tell application "Adobe InDesign CS3"

                      activate

                      tell document 1

                      try

                      ungroup (every item of all page items whose class is group)

                      on error

                      --no grouped items found

                      end try

                       

                      if not (exists layer "live copy") then

                      make layer with properties {name:"live copy"}

                      end if

                      try

                      set item layer of text frames to layer "live copy"

                      end try

                       

                      try

                      set item layer of text paths to layer "live copy"

                      end try

                      end tell

                      end tell

                      --

                      Shane Stanley <sstanley@myriad-com.com.au>

                      AppleScript Pro Sessions <http://scriptingmatters.com/aspro>

                      1 person found this helpful
                      • 8. Re: Is it possible to run a script on an entire book file?
                        marykulp Level 1

                        Hi there Stan.

                         

                        Thanks.

                         

                        -- but guess I should have put my "before" script in there -- It works like a charm (though I'll certainly take your technical notes, thank you).

                         

                        Problem is, I want to apply it to my entire book, but can't.

                         

                        Any ideas on that?

                         

                        Again, thanks very much.

                         

                        Mary

                        • 9. Re: Is it possible to run a script on an entire book file?
                          sstanleyau Level 4

                          To apply it to the whole book, you need to get a list of the documents in the book (the 'book content" elements), and then loop through them, opening them, doing the stuff, saving them, and closing them.

                           

                          --

                          Shane Stanley <sstanley@myriad-com.com.au>

                          AppleScript Pro Sessions <http://scriptingmatters.com/aspro>

                          • 10. Re: Is it possible to run a script on an entire book file?
                            marykulp Level 1

                            hmm. see I have an indb file... with 20 sections or so. All of my googling and such and the index OMV lead me to believe that I can insert some code that will allow me to apply the script to the group of files as listed in the indb... I will have to handle them all individually too; however labeling images and separatng out the text prior to having to open them all would be sweet... A girl can dream can't she?

                             

                            Thanks again for your input Stan!

                            • 11. Re: Is it possible to run a script on an entire book file?
                              Martin Fischer Level 2

                              Hi marykulp,

                               

                              if you have script and you have a book you can open each doc of the book and run the script.

                               

                              By script you can automate opening the docs of the book and call a script which is designed to do something with the active document by doScript().

                               

                              Take a look at the script doScriptWithDocsOfBooks.jsx.

                              This could be of some help for you.

                              You have to select one script and than the script 'doScriptWithDocsOfBooks.jsx' will open every document of every open book in loops and run the selected script.

                              Be sure that the selected script responds to the active document at a time.

                               

                               

                              Martin

                              • 12. Re: Is it possible to run a script on an entire book file?
                                marykulp Level 1

                                Hi Martin. Awesome! You ROCK like MJ when he was 14! Thank you!

                                • 13. Re: Is it possible to run a script on an entire book file?
                                  Dave Saunders Level 4

                                  The script I posted in message 1 looped through the documents of a book opening each of them.

                                   

                                  Dave