Home > HOT Posts!, InDesign Scripts > Rotate single selected object

Rotate single selected object

First, let’s clear how InDesign is working with selections. Selections are always in Arrays. So, first object in Array has index 0, second object is 1, and so on. For now, we are going to take a look just how to rotate a single object.


Method #1: (easier one)

Here is script for rotating just one selected object:

var myObj = app.selection[0];
myObj.rotationAngle = 45;

First, we put object in variable, and then apply rotationAngle to the object. Rotation angle is constant, so if you apply rotation once more with same settings, nothing will change. You can also check if object is rotated like this:

var myObj = app.selection[0];
alert(myObj.rotationAngle);

Ok, we have rotated object. It has been rotated around currently selected anchor point. If you want to change anchor point from script use this:

app.activeWindow.transformReferencePoint = AnchorPoint.BOTTOM_RIGHT_ANCHOR;

And anchor point enumeration:

AnchorPoint.BOTTOM_CENTER_ANCHOR
AnchorPoint.BOTTOM_LEFT_ANCHOR
AnchorPoint.BOTTOM_RIGHT_ANCHOR
AnchorPoint.CENTER_ANCHOR
AnchorPoint.LEFT_CENTER_ANCHOR
AnchorPoint.RIGHT_CENTER_ANCHOR
AnchorPoint.TOP_CENTER_ANCHOR
AnchorPoint.TOP_LEFT_ANCHOR
AnchorPoint.TOP_RIGHT_ANCHOR

Method #2: (harder one)

In this method we are creating transformation matrices, and we are going to add just one transformation matrix and that is rotation. After that, we are going to put selected object in variable, and then apply transformation to object.

var myTrans = app.transformationMatrices.add({counterclockwiseRotationAngle:45});
var myObj = app.selection[0];
myObj.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.CENTER_ANCHOR, myTrans);

As you can see, we again have anchor point settings, which you can use from first method.

So, what’s different between these two methods? First is easier, and faster, but you can do just one transformation. With second method, you can do multiple transformations at once, and if you repeat same settings again, object will be transformed again. In first method, when we applied rotation twice, nothing happened second time, but with second method, we applied 45 degree rotation, and if we do it again, it will become 90.

I hope that this explanation helped you. Next time we will go through transforming selection with multiple objects.

  1. July 24, 2010 at 02:00

    Hi tomaxxi,

    Let me be the first to say: “Welcome to Indisnips!”

    Happy to see you around😉

    @+
    Marc

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: