RESIZE, REFRAME

In our previous post we explored AppleScript’s resize command for modifying a page item in Adobe InDesign. A command similar to resize is reframe. These are similar in that they both change the size of the targeted object. The two are similar in that one of the benefits of using either of these commands is the fact that the size of the page item is changed without scaling its content or stroke weight. A command similar to resize is reframe. Reframe moves the bounding box of the subject item. The big difference in the two is in how the targeted object is resized:

  1. resize – perforns some mathematical process to change the size relative to its current bounds.
  2. Reframe – uses a given set of coordinates for the reframed object.

Both methods rely on a specified coordinate space:

  1. inner – the coordinate space in which the object was created
  2. parent – the coordinate space of the object’s containert which can be different than its object depending on the angle of its horizontal and vertical axes.
  3. pasteboard – the coordinate space for the document which, interestingly, does not correspond to the document’s ruler or zero point, and has nothing to do with the pasteboard.

To understand how this all works, a few sample scripts should help clarify.

Create an InDesign document with a rectangle on its first page. Save the document. With the rectangle selected, run the following script./p>

Resize (AppleScript)

   tell application "Adobe InDesign CC 2015"
	set selList to selection
	set selItem to item 1 of selList
	set myDuplicate to duplicate selItem
		resize myDuplicate in inner coordinates from center anchor by multiplying current dimensions by values {2, 2}
   end tell

Resize (ExtendScript)

   var myRectangle = app.selection[0];
   var myDuplicate = myRectangle.duplicate();
   myDuplicate.resize(CoordinateSpaces.innerCoordinates, AnchorPoint.centerAnchor, ResizeMethods.multiplyingCurrentDimensionsBy, [2,2]);
   myDuplicate

;

To get the geometric coordinates of the resized object within the given coordinate space, InDesign provides another command: resolve.

RESOLVE

Resolve returns a geometric coordinate of an anchor point for the referenced object within its given coordinate space.

To see how this works, add the following to your script from above:

Applescript

   set myTopLeft to resolve myDuplicate in inner coordinates location top left anchor

ExtendScript

   var myTopLeft = myDuplicate.resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.INNER_COORDINATES)[0];
   myTopLeft;

Notice that the result of the resolve command is given in point units with the horizontal coordinate before the vertical (x, y).

Should you wish the result to be returned in the measurement units currently set for the document, rather than points, add the optional parameter considering ruler units to your statement:

AppleScript

   set myTopLeft to resolve myDuplicate in inner coordinates location top left anchor with considering ruler units

ExtendScript

   var myTopLeft = myDuplicate.resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.INNER_COORDINATES, true)[0];

REFRAME

To resize an object using reframe, the coordinate values for the opposing corners (top left, bottom right) are specified. Revert your document back to the original state (File > Revert). With the rectangle selected, resize the object using one of the following:

Reframe (AppleScript)

   tell application "Adobe InDesign CC 2015"
	set selList to selection
	set selItem to item 1 of selList
	set myDuplicate to duplicate selItem
	set myTopLeft to resolve myDuplicate in inner coordinates location top left anchor
	set myBottomRight to resolve myDuplicate in inner coordinates location bottom right anchor
	set x0 to (item 1 of item 1 of myTopLeft) - 72
	set y0 to item 2 of item 1 of myTopLeft
	set x1 to (item 1 of item 1 of myBottomRight) + 72
	set y1 to item 2 of item 1 of myBottomRight
	reframe myDuplicate in inner coordinates opposing corners {{x0, y0}, {x1, y1}}
   end tell

Reframe (ExtendScript)

   var myRectangle = app.selection[0];
   var myDuplicate = myRectangle.duplicate();
   var myTopLeft = myDuplicate.resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.INNER_COORDINATES)[0];
   var myBottomRight = myDuplicate.resolve(AnchorPoint.BOTTOM_RIGHT_ANCHOR, CoordinateSpaces.INNER_COORDINATES)[0];
   var x0 = myTopLeft[0]-72;
   var y0 = myTopLeft[1];
   var x1 = myBottomRight[0]+72;
   var y1 = myBottomRight[1];
   myDuplicate.reframe(CoordinateSpaces.INNER_COORDINATES, [[x0, y0], [x1, y1]]);

Where the fun begins is when you start working with page items that are rotated. Repeat the exercises above using a page item that is rotated.

MATRIX TRANSFORMATIONS

