8 Replies Latest reply on May 13, 2016 11:49 PM by JustOneBren

    Set the x and y location of a page?

    CoverBuilder Level 1

      Hey Guys,

       

      Does anyone know how to set the y-position of a page in a spread using JavaScript.

      I have a spread where the pages are mis-aligned and I would like to re-align them.

       

      (The same as you can set it manually at the location y in the pages tool.)

       

      The page.bounds property is read only.

       

      Any ideas?

        • 1. Re: Set the x and y location of a page?
          b91823603 Level 3

          Hi,

           

          You could use page.reframe().

          • 2. Re: Set the x and y location of a page?
            CoverBuilder Level 1

            Exactly what I was looking for. Thanks!

            • 3. Re: Set the x and y location of a page?
              Marc Autret Level 4

              Hi Bruno,

               

              Page.reframe() is OK as long as you can trust the current bounding box coordinate system and assuming the pages do not undergo weird transformations. There are some side effects to be aware of regarding page locations (and size), as discussed here: http://www.indiscripts.com/blog/public/data/coordinate-spaces-and-transformations-1/Coordi nateSpacesTransfos01-03.pdf#pa…

               

              IMHO it would be safer to apply a Y-translation to the transformation matrices of the pages that require to move. This way you don't hit their bounding boxes, you only restore the Y-parameter to its correct value. All is done within the Page-to-Spread matrix. Here is a possible implementation.

               

              function yAlignPages(/*Spread*/spd,/*uint=0*/refPageIndex,  pgs,a,i,yRef,dy)
              // -----------------------------------------------
              // spd           ::  any Spread or MasterSpread.
              // refPageIndex  ::  index (in spread) of a properly aligned page (def: 0.)
              {
                  // Boring constants
                  // ---
                  const CS_PARENT = +CoordinateSpaces.parentCoordinates,
                        ANY_ORIG  = [0,0],
                        YTRANS_MX = [1,0,0,1,0,NaN];
                 
                  pgs = spd.pages;
              
                  // Get the Y-values relative to the spread (parent.)
                  // ---
                  a = [].concat(pgs.everyItem().transformValuesOf(CS_PARENT))[0];
                  for( i = a.length ; i-- ; a[i] = a[i].matrixValues[5] );
                 
                  // What is the Y to be used as a reference?
                  // ---
                  if( (i=a.length) > (refPageIndex>>>=0) ) refPageIndex = 0;
                  yRef = a[refPageIndex];
                 
                  // Reposition mis-aligned pages.
                  // ---
                  while( i-- )
                  {
                      if( i==refPageIndex || yRef==a[i] ) continue;
              
                      YTRANS_MX[5] = yRef - a[i]; // dy
              
                      pgs[i].transform( CS_PARENT, ANY_ORIG, YTRANS_MX );
                  }
              }
              
              
              // Sample code
              // ---
              var mySpread = app.activeDocument.spreads[1];
              yAlignPages(mySpread);
              
              

               

              (Note also that the X-location of a page can't be reassigned in facing pages mode.)

               

              Hope that helps.

               

              @+

              Marc

              • 4. Re: Set the x and y location of a page?
                CoverBuilder Level 1

                Oh man, that hurts my head

                 

                I can see this is great but I have not been able to make it work.

                 

                After I run your script all pages have odd seemingly random y locations.

                 

                It also seem to have broken the manual x and y numbers as if I type a number manually inside the x or y box. The page moves but the value reverts back to 0.

                 

                Not sure what is going on?!

                • 5. Re: Set the x and y location of a page?
                  Marc Autret Level 4

                  Well, that would be useful to me to study your document (or the IDML output.) I guess there might be interesting shifts in the matrices, or maybe something related to the MasterPageTransform issue—see Override elements by script problems

                   

                  …Or maybe my code is just wrong :-(

                  We always have surprises with InDesign and page geometry!

                   

                  @+

                  Marc

                  • 6. Re: Set the x and y location of a page?
                    Marc Autret Level 4

                    Here is a new try, using myPage.resolve() instead of myPage.transformValuesOf().

                     

                    function yAlignPages(/*Spread*/spd,/*'top'|'center'|'bottom'*/vRef,/*uint=0*/refPageIndex,  pgs,a,t,i,yRef,dy)
                    // -----------------------------------------------  
                    // spd           ::  any Spread or MasterSpread.  
                    // vRef          ::  the y-anchor ref relative to which pages are realigned (def:'center')
                    // refPageIndex  ::  index (in spread) of a properly aligned page (def: 0.)  
                    {  
                        // Validate and reformat vRef.
                        // ---
                        vRef = vRef && /^top|bottom$/i.test(vRef) ?
                            (vRef+'_').toUpperCase() :
                            '';
                    
                        // Boring constants  
                        // ---  
                        const CS_PARENT = +CoordinateSpaces.parentCoordinates,  
                              ANY_ORIG  = [0,0],  
                              YTRANS_MX = [1,0,0,1,0,NaN],
                              REF_ANCHOR = [
                                +AnchorPoint[vRef+'CENTER_ANCHOR'],
                                +BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS,
                                CS_PARENT
                                ];
                         
                        pgs = spd.pages.everyItem().getElements();
                        
                        // Compute the Y-values relative to the spread (parent.)  
                        // ---  
                        for( i=pgs.length, a=[] ; i-- ; a[i]=pgs[i].resolve(REF_ANCHOR,CS_PARENT)[0][1] );
                      
                        // What is the Y to be used as a reference?  
                        // ---  
                        if( (i=a.length) > (refPageIndex>>>=0) ) refPageIndex = 0;  
                        yRef = a[refPageIndex];  
                         
                        // Reposition mis-aligned pages.  
                        // ---  
                        while( i-- )  
                        {  
                            if( i==refPageIndex || yRef==a[i] ) continue;  
                      
                            YTRANS_MX[5] = yRef - a[i]; // dy  
                      
                            pgs[i].transform( CS_PARENT, ANY_ORIG, YTRANS_MX );  
                        }
                    }  
                      
                      
                    // Sample code  
                    // ---  
                    var mySpread = app.activeDocument.spreads[0];  
                    yAlignPages(mySpread,'bottom');
                    

                     

                    The approach here is quite different, as resolve() allows to retrieve locations with respect to the bounding box of the page(s), which might be necessary to compensate shifts.

                     

                    I've tested the code in odd layouts as this one (4-page spread):

                    test01.png

                    Note that:

                    (1) page sizes are non uniform,

                    (2) a rotation is applied to page #3,

                    (3) the pages and their underlying master pages do not fit at all.

                     

                    The new function allows to realign pages relative to either top vs. center vs. bottom reference point (2nd argument).

                    Here are my results in CC 2015 / Win7 :

                     

                    test-top.png

                    test-center.png

                    test-bot.png

                     

                    It should be also noticed that the script does not pretend to deal with "page items", which is a very different question.

                     

                    Please provide feedback if you have some time, in particular if this still does not work. I'd like to have a better understanding of the issue …and to fix it.

                     

                    @+

                    Marc

                    • 7. Re: Set the x and y location of a page?
                      CoverBuilder Level 1

                      That works perfectly Marc! Awesome work. I guess I should check for transformations and remove them.

                      • 8. Re: Set the x and y location of a page?
                        JustOneBren Level 1

                        Hi Marc,

                         

                        I wondered if you could help me with a similar problem, I have a 2 page spread (190mm(w) x 269mm(h) each page) and I need to put a page margin of 30mm between them.

                        I have a thread (Move page by x-coordinates (page tool)) asking about this but so far I have not managed to work it out. I have a few replies but I have only been scripting for about 2 weeks and don't have the knowledge or skills (yet) to build the script. I ran your script and then tried to work out if I could interpret it to my needs but it's a bit over my head at the moment.

                         

                        Hope you can help, Bren