Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Adobe Illustrator script to resize objects proportionally to fit inside the artboard.
NOT MAINTAINED! from ~2014
// cs4+ script for resizing objects proportionally to fit inside artboard
// based on:
// usage: create a new document with desired artboard size, paste object, select it, run this script
// bugs: centering does not work after changing artboard size
var activeDoc = app.activeDocument
var selection = activeDoc.selection
// check if anything is selected
if (selection.length > 0) {
for (var i = 0; i < selection.length; i++) {
var item = selection[i].duplicate()
var abActive =
var abProps = getArtboardBounds(abActive)
var boundsDiff = itemBoundsDiff(selection[i])
fitItem(item, abProps, boundsDiff) // scale object to fit artboard
} else {
alert('Select an object before running this script')
function getArtboardBounds(artboard) {
var bounds = artboard.artboardRect
var left = bounds[0]
var top = bounds[1]
var right = bounds[2]
var bottom = bounds[3]
var width = right - left
var height = top - bottom
var props = { left: left, top: top, width: width, height: height }
return props
function itemBoundsDiff(item) {
var itemVB = item.visibleBounds
var itemVW = itemVB[2] - itemVB[0] // right - left
var itemVH = itemVB[1] - itemVB[3] // top - bottom
var itemGB = item.geometricBounds
var itemGW = itemGB[2] - itemGB[0] // right - left
var itemGH = itemGB[1] - itemGB[3] // top - bottom
var deltaX = itemVW - itemGW
var deltaY = itemVH - itemGH
var diff = { deltaX: deltaX, deltaY: deltaY }
return diff
function fitItem(item, props, diff) {
var oldWidth = item.width
var oldHeight = item.height
if (item.width > item.height) {
// landscape, scale height using ratio from width
item.width = props.width - diff.deltaX
var ratioW = item.width / oldWidth
item.height = oldHeight * ratioW
} else {
// portrait, scale width using ratio from height
item.height = props.height - diff.deltaY
var ratioH = item.height / oldHeight
item.width = oldWidth * ratioH
// center = 0 - (props.height / 2 - item.height / 2)
item.left = props.width / 2 - item.width / 2
// deselect
item.selected = false
Copy link

ruandre commented Oct 20, 2014

This script does the opposite of what "Fit to Artwork Bounds" does

Instead of changing the Artboard, it scales the selected object proportionally to fit inside it (Artboard size remains the same)

It's useful for resizing objects with different dimensions to fit a specific size (for icon sets, game assets/sprites, etc.)

Here's an example

Copy link

petrchutny commented Feb 21, 2015

Thanks for making this, initially I thought it doesn't work, but then noticed that your script moves the copied artwork somewhere in my first artboard, which I don't have selected and therefore am not focused on it.

Copy link

Citizenprime commented Feb 28, 2015

Same here, selected the object - fits perfect the size but not in the middle of the artboard. Any trick to solve this? Very nice script because we really need it to make paint-decals for a new game.
Thank you for the script!

Copy link

ellockie commented Sep 3, 2015

Very useful, thanks! Btw., the fact that it's not working in CS3 isn't it simply due to the .jsx extension? Somewhere I saw recommendation to change the extension from .jsx to .js fo CS, but I haven't tried that yet, as I use later version.

Copy link

aholub commented Feb 3, 2016

Hi Ruandre. Thanks for doing this. There seems to be a bug in CC2016, though. It scales the artwork just fine, but it doesn't erase the original art, so you end up with both the original and scaled images all mixed up with each other.
screen shot 2016-02-03 at 1 06 10 pm

Copy link

hilukasz commented Nov 7, 2016

@aholub just this at the end:

var docSelected = app.activeDocument.selection; for (j=0; j<docSelected.length; j++) docSelected[j].remove();

Copy link

shivendra14 commented Sep 13, 2017

Check this out:

modified this to scale artwork and artboard :)

Copy link

shivendra14 commented Sep 17, 2017

Copy link

Ramalingamspi commented Apr 27, 2018

Hi Your script works great but i need to scale the object with scale & stroke effects on. Is it possible, please help me

Copy link

visusys commented Jul 19, 2022

This script is almost there... on Illustrator CC it doesn't place the resized object in its parent artboard. I'm an amateur at ExtendScript - hoping for a fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment