7 Replies Latest reply on Dec 7, 2014 8:22 PM by ThinkingThings

    Move object by "Reference Point"

    Ken0207

      I need to move images on my document using different reference points. But it looks like by default, it's moving them using the "AnchorPoint.TOP_LEFT_ANCHOR". I can't seem to change it.

       

      Here's my code:

       

      var myDoc = app.activeDocument;

      var changeRefPoint = app.activeWindow.transformReferencePoint = AnchorPoint.TOP_RIGHT_ANCHOR;

      if(myDoc.selection.length == 1)

      {

            if (app.selection[0].constructor.name == "Rectangle")

           {

                var rectangle_graph = app.selection[0].move([159,61.3]);

           }

      }

        • 1. Re: Move object by "Reference Point"
          Marc Autret Level 4

          Hi Ken0207,

           

          I'm afraid that the to parameter in PageItem.move(to, by) is always top-left-anchor relative, whatever the current transformReferencePoint.

           

          I cannot test in depth but the following function should do the job:

           

           

          function moveToConsideringAnchor(obj, xyDest, anchorPt)
          // -------------------------------------
          // <obj>      :  any singular PageItem specifier
          // <xyDest>   :  [x,y] location expressed in the current ruler space
          // <anchorPt> :  the considered AnchorPoint (default is top-left)
          {
              if( !(obj && obj.transform) ) return;
          
              var CS_PASTEBOARD = CoordinateSpaces.pasteboardCoordinates;
          
              anchorPt || (anchorPt=AnchorPoint.topLeftAnchor);
          
              var xy0 = obj.resolve(anchorPt,CS_PASTEBOARD)[0],
                  xy1 = obj.resolve([xyDest, anchorPt],CS_PASTEBOARD,true)[0],
                  dx = xy1[0]-xy0[0],
                  dy = xy1[1]-xy0[1];
          
              obj.transform(CS_PASTEBOARD,[0,0],[1,0,0,1,dx,dy]);
          }
          
          // ---
          // SAMPLE CODE
          // ---
          
          
          // Your settings:
          var CONSIDERED_ANCHOR = AnchorPoint.BOTTOM_RIGHT_ANCHOR,
              XY_DESTINATION = [159,61.3]; // in ruler space
          
          var sel = (1==app.selection.length) && app.selection[0];
          
          if( sel instanceof Rectangle )
              {
              moveToConsideringAnchor(sel, XY_DESTINATION, CONSIDERED_ANCHOR);
              }
          

           

          @+

          Marc

          • 2. Re: Move object by "Reference Point"
            Ken0207 Level 1

            Thank you very much, Marc. It works.

            • 3. Re: Move object by "Reference Point"
              przemek_macko Level 1

              Hi Marc

               

              Could you explain for me this line of code?

               

              if( !(obj && obj.transform) ) return;

               

               

              I ask because I thought that this line checks if the object passed to the function has the property "transform" but ESTK throws me an error when I pointed out the text object.

              Why in the segment of "if" you also have "obj" ? what does it do?

              • 4. Re: Move object by "Reference Point"
                Marc Autret Level 4

                Indeed I made a mistake. The correct test is:

                 

                if( !(obj && 'transform' in obj) ) return;

                 

                I always forgot that DOM objects do not behave as regular JS objects. A regular obj would evaluate obj.fakePropMeth to undefined, but DOM objects involve a kind of runtime pre-check, so that the usual shortcut does not work. The syntax 'fakePropMeth' in obj is generally safe in that it does not send a hidden command to the receiver.

                 

                Additional note.'fakeProp' in obj may still be misleading, though. It only checks whether the property is syntactically available. For instance, 'activeDocument' in app always evaluate to true, even while app.activeDocument may lead to a runtime error (if no document is present). This shows—once again—that the dot operator causes the program to send an actual command to the subsystem, rather than just checking that a property is available. That's the reason why we often have to use the obj.properties object, which collects only the properties that are actually available in the receiver. In my example, the correct test would be: if( 'activeDocument' in app.properties ){ /*ok*/ }

                 

                @+

                Marc

                • 5. Re: Move object by "Reference Point"
                  ThinkingThings Level 1

                  Hi Marc,

                   

                  You may want to consider updating your website with the syntax change -- http://www.indiscripts.com/post/2013/05/indesign-scripting-forum-roundup-4#hd2sb3 as I just happened to notice this today.

                   

                  Cheers!  -TT

                  • 6. Re: Move object by "Reference Point"
                    Marc Autret Level 4

                    Thanks

                    Code updated.

                     

                    Best,

                    Marc

                    • 7. Re: Move object by "Reference Point"
                      ThinkingThings Level 1

                      BTW -- great stuff you have there. Thanks for sharing!  -TT