Skip to main content
Known Participant
January 4, 2024
Answered

Transform properties width

  • January 4, 2024
  • 3 replies
  • 3510 views

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.

This topic has been closed for replies.
Correct answer GNDGN

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);

 

3 replies

Marc Autret
Legend
January 9, 2024

Hi @Chandrakanthi26574696ugut 

 

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

Marc Autret
Legend
January 9, 2024

[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

Known Participant
January 10, 2024

Thanks for the updates and new approach

rob day
Community Expert
Community Expert
January 4, 2024

the values are not matching

 

Hi @Chandrakanthi26574696ugut , do you have the linked image or its parent container selected? They wouldn't necessarily have the same width.

Known Participant
January 4, 2024

Hi the script is looping through all the graphics and calculating width on geometricbounds.

script is not selecting these links.

rob day
Community Expert
Community Expert
January 4, 2024

script is not selecting these links.

 

But what are you selecting to check the Transform panel Width?

 

For example this gets both the linked image width and its container frame width, which is what‘s showing in the Transform panel when I select the frame :

 

 

var lnks = app.documents[0].links;

//an image, the link’s parent
var img = lnks[0].parent

//the image bounds and width
var ib = img.geometricBounds
var iw = ib[3]-ib[1]

//the image’s parent container frame bounds and width
var fb = img.parent.geometricBounds
var fw = fb[3]-fb[1]

alert("\rImage Width: " + iw + "\r" + "Container Width: " + fw)

 

 

 

The image direct selected:

GNDGN
GNDGNCorrect answer
Inspiring
January 4, 2024

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);

 

____________________Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5