3 Replies Latest reply on Apr 28, 2014 7:13 AM by Jump_Over

    Sum of All Opened Documents Pages

    creativejoan0425

      I want to alert all opened documents pages, but is undefined, can anyone fix?

       


      var pageAlert = "";

      for (var i = 0; i < app.documents.length; i++) {


      var myDoc = app.documents[i];

                      var width = myDoc.documentPreferences.pageWidth;

                      var height = myDoc.documentPreferences.pageHeight;

                      var size = Math.round (width) + "mm x " + Math.round (height)+ "mm";

                      var eachDocPages = myDoc.pages.length;

      for (var j = 0; j < eachDocPages.length; j++) {

            var  totalPages = Math.max(eachDocPages[j]);

      }

                      if(size == size){

                               pageAlert += "\n" + myDoc.name + " : " + size;

                      }

                      else {

                                 pageAlert = size;

                       }


      }

      if (pageAlert.length) {


      alert("Paper Size: " + pageAlert + "\rTotal Pages: " + totalPages);

      }
        • 1. Re: Sum of All Opened Documents Pages
          Jump_Over Level 5

          Hi,

          Cant be sure while comparing your description to a code, but if a goal is to alert set of page sizes and sum of count of pages ==>use this:

          var 
                    pageAlert = "",
                    myDoc,
                    width, height, size,
                    eachDocPages, totalPages = 0,
                    i;
          
          for (i = 0; i < app.documents.length; i++) {
                    myDoc = app.documents[i];
                    width = myDoc.documentPreferences.pageWidth;
                    height = myDoc.documentPreferences.pageHeight;
                    size = Math.round (width) + "mm x " + Math.round (height)+ "mm";
                    eachDocPages = myDoc.pages.length;
                    totalPages += eachDocPages;
                    pageAlert += "\n" + myDoc.name + " : " + size;
                    }
            
                    alert("Paper Size: " + pageAlert + "\rTotal Pages: " + totalPages);
          

           

          If the goal is to alert max page number (name) from all opened docs, use this:

          var 
                    pageAlert = "",
                    myDoc,
                    width, height, size,
                    currMax, maxPage = 0,
                    i;
          
          for (i = 0; i < app.documents.length; i++) {
                    myDoc = app.documents[i];
                    width = myDoc.documentPreferences.pageWidth;
                    height = myDoc.documentPreferences.pageHeight;
                    size = Math.round (width) + "mm x " + Math.round (height)+ "mm";
               // here array of page names is sorted using custom function
                    currMax = myDoc.pages.everyItem().name.sort(sort_text2num)[0];
               // here max number is updated
                    maxPage = Math.max(maxPage, currMax);
                    pageAlert += "\n" + myDoc.name + " : " + size;
                    }
            
                    alert("Paper Size: " + pageAlert + "\rTotal Pages: " + maxPage);
          
          function sort_text2num (a,b) {     // sort text as numbers in reverse order (max first)
                    return parseInt(a) <= parseInt(b);
                    }
          
          

           

          Jarek

          • 2. Re: Sum of All Opened Documents Pages
            creativejoan0425 Level 1

            But If my doc 1 have 2 pages the first page number call 2, the second 1

            doc2 the first page no 2, the second 3

             

            the total pages number should be 4, but the result is 3

            • 3. Re: Sum of All Opened Documents Pages
              Jump_Over Level 5

              Hi,

               

              1st code should return 4 (count of pages)

              2nd code should return 3 (max page number)

               

              Jarek

               

              PS. Script always should return what you want.

              The question is how concrete can you be with your expectations.