• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Move object by "Reference Point"

Community Beginner ,
Jan 24, 2013 Jan 24, 2013

Copy link to clipboard

Copied

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]);

     }

}

TOPICS
Scripting

Views

4.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jan 25, 2013 Jan 25, 2013

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)

{

    i

...

Votes

Translate

Translate
Guide ,
Jan 25, 2013 Jan 25, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 25, 2013 Jan 25, 2013

Copy link to clipboard

Copied

Thank you very much, Marc. It works.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 13, 2013 Sep 13, 2013

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 14, 2013 Sep 14, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 07, 2014 Dec 07, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 07, 2014 Dec 07, 2014

Copy link to clipboard

Copied

Thanks

Code updated.

Best,

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 07, 2014 Dec 07, 2014

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines