Copy link to clipboard
Copied
Hi Guys,
is there a simple method to set a width/height of an object in absolute values without messing with .geometricBounds?
Something like this:
app.activeDocument.selection [0].width = "150 mm";
or
app.activeDocument.selection [0].width("150 mm", ConstrainProportion.Yes, AnchorPoint.bottomLeftAnchor);
The object is in my case usually a vector graphic copied out of Illustrator.
PanDulka
> Don't you have by any chance version for CS3?
In that case we need to use the PageItem.transform() method in a very special way.
Try this:
...function setWidthHeightCS3(/*PageItem*/o, /*str*/w, /*str*/h, /*bool=false*/useVisibleBounds)
{
if( !o.transform) return;
var CS_INNER = CoordinateSpaces.INNER_COORDINATES,
BB = BoundingBoxLimits[(useVisibleBounds?'OUTER_STROKE':'GEOMETRIC_PATH') + '_BOUNDS'];
var wPt = UnitValue(w).as('pt'),
hPt = UnitValue(h).as('pt');
if( 0 >= wPt |
Copy link to clipboard
Copied
@PanDulka – I would recommend reading the following blog post:
Marc Autret
Work Around the Width/Height Gap
http://www.indiscripts.com/post/2009/10/work-around-the-width-height-gap
Uwe
Copy link to clipboard
Copied
However my article only considers the question "How to GET the size?", not "How to SET the size".
Now to answer the original question, I think PageItem.resize() is the way to go.
function setWidthHeight(/*PageItem*/o, /*str*/w, /*str*/h, /*bool=false*/useVisibleBounds)
{
if( !o.resize ) return;
var CS_INNER = CoordinateSpaces.INNER_COORDINATES,
BB = BoundingBoxLimits[(useVisibleBounds?'OUTER_STROKE':'GEOMETRIC_PATH') + '_BOUNDS'];
var wPt = UnitValue(w).as('pt'),
hPt = UnitValue(h).as('pt');
if( 0 >= wPt || 0 >= hPt ) return;
o.resize(
[CS_INNER,BB],
AnchorPoint.CENTER_ANCHOR,
ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,
[wPt,hPt,CS_INNER]
);
}
// Sample code
// ---
setWidthHeight(app.selection[0], "5cm", "50pt");
@+
Marc
Copy link to clipboard
Copied
@Marc – thank you for sharing this function. Very interesting…
Especially the "BoundingBoxLimits" declaration where you make the distinction between visibleBounds and geometricBounds in one line.
Never thought of using enumerators like this. Great.
Uwe
Copy link to clipboard
Copied
Hi Marck,
thank you for your quick reply. The function looks great but I forgot to mention that I still use InDesign CS3 that does not support PageItem.resize() method. Resize seems to be supported from in CS4 and above.
Don't you have by any chance version for CS3?
PanDulka
Copy link to clipboard
Copied
> Don't you have by any chance version for CS3?
In that case we need to use the PageItem.transform() method in a very special way.
Try this:
function setWidthHeightCS3(/*PageItem*/o, /*str*/w, /*str*/h, /*bool=false*/useVisibleBounds)
{
if( !o.transform) return;
var CS_INNER = CoordinateSpaces.INNER_COORDINATES,
BB = BoundingBoxLimits[(useVisibleBounds?'OUTER_STROKE':'GEOMETRIC_PATH') + '_BOUNDS'];
var wPt = UnitValue(w).as('pt'),
hPt = UnitValue(h).as('pt');
if( 0 >= wPt || 0 >= hPt ) return;
var wsBkp = app.transformPreferences.whenScaling,
a = o.resolve([[1,1], BB], CS_INNER)[0].
concat(o.resolve([[0,0], BB], CS_INNER)[0]),
sx = wPt / (a[2]-a[0]),
sy = hPt / (a[3]-a[1]);
app.transformPreferences.whenScaling = WhenScalingOptions.APPLY_TO_CONTENT;
o.transform(
CS_INNER,
AnchorPoint.CENTER_ANCHOR,
[sx,0,0,sy,0,0],
MatrixContent.SCALE_VALUES
);
app.transformPreferences.whenScaling = wsBkp;
}
// Sample code
// ---
setWidthHeightCS3(app.selection[0], "5cm", "100pt");
@+
Marc
Find more inspiration, events, and resources on the new Adobe Community
Explore Now