Skip to main content
Inspiring
October 26, 2022
Answered

JavaScript to resize a selected object in Illustrator!

  • October 26, 2022
  • 1 reply
  • 3353 views

Hello Everyone!

 

I need your help with a JavaSrcript that can resize a selected object (frame/image/lineart etc) in a calculated way i.e.
a) Width of the selected object should become equal to the width of the Document/Artboard.
b) Height should become [sqrt(documentWidth * documentHeight) * 0.08]

 

Thanks in advance.

Masood

Correct answer femkeblanco
app.selection[0].width = app.activeDocument.width;
app.selection[0].height = Math.sqrt(app.activeDocument.width * app.activeDocument.height) * 0.08;

1 reply

femkeblanco
femkeblancoCorrect answer
Legend
October 26, 2022
app.selection[0].width = app.activeDocument.width;
app.selection[0].height = Math.sqrt(app.activeDocument.width * app.activeDocument.height) * 0.08;
Inspiring
October 26, 2022

Thanks, a lot, very quick, much appreciated.

The code is working fine on empty frames, but not on the ones with clipping masks. Can you please check this.

Inspiring
October 27, 2022

Thanks, for explaining things so nicely. Much appreciated.

The reason behind calculating object is based on brand guidelines. The frames or boxes needs to have a specific width and height. Width in most cases, remains the same which is equal to the width of the Artboard. However, the image(s) in these frames needs to be filled proportionally, and can be cropped to accordingly. So, we're not stretching/distorting the images, rather cropping it.

Thanks once again.


Correcting myself: If you manually select a clipping path plus the placed/raster item it clips, it does appear in `selection` as a single GroupItem. (AI is smart enough to recognize you want them treated together. It’s when you select non-clipping groups that `selection` unhelpfully gives you all the individual paths instead.)

 

With that in mind, what you want is to set the clipping path’s width and height to your desired dimensions, then proportionally scale and reposition the image so that it completely fills that clipping path with any excess width/height cropped. The following code should handle that one particular use-case:

 

var doc = app.activeDocument;

var artboard = doc.artboards[0];
var rect = artboard.artboardRect;
var artboardWidth = rect[2] - rect[0];

var sel = doc.selection;


function calculateClippingPathHeight(width) {
  return width * 0.8; // use whatever calculation you want here
}

function recenterItemInBounds(item, bounds) {
  var p = item.position;
  p[0] = bounds[0] - (item.width - (bounds[2] - bounds[0])) / 2;
  p[1] = bounds[1] + (item.height - (bounds[1] - bounds[3])) / 2;
  item.position = p;
}

// assume a clipping path with a single placed/raster item behind it
if (sel.length === 1 && sel[0].typename === 'GroupItem' && 
    sel[0].pageItems.length === 2 && sel[0].pageItems[0].clipping) {
	
  var group = sel[0];
  var clip = group.pageItems[0];
  var image = group.pageItems[1];
  
  // set the desired width and height of the clipping path
  clip.width = artboardWidth;
  clip.height = calculateClippingPathHeight(clip.width);
  
  // calculate proportional scale factor for image so that it fills the clipping path 
  var scale = Math.max(clip.width / image.width, clip.height / image.height) * 100;
  image.resize(scale, scale);
  
  // recenter the image in the clipping path
  recenterItemInBounds(image, clip.geometricBounds);
  
  // recenter the group on the artboard
  recenterItemInBounds(group, artboard.artboardRect);
  
} else {
	alert("Selection is not a clipped image.");
}

 

 Developing a more general-purpose script which can process any number and combination of selected items, position items according to different rules, etc is left as an exercise to the reader.