Skip to content

Instantly share code, notes, and snippets.

@iconifyit
Last active December 17, 2020 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iconifyit/324f7406caa7a64875dc814d2cb0e430 to your computer and use it in GitHub Desktop.
Save iconifyit/324f7406caa7a64875dc814d2cb0e430 to your computer and use it in GitHub Desktop.
Adobe Illustrator Script - Resize selected items
/**
* Usage : resizeSelectedItems(getUserInput())
*
* =========
* CAUTION : This script will scale all selected items. It cannot determine if an object is simple or compound.
* ========= It is best to group compound objects made up of multiple PageItems.
*
* `getUserInput` is separated so you can easily use the main function, `resizeSelectedItems`,
* in your code. Make sure the input to `resizeSelectedItems` is a string indicating the
* new size. The options are:
*
* Percentage : Scale to a percentage of the current size entered as integer + '%' (150%, 75%, etc).
* Max Dimension : Entered as an integer indicating the maximum size of width or height depending on orientation (landscape or portrait).
* Fixed-width : Sets the width of all selected items to the same size. Entered as an integer followed by a `w` - `1024w`, '2048w`, etc.
* Fixed-height : Sets the height of all selected items to the same size. Entered as an integer followed by a `h` - `1024h`, '2048h`, etc.
*/
function getUserInput() {
var doc = activeDocument;
var sel = doc.selection;
if (sel.length === 0) {
alert('No items selected');
return;
}
var input = prompt(
'Enter scale as percentage or ' +
'Max dimension (single integer, in pixels)',
'100%'
);
return input;
}
function resizeSelectedItems(input) {
var doc = activeDocument;
var sel = doc.selection;
var percent,
targetdim,
decimal,
maxwidth,
maxheight,
width,
height;
// Scale to percentage
if (input.indexOf('%') > 0) {
percent = parseInt(input) / 100;
}
// Scale all to exact width
else if (input.toLowerCase().indexOf('w') >= 0) {
targetwidth = parseInt(input);
targetheight = 0;
targetdim = 0;
}
// Scale all to exact height
else if (input.toLowerCase().indexOf('h') >= 0) {
targetheight = parseInt(input);
targetwidth = 0;
targetdim = 0;
}
// Scale to max targert dimension (width or height)
else if (! isNaN(input)) {
targetdim = input;
targetwidth = 0;
targetheight = 0;
}
try {
var oldUnits = String(activeDocument.rulerUnits).split('.').pop();
activeDocument.rulerUnits = RulerUnits.Pixels;
function scaleToTargetWidth(item, targetwidth) {
item.height = item.height * (targetwidth / item.width);
item.width = targetwidth;
}
function scaleToTargetHeight(item, targetheight) {
item.width = item.width * (targetheight / item.height);
item.height = targetheight;
}
function scaleToTargetDim(item, targetdim) {
// Get the scale ratio
var maxdim = Math.max(item.width, item.height);
var ratio = targetdim / maxdim;
// Square
if (item.width === item.height) {
item.width = targetdim;
item.height = targetdim;
}
// Landscape
else if (item.width > item.height) {
item.width = targetdim;
item.height = item.height * ratio;
}
// Portrait
else {
item.height = targetdim;
item.width = item.width * ratio;
}
}
for (var i = 0; i < sel.length; i++) {
var item = sel[i];
if (! isNaN(percent)) {
item.width = item.width * percent;
item.height = item.height * percent;
}
else if (targetwidth > 0) {
scaleToTargetWidth(item, targetwidth);
}
else if (targetheight > 0) {
scaleToTargetHeight(item, targetheight);
}
else if (targetdim > 0) {
scaleToTargetDim(item, targetdim);
}
}
activeDocument.rulerUnits = RulerUnits[oldUnits];
}
catch(e) {
alert(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment