/* | |
-------------------------------------------------- | |
NOTE: this is not maintained, and I haven't | |
tried it in a long time - but may work as a base | |
to modify for your needs i.e. ymmv | |
-------------------------------------------------- | |
Illustrator CS4+ script for resizing objects | |
proportionally so that they fit inside artboard | |
Based on: https://forums.adobe.com/message/4164590 | |
Usage: | |
1. Create new document with desired artboard size | |
2. Paste object into document, select it and run this script | |
Bugs: | |
Centering does not work after changing artboard size | |
Future versions: | |
- Scale original object, don't duplicate | |
*/ | |
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 = | |
activeDoc.artboards[activeDoc.artboards.getActiveArtboardIndex()] | |
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 | |
item.top = 0 - (props.height / 2 - item.height / 2) | |
item.left = props.width / 2 - item.width / 2 | |
// deselect | |
item.selected = false | |
} |
This comment has been minimized.
This comment has been minimized.
Sorry for not replying sooner Chris, I only saw this today. This script does the reverse of what "Fit to Artwork Bounds" does - instead of changing the artboard, it scales the selected object proportionally to fit perfectly inside of it instead. It's useful for resizing objects with wildly disparate dimensions to fit a specific size: |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@aholub just this at the end:
|
This comment has been minimized.
This comment has been minimized.
Check this out: modified this to scale artwork and artboard :) |
This comment has been minimized.
This comment has been minimized.
and this one to scale up assets: |
This comment has been minimized.
This comment has been minimized.
Hi Your script works great but i need to scale the object with scale & stroke effects on. Is it possible, please help me |
This comment has been minimized.
Is this different than using the command "Fit Artwork to Bounds"?