Now that we have a better understanding of coordinate spaces when it comes to resizing (reframing) an object, let’s look at some more complex transformations. In addition to resizing, a transformation can include rotation, shearing (or skewing), and movement (translation). When you need to perform more than one operation to the target object, you accomplish this by creating and passing a transformation matrix object to its transform method. The transformation matrix object specifies the operations to be performed, the coordinate space in which it is to be performed, and the transformation origin. The order in which the various transformation operations take place is very important. Changing the order of the transformations can produce different results.

Contrary to other objects in InDesign, the transformation matrix is a different animal in AppleScript than it is in ExtendScript. The reason being that ExtendScript offers a more robust platform for working with transformation than does AppleScript. This includes support for working with trigonometric functions. We will leave the higher math for those who love working with sines and cosines.

When working with AppleScript, you will soon discover that you can’t create a transformation matrix and then apply the various transformation properties to it. For instance, if create a transformation matrix and try to tell tell it to set its horizontal scale factor to 20, a script will simply return the error “‘horizontal scale factor’ is a read only property.”

   --produces an error
   tell application "Adobe InDesign CC 2015"
	set myMatrix to make transformation matrix with properties {name:"transformIt"}
	tell myMatrix
		set horizontal scale factor to 1.5
	end tell
   end tell

But you can set the properties as part of the process of creating the matrix:

   tell application "Adobe InDesign CC 2015"
	set myMatrix to make transformation matrix with properties {name:"transformIt", counterclockwise rotation angle:15, horizontal scale factor:1.5}
   end tell

To transform an object using the matrix, the matrix is provided as part of the target object’s transform method.

Here again, a sample script can help visualize the process. Revert your sample document back to its original state. With the rectangle selected, run the following to perform a series of transformations.

Transform (AppleScript)

   tell application "Adobe InDesign CC 2015"
	set selList to selection
	set selItem to item 1 of selList
	set myDuplicate to duplicate selItem
	set myMatrix to make transformation matrix with properties {counterclockwise rotation angle:15, clockwise shear angle:30, horizontal scale factor:1.5, horizontal translation:24, vertical scale factor:1, vertical translation:0}
	transform myDuplicate in inner coordinates from center anchor with matrix myMatrix
   end tell 

Transform (AppleScript)

   tell application "Adobe InDesign CC 2015"
	set selList to selection
	set selItem to item 1 of selList
	set myDuplicate to duplicate selItem
	set myMatrix to make transformation matrix with properties {counterclockwise rotation angle:45, clockwise shear angle:30, horizontal scale factor:1.5, horizontal translation:24, vertical scale factor:1, vertical translation:0}
	transform myDuplicate in inner coordinates from center anchor with matrix myMatrix
   end tell

ExtendScript, on the other hand, provides a complete set of methods for setting the various transformation values for a matrix. Here, you create the matrix and then apply a separate method for each transformation. These include: rotateMatrix, scaleMatrix, shearMatrix, and translateMatrix.

The modified matrix object is then used as part of the parameter argument list for the target object’s transform method.

Transform (ExtendScript)

   var myRectangle = app.selection[0];
   var myDuplicate = myRectangle.duplicate();
   var myMatrix = app.transformationMatrices.add();
   myMatrix = myMatrix.rotateMatrix(45);
   myMatrix = myMatrix.shearMatrix(30);
   myMatrix = myMatrix.scaleMatrix (1.5, 1);
   myMatrix = myMatrix.translateMatrix (24, 0);
   myRectangle.transform(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.centerAnchor, myMatrix); 

MORE POWER TO YOU

The interesting thing about transformations is they are remembered by the page item so you can tell the object to transform again, transform again individually, transform a sequence again, or transform a sequence again individually. The transformations remain with the document until closed. This works whether the page item is translated manually or using a transformation matrix.

UPWARD AND ONWARD

Working with transformation matrices is something you will want to spend some time with. Use the following script to experiment. Change values and translation parameters just to see what happens when you run the script.

Transformation Script Template

   tell application "Adobe InDesign CC 2015"
	set trans1 to make transformation matrix with properties {name:"Diagonal72", horizontal translation:72, vertical translation:72}
	tell document 1
		tell page 1
			set newItem to make rectangle with properties {name:"NewItem", geometric bounds:{"3p", "3p", "12p", "15p"}}
		end tell
	end tell
	set dupItem to duplicate newItem
	transform dupItem in parent coordinates from top right anchor with matrix trans1
	tell dupItem
		duplicate
		transform again
	end tell
   end tell

Have fun and think of ways you can use transformations to your advantage in your next script.