8 Replies Latest reply on Apr 16, 2012 7:46 PM by John Hawkinson

    Move a selection of page items

    Dan Macy Level 1

      I have a rectangle selected. I want to run a script that simply defines the X and Y coordinates of the rectangle, in other words, moves it from the location where it's currently pasted on this page to some other location specified by the script (on this page). I tried the following, but nothing happens:

       

      app.selection[0].geometricBounds[1] = 2.0;

      app.selection[0].geometricBounds[2] = 7.0;

       

      My page size is standard 8-1/2" x 11".

       

      I also eventually need to copy this object and position the copy somewhere else on the page. I'm new to scripting, so any pointers in getting me started would be greatly appreciated. Thanks!

        • 1. Re: Move a selection of page items
          Larry G. Schneider Adobe Community Professional & MVP

          What language are you scripting in?

          • 2. Re: Move a selection of page items
            Dan Macy Level 1

            JavaScript and InDesign CS5.

            • 3. Re: Move a selection of page items
              Larry G. Schneider Adobe Community Professional & MVP

              So JS is "0" based. From the object model reference for a rectangle:

               

              The bounds of the Rectangle excluding the stroke width, in the format [y1, x1, y2, x2], which give the coordinates of the top-left and bottom-right corners of the bounding box.

               

              So what you have listed above are x1 and y2. You will need to use the proper pair [0] and [1] or [2] and [3] and set the reference point.

              • 4. Re: Move a selection of page items
                Laubender Adobe Community Professional & MVP

                @Dan – try the move() method and/or duplicate() method on the selection.


                See:
                http://jongware.mit.edu/idcsjs5.5/pc_Rectangle.html#move

                 

                and:

                http://jongware.mit.edu/idcsjs5.5/pc_Rectangle.html#duplicate

                 

                If you like the duplicate() method you would remove your original after duplication.
                Also think about the viewPreferences of the document:

                 

                viewPreferences.rulerOrigin

                viewPreferences.horizontalMeasurementUnits

                viewPreferences.verticalMeasurementUnits

                 

                If you are searching for move or duplicate here in the forum you can find some examples.

                 

                Uwe

                1 person found this helpful
                • 5. Re: Move a selection of page items
                  Dan Macy Level 1

                  Thank you for your helpful reply. Here is my script:

                   

                  var centerColumn1 = 2.1875;

                  var centerColumn2 = 5.5625;

                  var centerRow1 = 1.6875;

                  var centerRow2 = 4.0625;

                  var centerRow3 = 6.4375;

                  var centerRow4 = 8.8125;

                  var centerX, centerY;

                  var deltaX, deltaY;

                  var rectangle;

                  var x1, x2, y1, y2;

                   

                  if (app.documents.length && app.selection.length > 0) {

                      rectangle = app.selection[0];

                      y1 = rectangle.geometricBounds[0];

                      x1 = rectangle.geometricBounds[1];

                      y2 = rectangle.geometricBounds[2];

                      x2 = rectangle.geometricBounds[3];

                         

                      centerX = (x1 + x2)/2.0;

                      centerY = (y1 + y2)/2.0;

                     

                      deltaX = centerColumn1 - centerX;

                      deltaY = centerRow1 - centerY;

                     

                      app.selection[0].geometricBounds[0] = y1 + deltaY;

                      app.selection[0].geometricBounds[1] = x1 + deltaX;

                      app.selection[0].geometricBounds[2] = y2 + deltaY;

                      app.selection[0].geometricBounds[3] = x2 + deltaX;

                      }

                   

                  I put an alert after setting the values of x1, x2, y1 and y2, and the values are correct. However, the object doesn't move at the end of the script. Is there a different way I should be setting the new coordinates?

                  • 6. Re: Move a selection of page items
                    [Jongware] Most Valuable Participant

                    geometricBounds is an array, but it's a special one -- most likely because it's directly "bound" to a real existing object (if such a thing can exist in the memory of a computer). So you cannot change just one of its numbers (it would "deform" the object it points to). But you can set a new set of bounds in one command:

                     

                    app.selection[0].geometricBounds = [ y1 + deltaY, x1 + deltaX,y2 + deltaY, x2 + deltaX];

                    1 person found this helpful
                    • 7. Re: Move a selection of page items
                      Dan Macy Level 1

                      That worked! Thank you!

                       

                      Now, what if I have a collection of objects that is selected? How do I find the center of that collection? Do I have to loop on all the objects and keep track of the minimum and maximum X and Y? I want to move the whole collection to a new X and Y based on the center of the collection.

                       

                      The objects are on different layers, so I can't group them without losing the layer distinctions.

                      • 8. Re: Move a selection of page items
                        John Hawkinson Level 5

                        app.selection[0].geometricBounds[1] = 2.0;

                        The actual problem here is that once you dereference element [1] of the geometricBounds array, it loses its connection to the DOM and you're just setting an anonymous array element to 2.0. You need to set all of geometricBounds as once, as was illustrated in Jongware's solution.

                         

                        Now, what if I have a collection of objects that is selected? How do I find the center of that collection? Do I have to loop on all the objects and keep track of the minimum and maximum X and Y? I want to move the whole collection to a new X and Y based on the center of the collection.

                         

                        The objects are on different layers, so I can't group them without losing the layer distinctions.

                        Yes, you have to do this manually.

                        You can loop over them and track their minimum coordinates.

                        You could also duplicate them, group them, and track the group (might cause text wrap issues).