10 Replies Latest reply on Jul 31, 2014 1:41 PM by HarveyLiu

    Can you crrect my script?

    HarveyLiu

      Hi every one

       

       

      I got this script:

       

      //---------------------------------------------------------------------------------------- -------------------

      var book = app.activeBook, 
          _content = []; 
          overlap = []; 
      for(var i =book.bookContents.length-1;i>=0;i--) 

              _content.push(book.bookContents[i].name + "\t" + (book.bookContents[i].documentPageRange.replace(/-[^-]+$/g,"")) + "\r"); 
              if(book.bookContents[i].documentPageRange == book.bookContents[i-1].documentPageRange) 
              { 
                      overlap.push(book.bookContents[i].name + "\t" +book.bookContents[i].documentPageRange + "\r" + book.bookContents[i-1].name + "\t" +book.bookContents[i-1].documentStartPage); 
                  } 
          } 
      alert(overlap.reverse())

      //alert(_content);
          WriteToFile(_content, overlap);
      function WriteToFile(text) {
          file = new File("~/Desktop/Book contents and page number.txt");
          file.encoding = "UTF-8";
          file.open("w");
          file.write(text);
          file.close();
          file.execute();
      }
      //---------------------------------------------------------------------------------------- -------------------

       

      aim to get book contents report

      and tell me the overlop page numbers

       

       

      I got the text file:

       

      ======================================================

       

      13 Notes to account.indd 28

      ,14 Corporate Information.indd 27

      ,12 Cash flow statement.indd 26

      ,11 Equity change.indd 25

      ,10 Profit and loss.indd 20

      ,09 Income statement.indd 19

      ,08 Auditor's report.indd 18

      ,07 Directors' report.indd 17

      ,06 CG report.indd 12

      ,05 MD and A.indd 7

      ,03 Financial highlight.indd 6

      ,04 Chairman's statement.indd 3

      ,02 Summary.indd 2

      ,01 Contents.indd 1

      ======================================================

      the sequence are upside down

      ======================================================

       

       

       

      1) it always report me, the book has overlop page number, even the book has no overlop page number

      2) if the files has overlop over more 2 pages or more two files have overlop page numbers the         alert(overlap.reverse())              will not correct in report

      3) the report text file has a comma at beginning of each line

      4) the sequence are upside down

       

      I expected the       alert(overlap.reverse())   don't show if book has not overlap page number

      can you fix it?

       

      Harvey

        • 1. Re: Can you crrect my script?
          Chinnadk Level 4

          Try this,

           

          var book = app.activeBook,  
              _content = [],  
              overlap = [];  
          for(var i =book.bookContents.length-1;i>=0;i--)  
          {  
                  _content.push(book.bookContents[i].name + "\t" + (book.bookContents[i].documentPageRange.replace(/-[^-]+$/g,"")) + "\r");  
                  if(book.bookContents[i].documentPageRange == book.bookContents[i-1].documentPageRange)  
                  {  
                          overlap.push(book.bookContents[i].name + "\t" +book.bookContents[i].documentPageRange + "\r" + book.bookContents[i-1].name + "\t" +book.bookContents[i-1].documentPageRange);  
                      }  
              } 
          if(overlap.length >0)
          {
                  alert(overlap.reverse())
              }
          _content.reverse();
          var con = "";
          for(var i =0;i<_content.length;i++)
          {
                  con+=_content[i] + "\r";
              }
          WriteToFile(con);
          function WriteToFile(text) {
                  file = new File("~/Desktop/Book contents and page number.txt");
                  file.encoding = "UTF-8";
                  file.open("w");
                  file.write(text);
                  file.close();
                  file.execute();
              }
          

           

          Regards,

          Chinna

          • 2. Re: Can you crrect my script?
            HarveyLiu Level 1

            Hi, Chinna

             

            It works, but without overlap report:

             

            book_report.jpg

            thanks

             

            Regard

             

            Harvey

            • 3. Re: Can you crrect my script?
              HarveyLiu Level 1

              Hi, Chinna

               

               

              I expected the       alert(overlap.reverse())   don't show if book has not overlap page number

               

              but if there was some overlap pages I need it to report to me the files or page number

               

              for instance:

               

              if

               

              05. MD&A -----------> page number: 5-9

               

              but

               

              06. CG report -------> page number 6-9 or 4-5

               

              the script have to tell me the overlop files

               

              because the sequence

               

              06.XXX always after 05.xxxxx

               

              and the page numbers shall be after then 5-9 (the page numbers of 05. xxxxxxxxxx)

               

              the function aim to tell me that have to solve the book page number problem

               

              Regard

               

              Harvey

              • 4. Re: Re: Can you crrect my script?
                Chinnadk Level 4

                Try now,

                 

                var book = app.activeBook, 
                    _content = [], 
                    overlap = [], 
                    con = "",
                    con1 = "";
                for(var i =book.bookContents.length-1;i>=0;i--) 
                { 
                        _content.push(book.bookContents[i].name + "\t" + (book.bookContents[i].documentPageRange.replace(/-[^-]+$/g,"")) + "\r"); 
                        if(book.bookContents[i].documentPageRange == book.bookContents[i-1].documentPageRange) 
                        { 
                                overlap.push(book.bookContents[i-1].name + "\t" +book.bookContents[i-1].documentPageRange + "\r" + book.bookContents[i].name + "\t" +book.bookContents[i].documentPageRange); 
                            } 
                    } 
                if(overlap.length>0)
                {
                        alert(overlap);
                        for(var i=0;i<overlap.length;i++)
                        {
                                con1 += overlap + "\r";
                            }
                    }
                
                
                _content.reverse();
                
                for(var i =0;i<_content.length;i++)
                {
                        con+=_content[i] + "\r";
                    }
                WriteToFile(con, con1);
                function WriteToFile(content, overlap) {
                        file = new File("~/Desktop/Book contents and page number.txt");
                        file.encoding = "UTF-8";
                        file.open("w");
                        file.write("Contents\r\r");
                        file.write(content);
                        file.write("\r\rOverlap\r\r")
                        file.write(overlap);
                        file.close();
                        file.execute();
                    }
                

                 

                Regards,

                Chinna

                • 5. Re: Re: Re: Can you crrect my script?
                  [Jongware] Most Valuable Participant

                  Chinna,

                   

                  HarveyLiu keeps on complaining your code does not report overlapping page numbers because your check is wrong.

                   

                  Your line

                   

                  if(book.bookContents[i].documentPageRange == book.bookContents[i-1].documentPageRange


                  checks if the page range is entirely equal, nothing more.

                   

                  To check for overlap, you need to do the following:

                   

                  1. split the .documentPageRange string on the separator '-' (note that a one-page document does not contain the separator)
                  2. you get a list of "first page, last page" for every document (see below for caveats!)
                  3. test each document's first page number and last page number separately against all other documents' page ranges
                  4. if a first pagenumber is "between" (inclusive) another document's range, it overlaps;
                  5. and the same for a last page number.

                   

                  The caveats are that: (1) page numbering may jump inside a single document, e.g., first section "1-5", second section "21-25". I think .documentPageRanges will report "1-25" for this. If necessary, inspect each document's sections and build your first-last list from this instead.

                  (2) Since a page "number" is actually a string, you could receive ranges such as "lxiv-ccix" or "AA-PQ". Again (also only if necessary), inspect the sections and store the real page numbers. Note that you still need to "remember" each  page number notation. Page number "vi" overlaps "i-x" but not "1-10".

                  • 6. Re: Can you crrect my script?
                    HarveyLiu Level 1

                    Hi, Chinna

                     

                    It keeps telling me that I have a overlap, even though I don't

                     

                    book_report.jpg

                     

                     

                     

                     

                    can you fix it?

                     

                    Hi, Obey

                     

                    I am a really new for sript, Can you Help the fix it?

                     

                     

                    Harvey

                    • 7. Re: Can you crrect my script?
                      Laubender Adobe Community Professional & MVP

                      I will not provide any code, but layout the idea for it:

                       

                      1. Open all documents of the book

                      2. Update all numbers in the book

                      3. Save the book

                      4. Loop through every open document of the book

                      5. Loop through every page of the an individual document of the book

                      6. Write the name of an individual page to an associative array and assign something meaningful like the name of the document

                      7. In case something is already assigned to the name of a page, the name is already used by a document.

                      Alert then. Or write the alert to an array and present its contents at the end of the script.

                       

                      That's all in essence.

                       

                      Uwe

                      • 8. Re: Can you crrect my script?
                        HarveyLiu Level 1

                        thank you Uwe

                         

                        Harvey

                        • 9. Re: Re: Can you crrect my script?
                          Chinnadk Level 4

                          Hi Harvey,

                           

                          Try this,

                           

                          var book = app.activeBook,  array1 = [], array2 = [],  array3 = [],  _content = "", _overlap = ""; 
                          for(var i =0;i<book.bookContents.length;i++) 
                          { 
                                  _content +=book.bookContents[i].name + "\t" + (book.bookContents[i].documentPageRange.replace(/-[^-]+$/g,"")) + "\r"; 
                                  array1.push(book.bookContents[i].name + "\t" + book.bookContents[i].documentPageRange)
                                  array2.push(book.bookContents[i].documentPageRange.split("-"))
                              } 
                          for(var i =1;i<array2.length;i++)
                          {
                                  if(array2[i][0]<=array2[i-1][1])
                                  {
                                          array3.push(array2[i].join("-"))
                                      }
                              }
                          for(var i =0;i<array1.length;i++)
                          {
                                  for(var j =0;j<array3.length;j++)
                                  {
                                          if(array1[i].toString().indexOf(array3[j]) != -1)
                                          {
                                                  _overlap+=array1[i] + "\r";
                                              }
                                      }
                              }
                          WriteToFile(_content, _overlap); 
                          function WriteToFile(content, overlap)
                          { 
                                  file = new File("~/Desktop/Book contents and page number.txt"); 
                                  file.encoding = "UTF-8"; 
                                  file.open("w"); 
                                  file.write("Contents\r\r"); 
                                  file.write(content); 
                                  file.write("\r\r\r\rOverlap Contents\r\r") 
                                  file.write(overlap); 
                                  file.close(); 
                                  file.execute(); 
                              }
                          

                           

                          Regards,

                          Chinna

                          • 10. Re: Can you crrect my script?
                            HarveyLiu Level 1

                            Hi, Chinna

                             

                            Thank you Chinna, your script is pefectly fit on my need

                             

                            thank you so much, appreciate

                             

                            Best Regard

                             

                            Harvey