Dave, you are absolutely right (no surprise here) about always returning points. I'm not sure, but I think this may actually be an InDesign bug. At the end of the 'resolve' method the 'true' value is meant to indicate 'use ruler units', I believe. Having read the documentation, however, it does seem like certain transformation coordinate spaces functions only work in points.
To make this a more complete function, I've updated it to include a conversion to the current ruler units. I'm using the UnitValue object, and perhaps I'm misunderstanding something, but I cannot easily convert to a new UnitValue using the ruler measurements enumerations (it requires a string value to represent the target units). So I've had to include a switch function to get from the enumeration to the string value. Also, the UnitValue object does not seem to work with agates, and I haven't gone so far as to write my own conversion function for that, so the function has to exit with an alert if the user has agates as their ruler units.
If anyone can offer a more elegant way of dealing with this, it would be great. Or even better, some insight into why the resolve method does not seem to honour the ruler units. This _is_ getting a bit over-the-top for a simple width and height function. Maybe Adobe will consider just adding the property to the page item and putting us all (well, certainly me, anyway) out of our misery (I've put in a feature request).
> function WidthAndHeight(myRectangle) // Returns an array [width,height]
] {
]] var myRectangleTopLeft = myRectangle.resolve(AnchorPoint.topLeftAnchor, CoordinateSpaces.pasteboardCoordinates, true);
var myRectangleTopRight = myRectangle.resolve(AnchorPoint.topRightAnchor, CoordinateSpaces.pasteboardCoordinates, true);
var myRectangleBottomLeft = myRectangle.resolve(AnchorPoint.bottomLeftAnchor, CoordinateSpaces.pasteboardCoordinates, true);
]] var d1x = (myRectangleTopRight[0][0] - myRectangleTopLeft[0][0]);
var d1y = (myRectangleTopRight[0][1] - myRectangleTopLeft[0][1]);
var myWidth = hypotenuse(d1x,d1y);
var d2x = (myRectangleTopLeft[0][0] - myRectangleBottomLeft[0][0]);
var d2y = (myRectangleTopLeft[0][1] - myRectangleBottomLeft[0][1]);
var myHeight = hypotenuse(d2x,d2y);
]] var myHorizontalUnits = unitsEnumToString(app.documents[0].viewPreferences.horizontalMeasurementUnits);
var myVerticalUnits = unitsEnumToString(app.documents[0].viewPreferences.verticalMeasurementUnits);
]] myWidth = new UnitValue(myWidth,"points");
myHeight = new UnitValue(myHeight,"points");
]] myWidth = myWidth.as(myHorizontalUnits);
myHeight = myHeight.as(myVerticalUnits);
]] return [myWidth,myHeight];
]] function hypotenuse(d1,d2)
]]] {
]]]] return Math.sqrt ( Math.pow(d1,2) + Math.pow(d2,2) );
]]] }
]] function unitsEnumToString(myUnitsEnum)
]]] {
]]]] switch(myUnitsEnum)
]]]]] {
]]]]]] case 2054188905 :
]]]]]]] return "points";
]]]]]] case 2054187363 :
]]]]]]] return "picas";
]]]]]] case 2053729891 :
]]]]]]] return "inches";
]]]]]] case 2053729892 :
]]]]]]] return "inches decimal";
]]]]]] case 2053991795 :
]]]]]]] return "millimeters";
]]]]]] case 2053336435 :
]]]]]]] return "centimeters";
]]]]]] case 2053335395 :
]]]]]]] return "ciceros";
]]]]]] default :
]]]]]]] alert("Cannot convert to the current ruler units. Sorry.");
exit();
]]]]] }
]]] }
]] }