Copy link to clipboard
Copied
Hi
Is it possible to get the value shown in the width box in the tranform panel, through a script ?
I have tried substracting geometricbounds values, but for every embedded link(png/psd), the values are not matching.
I am not aware of any alternative method to read the width of an element except using the geometricBounds coordinates.
Select an element and try this:
var gb = app.selection[0].geometricBounds;
var width = gb[3] - gb[1];
alert(width);
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Sorry, my example script is setting absoluteRotationAngle—rotationAngle works.
Copy link to clipboard
Copied
Sorry, my example script is setting absoluteRotationAngle—rotationAngle works.
By @rob day
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Image.html
absoluteRotationAngle is "The rotation angle of the Image relative to its containing object. (Range: -360 to 360)" - and it looks like is the sum of all rotations of all parent objects. or maybe not?
rotationAngle is "The rotatation angle of the Image. (Range: -360 to 360)" - per Page / Document.
As per the screenshot I've posted earlier:
Copy link to clipboard
Copied
To my knowledge, the dimensions shown in the Transform panel always reflect the size of the inner bounding box of the selected object — assuming a single object is selected — BUT with respect to the scale factor of the inner-to-pasteboard matrix and using the ruler units, so you get relevant values that fit the viewport.
In general you can't get reliable information from the geometricBounds property. But contrary to what was said above, it is possible to calculate these values from transformation states, including for internal objects like Images, etc, which would not fit their container.
Also, the preference Dimensions Include Stroke Weight impacts the displayed dimensions, which can also be taken into account by the script.
I have just written the code below as a first sketch (not tested it in all possible conditions but gives fairly satisfactory results):
(function selTransformDimensions( sel,VP,BBL,CS_IN,CS_PB,tl,br,mx,rw,rh,wu,hu)
//----------------------------------
// Display the dimensions of the selection (W,H)
// as shown in the Transform panel.
{
const PRECISION = 1e4;
sel = (app.selection||0)[0]||0;
if( !sel.hasOwnProperty('resolve') ) return;
VP = app.properties.activeDocument.viewPreferences;
// Boring enums.
BBL = +BoundingBoxLimits
[
app.transformPreferences.dimensionsIncludeStrokeWeight
? 'OUTER_STROKE_BOUNDS'
: 'GEOMETRIC_PATH_BOUNDS'
];
CS_IN = +CoordinateSpaces.innerCoordinates;
CS_PB = +CoordinateSpaces.pasteboardCoordinates;
// Inner bounding box corners -> inner dims (in pt)
tl = sel.resolve([ [0,0], BBL, CS_IN ], CS_IN)[0];
br = sel.resolve([ [1,1], BBL, CS_IN ], CS_IN)[0];
w = br[0] - tl[0];
h = br[1] - tl[1];
// Compensate the scale factors relative to PB.
mx = sel.transformValuesOf(CS_PB)[0];
w *= mx.horizontalScaleFactor;
h *= mx.verticalScaleFactor;
// Use horiz./vert. ruler units (instead of pt)
tl = sel.resolve( [[0,0],0], CS_PB, true)[0];
br = sel.resolve( [[1,1],0], CS_PB, true)[0];
rw = br[0]-tl[0];
rh = br[1]-tl[1];
// Final message.
wu = 'W: ' + Math.round((PRECISION*w)/rw)/PRECISION
+ ' ' + VP.horizontalMeasurementUnits.toString();
hu = 'H: ' + Math.round((PRECISION*h)/rh)/PRECISION
+ ' ' + VP.verticalMeasurementUnits.toString();
alert( [wu,hu].join('\r') );
})();
Animation shows script behavior with various cases of (nested, rotated…) selection:
I don't remember how the shear attribute is dealt with in the transform panel. This scenario might require a small adjustment of the proposed code… if you really need it.
Hope that helps.
Best,
Marc
Copy link to clipboard
Copied
[In addition to my previous message]
When a Shear X angle α is applied, the displayed height is updated accordingly (while width is not impacted), so we must divide h by cosα, where α ranges in ]-π/2,+π/2[. This leads to the fix below:
(function selTransformDimensions( sel,VP,BBL,CS_IN,CS_PB,tl,br,mx,rw,rh,wu,hu,a)
//----------------------------------
// Display the dimensions of the selection (W,H)
// as shown in the Transform panel.
// [FIX240109] Now supporting Shear X angle.
{
const PRECISION = 1e4;
sel = (app.selection||0)[0]||0;
if( !sel.hasOwnProperty('resolve') ) return;
VP = app.properties.activeDocument.viewPreferences;
// Boring enums.
BBL = +BoundingBoxLimits
[
app.transformPreferences.dimensionsIncludeStrokeWeight
? 'OUTER_STROKE_BOUNDS'
: 'GEOMETRIC_PATH_BOUNDS'
];
CS_IN = +CoordinateSpaces.innerCoordinates;
CS_PB = +CoordinateSpaces.pasteboardCoordinates;
// Inner bounding box corners -> inner dims (in pt)
tl = sel.resolve([ [0,0], BBL, CS_IN ], CS_IN)[0];
br = sel.resolve([ [1,1], BBL, CS_IN ], CS_IN)[0];
w = br[0] - tl[0];
h = br[1] - tl[1];
// Apply the scale factors (relative to PB).
mx = sel.transformValuesOf(CS_PB)[0];
w *= mx.horizontalScaleFactor;
h *= mx.verticalScaleFactor;
// [FIX240109] Apply the shear X angle (relative to PB).
(a=mx.clockwiseShearAngle) && (h/=Math.cos(a*Math.PI/180));
// Use horiz./vert. ruler units (instead of pt)
tl = sel.resolve( [[0,0],0], CS_PB, true)[0];
br = sel.resolve( [[1,1],0], CS_PB, true)[0];
rw = br[0]-tl[0];
rh = br[1]-tl[1];
// Final message.
wu = 'W: ' + Math.round((PRECISION*w)/rw)/PRECISION
+ ' ' + VP.horizontalMeasurementUnits.toString();
hu = 'H: ' + Math.round((PRECISION*h)/rh)/PRECISION
+ ' ' + VP.verticalMeasurementUnits.toString();
alert( [wu,hu].join('\r') );
})();
Best,
Marc
Copy link to clipboard
Copied
Thanks for the updates and new approach