14 Replies Latest reply on Nov 13, 2016 12:16 PM by cmoke73

    get page numbers out of a book

    cmoke73 Level 1

      My aims are a few steps away from where I´m standing now with my knowledges and progress.

      I want to get all the last pages of all the dokuments of a book. All I have by now is:

       

      var chapters = app.activeBook.bookContents.length;
      var pages = app.books[0].bookContents.everyItem().getElements();
      

       

      It´s not much...

       

      I found nothing at searching for solutions for this purpose. I also tried a lot. No chance.

        • 1. Re: get page numbers out of a book
          Laubender Adobe Community Professional & MVP

          That should get you going:

          Adobe InDesign CS6 (8.0) Object Model JS: BookContent

           

          Download Jongware's DOM documentation.

          The iChm files are searchable.

          Indesign JavaScript Help

           

          When on Mac OSX: Download and install the free iChm.app to open the iChm files.

           

          Regards,
          Uwe

          • 2. Re: get page numbers out of a book
            Peter Kahrel Adobe Community Professional & MVP

            While you're reading Uwe's recommendations, be aware of a couple of potentially confusing things.

            1. app.books[0] and app.activeBook denote the same object. The main difference in usage is that app.activeBook cannot be used in InDesign Server. That goes for everything that has 'active' in its name: activeDocument, activePage, etc.

            2. bookContents are not documents, they're bookContents objects. Their properties are not the same as those of documents (though there is some overlap). To get the document, you have to open the document associated with a bookContent:

             

            app.open (app.books[0].bookContents[0].fullName);

             

            3. When you ask a question, be precise. You say 'I want to get all the last pages of all the documents of a book'. What exactly do you mean by that? Do you want to extract all last pages? Get their page numbers?

             

            Peter

            • 3. Re: get page numbers out of a book
              Laubender Adobe Community Professional & MVP

              Hi Peter,
              I guess he wants to get the page names.
              Or is it the numbers?

               

              (And that could be quite a difference depending on the documents in the book and their naming scheme.)

               

              with bookContents.everyItem().documentPageRange one would get a string. The individual ranges are divided by commas. So this string has to be processed further to extract the information. Last page names, first page names, whatever…

               

              But let's see what the OP will say.

               

              Regards,
              Uwe

              • 4. Re: get page numbers out of a book
                cmoke73 Level 1

                Hallo Peter.

                 

                First, I would like to thank you for the detailed explanation.
                After a weekend reading codes and compare them I came to the same conclusion.

                 

                You are exactly right: I want to get all the last pages number: Let´s say one have a book with three documents. All of them have 3 pages. Now I want to have the page numbers: 3, 6 and 9.
                • 5. Re: get page numbers out of a book
                  cmoke73 Level 1

                  Hi Uwe.

                  As I said to Peter, I´ve tried to understand how that path-thing works in JS. In the book "InDesign automatisieren" ("Automate Indesign") by Gregor Fellenz there is a short script-reference. My problem is, that I did not found the path from the book to the page, not even to the document! (In that book there is no hint how to work with book-objects)

                  I´m sorry if my problem is to sub-basic.

                  • 6. Re: get page numbers out of a book
                    Peter Kahrel Adobe Community Professional & MVP

                    InDesign's object model is not easy to get to grips with, and that there are numerous gaps and inconsistencies doesn't make thing easier.

                     

                    To get your last pages you can start, as Uwe suggested, with

                     

                    app.books[0].bookContents.everyItem().documentPageRange;

                     

                    which returns an array of page ranges. Each page range is a string of the form '1-3' for multi-page documents or '1' for single-page documents. You then process that array, extracting the number of the last page or the single page from each range, adding that to a new array:

                     

                    pageRanges = app.books[0].bookContents.everyItem().documentPageRange;
                    lastPages = [];
                    for (i = 0; i < pageRanges.length; i++) {
                      lastPages.push (pageRanges[i].split('-').pop());
                    }
                    
                    $.writeln (lastPages);
                    

                     

                    That line 'lastPages.push (pageRanges[i].split('-').pop());' looks cryptic. If you split e.g. '1-3' on the hyphen, you get a 2-element array [1,3], whereas splitting '5' on the hypen returns a 1-element array. In other words, you always want to last element of the array produced by splitting. myArray.pop() returns the last array element, so that's what you use to extract the last or the single page number from a range string.

                     

                    Peter

                    • 7. Re: get page numbers out of a book
                      Laubender Adobe Community Professional & MVP

                      Hi, I don't think you need the path to the document, that is stored in the book.

                      Assumed one book file is open:

                       

                      var docPageRange = app.books[0].bookContents.everyItem().documentPageRange;
                      // Contents of variable docPageRange is a series of stringa stored in an array:
                      // 1-3,4-6,7-9
                      

                       

                      To get the last page name of every bookContent you could do:

                       

                      /**
                      * @@@BUILDINFO@@@ GetLastPageName-of-bookContent.jsx !Version! Sun Nov 06 2016 14:30:31 GMT+0100
                      */
                      
                      
                      // Assumed one single book is open:
                      var docPageRangeArray = app.books[0].bookContents.everyItem().documentPageRange;
                      var arrayOfLastPageNames = [];
                      
                      // Every individual documentPageRange is a string, it is text, not a number!
                      // Examples: 001-003, 1-3, 001, I-IV, etc.pp. , even 1-1 is possible, 
                      // if we have two or more page ranges beginning with page name 1 and ending with page name 1.
                      // It can contain zero times: "-" (the document is 1 page long)
                      // It can contain one time: "-" (the document is more than 1 page long)
                      
                      for(var n=0;n<docPageRangeArray.length;n++)
                      {
                          // Use a regular expression to remove all characters from the page range string
                          // from character "-" to the beginning of the page range string:
                          var nameOfLastPage = docPageRangeArray[n].replace(/^.+-/,"");
                          $.writeln(n+"\t"+"Last page name:"+"\t"+nameOfLastPage);
                          
                          arrayOfLastPageNames[arrayOfLastPageNames.length++] = nameOfLastPage;
                      };
                      
                      $.writeln(arrayOfLastpageNames);
                      

                       

                      Then work on with the array of strings you filtered out.
                      Hm, I think this thread here is related to the linked one below, where you try to insert last page names to a TOC:

                      table of content - last page number

                       

                      Next time, please tell the whole story…

                       

                      // EDIT:  Ah! I see, that Peter already answered.

                       

                      Regards,
                      Uwe

                      • 8. Re: get page numbers out of a book
                        cmoke73 Level 1

                        Thank you both Peter & Uwe. It was more help than I have expect.

                        That makes it much more clear for me. I remember I´ve seen a kind of "push (pageRanges[i].split('-').pop());" in aktion-script (Flash).
                        One day I will understand the logic behind JS! I´m sure.

                         

                         

                        N8

                        Andreas

                        • 9. Re: get page numbers out of a book
                          cmoke73 Level 1

                          Little late, I realized that I need not only the last page numbers, but the whole page number range.

                          Here my script:

                           

                          var buch = app.activeBook, 
                              _content = [ ];
                          var kapitel = app.activeBook.bookContents;
                          var seitenListe = kapitel.everyItem().documentPageRange; 
                          var inhaltSeite = app.activeDocument;
                          var inhaltsangabe = inhaltSeite.textFrames.add({geometricBounds: [27.5, 101, 284.5, 197.5]});
                          for(var i = 0; i<kapitel.length;i++) 
                                  _content.push(kapitel[i].name + "\t" + (seitenListe[i].replace(/,/g,"~b")) + "\r"); 
                             
                          $.writeln(_content); 

                           

                          If I let him write the match in the console or make an alert its perfect. But if I want to put the content into a Textframe it doesn´t work.

                          Why?

                          • 10. Re: get page numbers out of a book
                            Peter Kahrel Adobe Community Professional & MVP

                            Replace $.writeln(_content) with this:

                             

                            inhaltsangabe.insertionPoints[0].contents = _content.join('\r');

                             

                            P.

                            • 11. Re: get page numbers out of a book
                              cmoke73 Level 1

                              That´s how it works. thanks!

                               

                              What about how I tried?:

                              inhaltsangabe.contents = _content;

                               

                              why does this not work? In the loop (for) it replaces in the end all the other chapters with the last one. Outside of the loop it cause an error.

                              • 12. Re: get page numbers out of a book
                                Peter Kahrel Adobe Community Professional & MVP

                                _content is an array, you have to convert that to a single string before you can insert it into a text frame. That's what .join() does: It strings together the array elements, using '\r' so that each array element appears on a new line.

                                • 13. Re: get page numbers out of a book
                                  Peter Kahrel Adobe Community Professional & MVP

                                  For inhaltsangabe.insertionPoints[0].contents you could use your inhaltsangabe.contents as well. Force of habit.

                                  • 14. Re: get page numbers out of a book
                                    cmoke73 Level 1

                                    That makes it more clear for me. Step by step...

                                    Thanks!