11 Replies Latest reply on Dec 9, 2016 4:33 PM by moluapple

    Scale Selected Item Proportionately

    Babymac08 Level 1

      Looking for a script that would scale a selected item that's bigger than 7"w x 5"h Proportionately..

       

      I've found several samples online but can't seem to get any to work or it's part of a longer process... I just need the simple If object is bigger than (") x (") then scale

       

      Thanks in advance for any help or direction....

        • 1. Re: Scale Selected Item Proportionately
          Silly-V Adobe Community Professional

          A way to do this is to make sure your item is in a group, then use the .resize() function to scale it.

          In my example, you can see I've grouped some shapes and I'm using the resize() command to increase the size of one of them 150%;

           

          This is a great learning example, btw - just to scale something is a very basic thing you can do to learn a lot of code principles.

           

          #target illustrator

          function test(){

            var art = app.activeDocument.groupItems[0];

            art.resize(150, 150);

          };

          test();

           

          2016-12-02 13_49_32-.png

          2016-12-02 13_49_24-Untitled-2_ @ 100% (CMYK_Preview).png

           

          Other things to know - the Illustrator scripting unit of measure for scripting is in points which are 72 to 1 inch.

          If you want to see if something is bigger than 7 inches, you need to make sure to multiply the stuff by 72 accordingly.

          Here's an example of a statement for width:

           

          if(art.width > (72 * 7)){
               // find your ratio of the art vs what the art needs to be and use that ratio in the resize() function
          }

          • 2. Re: Scale Selected Item Proportionately
            Babymac08 Level 1

            I've found thing similiar to this... but what I need is the factor of it's it's bigger than a specific dimension H or W then I need to size it to my max set size... A general Blanket percentage may not fit the bill...

             

            (ie: if I'm laying out a flyer with several sponsor logos that come at different sizes... I need to make them all the same basic height or width) so I would manually select the item then run the script so in the end everything is the same size)

            • 3. Re: Scale Selected Item Proportionately
              Silly-V Adobe Community Professional

              That's why you find out your proportion ratio.

              Let's say for example your art is 8.5" wide and you want it to be 7" wide.

              So you divide 7 by 8.5 and get 0.82 (*100) and you get 82% - put that into your resize() function and your art should be now at 7" because it's been resized to 82% of the original art.

              Then if it's taller than your height requirement, do same for the height and you're set.

              • 4. Re: Scale Selected Item Proportionately
                Silly-V Adobe Community Professional

                Soon you are going to be one of US and there shall be no turning back.

                • 5. Re: Scale Selected Item Proportionately
                  Babymac08 Level 1

                  OK... I've struggled with this all weekend... I think I've figured out the formula for doing the math... but I don't know how to write the results into my scale factor... Just for the sake of testing I set my max width to 72 which should equal 1" and I'm asking it to check size of selection and if larger than 72 get my percentage... but obviously something isn't correct because I'm not getting results...

                   

                  Can you please see what's missing or incorrect..

                   

                  #target illustrator

                  var MAX_WIDTH = 72;

                  var art = app.activeDocument.groupItems[0];

                  var percentageX = Math.round((MAX_WIDTH/W) * 100);

                     art.resize(percentageX);

                  • 6. Re: Scale Selected Item Proportionately
                    Babymac08 Level 1

                    Silly-V

                     

                    I found a script that you had done for someone else... and I've got it to do almost exactly what I wanted... However... I just need to add a qualifier that if the image is already under 7" (504 pt) to do nothing...  Can you assist please...

                     

                    #target illustrator-19 

                    function  ScaleSelection() { 

                     

                     

                    var scaleProp = 504; 

                     

                      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 = ((scaleProp / 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(); 

                    • 7. Re: Scale Selected Item Proportionately
                      williamadowling Level 4

                      try this:

                        for (var i = 0; i < sel.length; i++) {

                       

                          if(sel[i].height >= 504 || sel[i].width >= 504)

                          {

                            sel[i].resize(scaleRatio, scaleRatio, true, true, true, true, 100, Transformation.DOCUMENTORIGIN);

                          }

                        

                        };

                      • 8. Re: Scale Selected Item Proportionately
                        Babymac08 Level 1

                        William,

                         

                        I'm assuming your replacing the lines toward the end of the original script with this... If so I was not able to get it to work successfully... Can you please look below at where I pasted and let me know if this is correct or what's missing.. - Thanks so much...

                         

                        #target illustrator-19

                        function  ScaleSelection() {

                         

                         

                        var scaleProp = 504;

                         

                          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 = ((scaleProp / selWidth) ) *100;

                         

                          doc.rulerOrigin = [center[0], center[1]];

                         

                        for (var i = 0; i < sel.length; i++) {

                         

                            if(sel[i].height >= 504 || sel[i].width >= 504)

                            {

                              sel[i].resize(scaleRatio, scaleRatio, true, true, true, true, 100, Transformation.DOCUMENTORIGIN);

                            }

                         

                          };

                        ScaleSelection();

                        • 9. Re: Scale Selected Item Proportionately
                          williamadowling Level 4

                          Are you getting an error? If so, what does the error say?

                           

                          I didn't test this code because I don't know what conditions the script is looking for in the file and didn't have time to sit down and figure it out. But that if statement should filter out any items that are less than 504pt in height or width and perform the resize function on any items that are more than 504pt in either dimension..

                           

                          I need to know more about the failure. Did it do something you didn't want it to do? did it NOT do something you DID want it to do? What was the error message, etc.

                           

                          It would also help if you could provide a sample file so I can test it as well and see where it might be failing.

                          • 10. Re: Scale Selected Item Proportionately
                            Silly-V Adobe Community Professional

                            I wouldn't mess with that scale-selection code, it's mostly for situations where you're not allowed to group things into one group.

                             

                            Here is my example that you can use, with accompanying screenshots.

                             

                            #target illustrator
                            function test(){
                              var doc = app.activeDocument;
                              var art = doc.groupItems.getByName("MyArt");
                              var landingZone = doc.pathItems.getByName("LandingZone");
                            
                            
                              var preferredWidth = (7 * 72);
                              var preferredHeight = (5 * 72);
                            
                            
                              // do the width
                              var widthRatio = (preferredWidth / art.width) * 100;
                              if(art.width != preferredWidth){
                              art.resize(widthRatio, widthRatio);
                              }
                            
                            
                              // now do the height
                              var heightRatio = (preferredHeight / art.height) * 100;
                              if(art.height != preferredHeight){
                              art.resize(heightRatio, heightRatio);
                              }
                            
                            
                              // now let's center the art on the landing zone
                              var centerArt = [art.left + (art.width / 2), art.top + (art.height / 2)];
                              var centerLz = [landingZone.left + (landingZone.width / 2), landingZone.top + (landingZone.height / 2)];
                            
                            
                              art.translate(centerLz[0] - centerArt[0], centerLz[1] - centerArt[1]);
                            
                            };
                            test();
                            

                             

                            Screen Shot 2016-12-08 at 8.02.29 PM.png

                            Before

                             

                            Screen Shot 2016-12-08 at 8.02.48 PM.png

                            After

                            • 11. Re: Scale Selected Item Proportionately
                              moluapple Level 4

                              Please select one item large than 7"*5" and try this code. You can set maxSize to whatever you want.

                              function scaleDownProportionally(item, maxSize) {
                                  var W = item.width,
                                      H = item.height,
                                      MW = maxSize.W,
                                      MH = maxSize.H,
                                      factor = W / H > MW / MH ? MW / W * 100 : MH / H * 100;
                              
                                  if (W > MW || H > MH) {
                                      item.resize(factor, factor);
                                  }
                              };
                              
                              scaleDownProportionally(app.selection[0], {W: 7 * 72, H: 5 * 72});