Skip to main content
Babymac08
Known Participant
December 2, 2016
解決済み

Scale Selected Item Proportionately

  • December 2, 2016
  • 返信数 6.
  • 3199 ビュー

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

このトピックへの返信は締め切られました。
解決に役立った回答 moluapple

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

返信数 6

moluapple解決!
Inspiring
December 10, 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});

Babymac08
Babymac08作成者
Known Participant
December 8, 2016

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

Disposition_Dev
Legend
December 8, 2016

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.

Babymac08
Babymac08作成者
Known Participant
December 6, 2016

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

Disposition_Dev
Legend
December 8, 2016

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

    }

  

  };

Babymac08
Babymac08作成者
Known Participant
December 6, 2016

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

Babymac08
Babymac08作成者
Known Participant
December 2, 2016

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)

Silly-V
Legend
December 2, 2016

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.

Silly-V
Legend
December 2, 2016

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

Silly-V
Legend
December 2, 2016

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

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
}