Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can I set width or height of an object?

New Here ,
Nov 13, 2012 Nov 13, 2012

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

TOPICS
Scripting
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Nov 13, 2012 Nov 13, 2012

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

...
Translate
Community Expert ,
Nov 13, 2012 Nov 13, 2012

@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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 13, 2012 Nov 13, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2012 Nov 13, 2012

@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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 13, 2012 Nov 13, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 13, 2012 Nov 13, 2012
LATEST

> 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines