Last active
July 19, 2022 12:16
-
-
Save ruandre/7b47cbf2a4c55dac9adb to your computer and use it in GitHub Desktop.
Adobe Illustrator script to resize objects proportionally to fit inside the artboard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************** | |
NOT MAINTAINED! from ~2014 | |
****************************/ | |
// cs4+ script for resizing objects proportionally to fit inside artboard | |
// based on: https://forums.adobe.com/message/4164590 | |
// 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 = | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!