11 Replies Latest reply on Jul 3, 2011 10:15 PM by REVA83

    How to remove all the contents of a book in scripting

    Dargon Kong Level 1

      I am a new scripter. Learning scripting.

      I want to write a script for my job. I want to remove all the contents of a existed book. So I write lines of code below:

       

      /*indesign version is CS3*/

       

      app.open (app.activeDocument.filePath.getFiles ("*.indb"), true)

      /*work*/
      app.activeBook.bookContents.everyItem ().remove ()

      /*doesn't work, the whole line turn to orange*/

      app.activeBook.bookContents.add(app.activeDocument.filePath.getFiles ("*.indd"))

      /*waiting for testing*/

      app.activeBook.save()

      /*waiting for testing*/

       

      But when I debug the script, it runs to stop the second line in ESTK.

      Please help me fix it. I don't know what's the problem.

       

      Thanks in advance.

        • 1. Re: How to remove all the contents of a book in scripting
          [Jongware] Most Valuable Participant

          The getFiles line does not return a single book, it always returns an array. The array may be empty, or may be one item only (which you seem to be expecting here), or it may hold five thousand entries. Point is, since getFiles returns an array and app.open expects a file, this line will always fail. Then the next line wants to operate on 'the currently active book' but there is none (as you would have seen in the interface, surely?). Hence, the script stops at that line.

           

          A warning: 'activeBook' is totally unreliable and should not be used. When you have more than one book opened, you cannot tell upon which one this script will run. And since you made it automatically empty and save the "active book", you might be in for a nasty surprise if you had something else opened as well. I usually check for app.books.length != 1, and if so I prompt the user to close all book files except the one to work on.

          • 2. Re: How to remove all the contents of a book in scripting
            Dargon Kong Level 1

            OK. Thanks for your tips first. I am sorry for my poor English and poor Scripting.

            Let's make it simpler.

            I do open a existed book, and only one. There are a lots of contents in it.

            I ues a line of code below to clean all the bookcontents:

             

            app.books[0].bookContents.everyItem ().remove()

            /*It doesn't work*/

             

            I can't figure it out myself.

            • 3. Re: How to remove all the contents of a book in scripting
              [Jongware] Most Valuable Participant

              Uh, you are doing everything correctly so far. You brought the problem down to one single line, which may show whether your syntax works or not. That's the Good news.

               

              The Bad news is, your single line command works perfectly for me! I can't think of a reason why it should fail for you ...

              • 4. Re: How to remove all the contents of a book in scripting
                Dargon Kong Level 1

                I have spent two hours trying to figure it out. Nothing happend. I tried all the possbilities including capitalization. Nothing for me to say but "Oh, god. ANYONE helps me out". The script just stops there in process. The wrong information below just read "remove".

                 

                Thank you all the same.

                • 5. Re: How to remove all the contents of a book in scripting
                  Kasyan Servetsky Level 5
                  for (var i = app.books[0].bookContents.length-1; i >= 0; i--) {
                   app.books[0].bookContents[i].remove();
                  }
                  


                  The one-line version works for me too:

                  app.books[0].bookContents.everyItem().remove();
                  
                  • 6. Re: How to remove all the contents of a book in scripting
                    Dargon Kong Level 1

                    two.jpg

                    one.jpg

                    These photoes are the information of error. I try it out third more. Neither the first method nor the second method doesn't work. BTW, my indeisgn version is CS3. Kasyan, can you save the script and send it to me? I think I miss something.

                    • 7. Re: How to remove all the contents of a book in scripting
                      Kasyan Servetsky Level 5

                      The screenshots don't help me much to figure out what causes the error because I don't know the language.

                      Kasyan, can you save the script and send it to me?

                      I don't know your e-mail address. But coping from forum and pasting should work without any problems.

                      Could you run this script from ESTK and see what error message is written into the console?

                       

                      for (var i = app.books[0].bookContents.length-1; i >= 0; i--) {
                           try {
                                app.books[0].bookContents[i].remove();
                           }
                           catch (err) {
                                $.writeln(i + " - Error: " + err.description + " - line: " + err.line);
                           }
                      }
                      
                      1 person found this helpful
                      • 8. Re: How to remove all the contents of a book in scripting
                        Dargon Kong Level 1

                        Contents below are the information of error. I don't get any useful to solve the problem. I think something wrong with my Indesign software. Not a script problem. Thank you all the same, Kasyan.

                         

                        8 - Error: remove - line: 3
                        7 - Error: remove - line: 3
                        6 - Error: remove - line: 3
                        5 - Error: remove - line: 3
                        4 - Error: remove - line: 3
                        3 - Error: remove - line: 3
                        2 - Error: remove - line: 3
                        1 - Error: remove - line: 3
                        0 - Error: remove - line: 3
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249
                        *** INTERNAL ERROR
                        TypeError: null is not an object
                        File /c/Program%20Files/Adobe/Adobe%20Utilities/ExtendScript%20Toolkit%202/Required/35omvData. jsx, line 249

                        • 9. Re: How to remove all the contents of a book in scripting
                          Dargon Kong Level 1

                          Hey, guys. I found the answer by accident.

                           

                          Here is it:

                           

                          app.activeBook.bookContents.everyItem().remove(true)

                           

                          I am so touched.

                          • 10. Re: How to remove all the contents of a book in scripting
                            Dave Saunders Level 4

                            Fascinating. The OMV shows no parameter needed for the BookContent.remove() method.

                             

                            I ran into what looks like a similar problem a year or so ago. It never occured to me to try your solution. I ended up just deleting the book file from the file system and making a new book with the same name.

                             

                            Dave

                            • 11. Re: How to remove all the contents of a book in scripting
                              REVA83

                              Hello Dave,

                               

                              Am one of the fan of your http://jsid.blogspot.com/ site. But i didn't find any new tips or ideas or indesign scripts after Saturday, October 18, 2008.

                               

                              Have you move to any new blog or site.Please let me know if there is anything new. Thanks for your valuable scripting ideas. Am looking forward your reply.

                               

                              Thanks again

                               

                              Regards

                              Peru.