5 Replies Latest reply on Oct 30, 2017 3:40 AM by Silly-V

    How to scale all selected objects by 1 inch without grouping

    CustomJed Level 1

      Hello everyone!

       

      I'm new to scripting and am probably in over my head. After days of searching these forums and stack overflow and reading all the references people recommend I still can't figure out how to apply a scale to a selection without first grouping the items.  I've been able to get it to scale each item, but the problem is it is scaling at the objects point of origin, instead of the mass selections.  I can't rely on grouping because many of the files I work on have locked layers and grouping before scaling moves all the selected objects to another layer.  Here is the code I'm trying to make work.  I'm aware the actual resize code doesn't work at all.

       

      #target Illustrator
      if ( app.documents.length > 0 ) 
      {    var myDoc = app.activeDocument;    var selectedObjects = myDoc.selection;
          for (i = 0; i < selectedObjects.length; i++ ) 
          {  
              var currentSize = selectedObjects.width;
              var desiredSize = currentSize + 72;
              var scaleFactor = ((desiredSize/currentSize) * 100);
              selectedObjects.resize (scaleFactor,scaleFactor,true,true,true,true,00,Transformation.TOP);  
          }
      }    
      


      If anyone has any suggestions I would love it! Thanks!

        • 1. Re: How to scale all selected objects by 1 inch without grouping
          Alexander Ladygin Level 2
          var s = selection, i = s.length;
          while ( i-- ) {
              var scale = ( ( s[i].width + 72 ) / s[i].width ) * 100;
              s[i].resize( scale, scale, true, true, true, true, 00, Transformation.TOP );
          }
          
          

           

          or:

          var s = selection, i = s.length;
          while ( i-- ) {
              s[i].width += 72;
              s[i].height += 72;
              s[i].left -= 36;
          }
          
          • 2. Re: How to scale all selected objects by 1 inch without grouping
            CustomJed Level 1

            Thanks for the fast response Alexander! Both scripts work well to scale up 1 inch, but they are both producing the same behavior I was able to get.  The problem these scripts have is that they scale up based on the origin point of the object, and not the selection.  The effect I'm trying to get is the same as selecting more than one object and then typing +1 into the width of the transform window.

            • 3. Re: How to scale all selected objects by 1 inch without grouping
              Silly-V Adobe Community Professional

              Try this trick:

              1. Get bounds of your selection
                • calculate it out using some calculation function to compare each object's bounds to get the very var top, left, right and bottom numbers
                • Or, manipulate artboards to get the bounds by using an artboard (could be temp artboard) and use the "fit to selected artwork" command to get a snapshot of the bounds via the artboardRect property of the artboard.
              2. Set the document origin to the calculated middle of the previously-obtained bounds rectangle of the selection (doc.rulerOrigin = [x, y])
              3. Use a resize() function with a Transformation.DOCUMENTORIGIN parameter to get the scaling from the center of the new document origin, which is the middle of your selection
              4. Reset the document origin back (if you saved the origin in a variable for this purpose)
              • 4. Re: How to scale all selected objects by 1 inch without grouping
                Silly-V Adobe Community Professional
                #target illustrator-19
                function  ScaleSelection() {
                
                
                  var scaleUpBy = 1 * 72;
                
                
                  function marker(xy){
                  var doc = app.activeDocument;
                  var p = doc.pathItems.ellipse(xy[1] + 3, xy[0] - 3, 6, 6);
                  p.stroked = false;
                  p.filled = true;
                  return p;
                  };
                
                
                  function getBounds(mySelection){
                  var tempArr = [];
                  for (var i = 0; i < mySelection.length; i++) {
                  tempArr.push(mySelection[i]);
                  };
                  var selBounds = [
                  tempArr.slice(0,).sort(function(a,b){
                  return b.visibleBounds[0] < a.visibleBounds[0];
                  })[0].visibleBounds[0],
                  tempArr.slice(0,).sort(function(a,b){
                  return a.visibleBounds[1] < b.visibleBounds[1];
                  })[0].visibleBounds[1],
                  tempArr.slice(0,).sort(function(a,b){
                  return b.visibleBounds[2] > a.visibleBounds[2];
                  })[0].visibleBounds[2],
                  tempArr.slice(0,).sort(function(a,b){
                  return a.visibleBounds[3] > b.visibleBounds[3];
                  })[0].visibleBounds[3]
                  ];
                  return selBounds;
                  };
                  if(app.documents.length == 0){
                  return;
                  }
                
                
                  var doc = app.activeDocument;
                
                
                  if(doc.selection.length == 0){
                  return;
                  }
                  doc.rulerOrigin = [0, 0];
                  var sel = doc.selection;
                  var selBounds = getBounds(sel);
                
                
                  /* TEST
                  marker([selBounds[0], selBounds[1]]);
                  marker([selBounds[0], selBounds[3]]);
                  marker([selBounds[2], selBounds[1]]);
                  marker([selBounds[2], selBounds[3]]);
                  */
                
                
                  var selWidth = (selBounds[2] - selBounds[0]);
                  var selHeight = (selBounds[1] - selBounds[3]);
                
                
                  var center = [
                  selBounds[0] + (selWidth / 2),
                  selBounds[1] - (selHeight / 2)
                  ];
                
                
                  /* TEST
                  marker([center[0], center[1]]);
                  */
                
                
                  var scaleRatio = ((selWidth + scaleUpBy) / selWidth) * 100;
                
                
                  doc.rulerOrigin = [center[0], center[1]];
                
                
                  for (var i = 0; i < sel.length; i++) {
                  sel[i].resize(scaleRatio, scaleRatio, true, true, true, true, 100, Transformation.DOCUMENTORIGIN);
                  };
                
                
                  doc.rulerOrigin = [0, 0];
                
                
                };
                ScaleSelection();
                
                • 5. Re: How to scale all selected objects by 1 inch without grouping
                  Silly-V Adobe Community Professional

                  This code has been working for me for this purpose for a long time now without detrimental effects, therefore I'm going to mark my own answer as 'correct' on this one.