6 Replies Latest reply on Jun 10, 2015 11:31 PM by BEGINNER_X

    Move Pages within Document (as per Width)

    BEGINNER_X Level 3

      Hi All,

       

      First of all, Tons of thanks to all Forum Members.

      You help and guide me a lot in Javascript.

       

      Request:

      1. In Active Document

      2. Collect all Pages width

      3. Sort out as per width

      4. Move pages as per width(minimum to maximum)   //Here is the Problem, Plz help..


      Current Script Output(LEFT) & Needed Output (RIGHT):


      Screen Shot 2015-06-06 at 2.06.39 PM.png              Screen Shot 2015-06-06 at 10.29.02 AM.png    

      Coding is below:

       

      var destination_doc = app.activeDocument;
      destination_doc.documentPreferences.allowPageShuffle = true;
      destination_doc.spreads[-1].allowPageShuffle = true;     //
      var myWidthArray = new Array(); 
      
      var myPages = app.activeDocument.pages;   //Doubt, pages or spread
      for(p=0; p<myPages.length; p++)
      {
          var myDocument_Width = Math.round(myPages[p].bounds[3]-myPages[p].bounds[1]);
          var myPage_Name = myPages[p];
          //var myPage_Name = myPages[p].name;
          var myReq = {w:myDocument_Width, d:myPage_Name};
          myWidthArray.push(myReq)
          }
      alert("FirstWidth: " + myWidthArray.toSource()); 
      
      var myPoints = myWidthArray;  
      myPoints.sort(function(a, b){return a.w * 1-b.w * 1}); 
      alert("myPoints: " + myPoints.toSource()); 
      
      for(var k=0; k<myPoints.length; k++) 
      { 
           var sourceSpreads = myPoints[k].d; 
           alert(sourceSpreads);
           sourceSpreads.move(LocationOptions.AT_END, app.activeDocument.pages.item(-1));     //I think here is the problem
           } 
      
      

       

      Thanks in Advance

      Siraj

        • 1. Re: Move Pages within Document (as per Width)
          Chinnadk Level 4

          Hi Siraj,

           

          Try this,

           

          var doc = app.activeDocument;
          var pages = doc.pages;
          var pagewidth = [];
          for(var i=0;i<pages.length;i++)
          {
                  pagewidth.push(Math.round(pages[i].bounds[3] - pages[i].bounds[1]))
              }
          pagewidth.sort(function (a,b){return a - b;});
          for(var i=0;i<pagewidth.length;i++)
          {
                  for(var j=0;j<pages.length;j++)
                  {
                          if(Math.round(pages[j].bounds[3] - pages[j].bounds[1]) == pagewidth[i])
                          {
                                  pages[j].move(LocationOptions.AT_END);
                              }
                      }
              }
          

           

          Regards,

          Chinna

          • 2. Re: Move Pages within Document (as per Width)
            BEGINNER_X Level 3

            Hi Chinna,

             

            Thanks for your immediate help...

             

            Please look at the screenshot by using provided script,

             

            Screen shot 2015-06-06 at 4.20.25 PM.png

             

            Even though, I added the below lines in the top of script:

            var doc = app.activeDocument;  
                doc.documentPreferences.allowPageShuffle = true;
                doc.spreads[-1].allowPageShuffle = true;
            

             

            Thanks a lot for you help!!!

            • 3. Re: Move Pages within Document (as per Width)
              Trevorׅ Adobe Community Professional

              Hi Siraj,

               

              Looks like a fish to me.

               

              The answer to this is the same as the last question you asked on sticky pages.

              The rule is that when you move pages of different sizes they will stick together to form a spread so you either have to move the spreads instead of the pages in your case or add temp pages and remove them in other cases.

               

              So with Chinna's snippet in your case  would need to change

              pages[j].move(LocationOptions.AT_END);

              to

              doc.spreads[j].move(LocationOptions.AT_END);

               

              Regards

               

              Trevor

              • 4. Re: Move Pages within Document (as per Width)
                Trevorׅ Adobe Community Professional

                Hi Chinna,

                 

                I had a look at your snippet and it leaves a bit of room for improvement.

                This is a different approach. The n is just there to see how many times the spreads are being told to move.

                I put the snippets into a doScripts to make it easier to compare.

                 

                In the below case your snippet is "moving" the spreads 8 times and that is true even if you rerun the snippet.

                It should only move 2 pages.

                 

                HTH

                 

                Trevor

                _Untitled-31 @ 12%_2015-06-07_14-36-16.png

                My Method

                app.doScript (f, ScriptLanguage.JAVASCRIPT, undefined,UndoModes.FAST_ENTIRE_SCRIPT);
                function f () {
                    var myPages, mySortedPages, l, n = 0;
                        mySortedPages = (myPages = app.activeDocument.pages).everyItem ().getElements ().slice(0).sort(function (a,b) {return (a.bounds[3] - a.bounds[0]) - (b.bounds[3] - b.bounds[0])});
                        l = myPages.length;
                        while (l--) { if (mySortedPages[l].documentOffset != l) mySortedPages[l].parent.move(LocationOptions.AFTER, myPages[l].parent) && n++; }
                        alert(n + " page moves")
                }
                
                

                 

                 

                 

                Below is your method just with the page move changed to spreads move and an alert of the spread move calls

                app.doScript (f, ScriptLanguage.JAVASCRIPT, undefined,UndoModes.FAST_ENTIRE_SCRIPT);
                function f() {
                   var doc = app.activeDocument;
                    var pages = doc.pages;
                    var pagewidth = [];
                    var n = 0;
                    for(var i=0;i<pages.length;i++)
                    {
                            pagewidth.push(Math.round(pages[i].bounds[3] - pages[i].bounds[1]))
                        }
                    pagewidth.sort(function (a,b){return a - b;});
                    for(var i=0;i<pagewidth.length;i++)
                    {
                            for(var j=0;j<pages.length;j++)
                            {
                                    if(Math.round(pages[j].bounds[3] - pages[j].bounds[1]) == pagewidth[i])
                                    {
                                            doc.spreads[j].move(LocationOptions.AT_END) && n++;
                                        }
                                }
                    alert(n + " page moves")
                        }
                }
                
                
                • 5. Re: Move Pages within Document (as per Width)
                  Trevorׅ Adobe Community Professional

                  Something not answered here?

                  • 6. Re: Move Pages within Document (as per Width)
                    BEGINNER_X Level 3

                    Hi Trevor/Chinna,

                     

                    Sorry for the delayed response. Due to emergency, went to my native.

                     

                    Tons of thanks...

                     

                    On that day itself, I also got answered

                    by using Spreads.pages[0].bounds[3]-Spreads.pages[0].bounds[1].

                     

                    And lacking in Dynamic and Static Array also.

                     

                    Initially:

                    var mySpreads = app.activeDocument.spreads;

                     

                    Finally:

                    var mySpreads = app.activeDocument.spreads.everyItem().getElements();

                     

                    Finally got solution.

                     

                    Thanks again...