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.
Hi tomaxxi,
Let me be the first to say: “Welcome to Indisnips!”
Happy to see you around 😉
@+
Marc