5 Replies Latest reply on Nov 13, 2012 9:51 AM by Marc Autret

    How can I set width or height of an object?

    PanDulka Level 1

      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

        • 1. Re: How can I set width or height of an object?
          Laubender Adobe Community Professional & MVP

          @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

          • 2. Re: How can I set width or height of an object?
            Marc Autret Level 4

            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

            • 3. Re: How can I set width or height of an object?
              Laubender Adobe Community Professional & MVP

              @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

              • 4. Re: How can I set width or height of an object?
                PanDulka Level 1

                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

                • 5. Re: How can I set width or height of an object?
                  Marc Autret Level 4

                  > 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