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

Scale Selected Item Proportionately

Explorer ,
Dec 02, 2016 Dec 02, 2016

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

2.4K

Translate

Translate

Report

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

Enthusiast , Dec 09, 2016 Dec 09, 2016

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});

Votes

Translate

Translate
Adobe
Valorous Hero ,
Dec 02, 2016 Dec 02, 2016

Copy link to clipboard

Copied

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
}

Votes

Translate

Translate

Report

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
Explorer ,
Dec 02, 2016 Dec 02, 2016

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

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
Valorous Hero ,
Dec 02, 2016 Dec 02, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Valorous Hero ,
Dec 02, 2016 Dec 02, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Dec 06, 2016 Dec 06, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Dec 06, 2016 Dec 06, 2016

Copy link to clipboard

Copied

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

  }; 

  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.resize(scaleRatio, scaleRatio, true, true, true, true, 100, Transformation.DOCUMENTORIGIN); 

  }; 

 

  doc.rulerOrigin = [0, 0]; 

 

}; 

ScaleSelection(); 

Votes

Translate

Translate

Report

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 ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

try this:

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

    if(sel.height >= 504 || sel.width >= 504)

    {

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

    }

  

  };

Votes

Translate

Translate

Report

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
Explorer ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

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

  };

  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.height >= 504 || sel.width >= 504)

    {

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

    }

 

  };

ScaleSelection();

Votes

Translate

Translate

Report

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 ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Valorous Hero ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 Beginner ,
Jul 14, 2022 Jul 14, 2022

Copy link to clipboard

Copied

LATEST

I actually had to use a mix of yours and this for the desired effect, thanks both! 

 

   // create a landing zone square to place icon inside
   let getArtLayer = sourceDoc.layers.getByName('Art');
   let landingZoneSquare = getArtLayer.pathItems.rectangle(
      -844,
      571,
      460,
      460);
   let setLandingZoneSquareColor = new RGBColor();
   setLandingZoneSquareColor.red = 12;
   setLandingZoneSquareColor.green = 28;
   setLandingZoneSquareColor.blue = 151;
   landingZoneSquare.filled = true;
   landingZoneSquare.fillColor = setLandingZoneSquareColor;
   landingZoneSquare.name = "LandingZone"
   /*@ts-ignore*/
   landingZoneSquare.move(getArtLayer, ElementPlacement.PLACEATEND);

   function placeIconMasthead1Correctly(mastBannerIconOnText, maxSize) {


      // start moving expressive icon into our new square
      var placedmastBannerIconOnText = mastBannerIconOnText;
      var landingZone = sourceDoc.pathItems.getByName("LandingZone");
      var preferredWidth = (230);
      var preferredHeight = (460);
      // do the width
      var widthRatio = (preferredWidth / placedmastBannerIconOnText.width) * 100;
      if (placedmastBannerIconOnText.width != preferredWidth) {
         placedmastBannerIconOnText.resize(widthRatio, widthRatio);
      }
      // now do the height
      var heightRatio = (preferredHeight / placedmastBannerIconOnText.height) * 100;
      if (placedmastBannerIconOnText.height != preferredHeight) {
         placedmastBannerIconOnText.resize(heightRatio, heightRatio);
      }
      // now let's center the art on the landing zone
      var centerArt = [placedmastBannerIconOnText.left + (placedmastBannerIconOnText.width / 2), placedmastBannerIconOnText.top + (placedmastBannerIconOnText.height / 2)];
      var centerLz = [landingZone.left + (landingZone.width / 2), landingZone.top + (landingZone.height / 2)];
      placedmastBannerIconOnText.translate(centerLz[0] - centerArt[0], centerLz[1] - centerArt[1]);

      // need another centered proportioning to fix it exactly in correct position
      var W = mastBannerIconOnText.width,
         H = mastBannerIconOnText.height,
         MW = maxSize.W,
         MH = maxSize.H,
         factor = W / H > MW / MH ? MW / W * 100 : MH / H * 100;
      mastBannerIconOnText.resize(factor, factor);

   }

   placeIconMasthead1Correctly(mastBannerIconOnText, { W: 460, H: 460 });

   // delete the landing zone
   // let docSelected = sourceDoc.layers.getByName('Art').groupItems.getByName("LandingZone");
   // docSelected.remove();

Votes

Translate

Translate

Report

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
Enthusiast ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

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});

Votes

Translate

Translate

Report

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