Skip to content

Instantly share code, notes, and snippets.

@maxime1992
Forked from ruandre/AiFitToArtboard.jsx
Last active August 29, 2015 14:24
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 maxime1992/ca17ea9701b90d7f4643 to your computer and use it in GitHub Desktop.
Save maxime1992/ca17ea9701b90d7f4643 to your computer and use it in GitHub Desktop.
/*
License: GPLv2 or later
Adobe Illustrator script to resize objects proportionally to fit inside the artboard.
Works in CS6 (should work in later versions).
Doesn't work in CS3 (and probably won't work in earlier versions).
Based on the fine work of these folks:
Carlos Canto, dbburgess
https://forums.adobe.com/message/4164590
http://aiscripts.com/2012/09/fit-object-to-artboard/
How to use:
1) Create a new document with the desired artboard size.
2) Paste an object into this newly created document.
3) Select the object and run this script.
4) Profit.
Known bugs:
Centering is kinda buggy at times - it doesn't seem
to work after changing the artboard size, for example :(
Future versions, maybe:
- Just scale the object/don't duplicate?
- Modify the script to create a new document so that
it can be run from any file with an object selected?
*/
// Current document and selection:
var activeDoc = app.activeDocument,
selection = activeDoc.selection;
// Check if anything is selected:
if (selection.length > 0) {
// Loop over selected items:
for (i = 0; i < selection.length; i++) {
var item = selection[i].duplicate(),
abActive = activeDoc.artboards[
activeDoc.artboards.getActiveArtboardIndex()
],
abProps = getArtboardBounds(abActive),
boundsDiff = itemBoundsDiff(selection[i]);
// Scale object to fit artboard:
fitItem(item, abProps, boundsDiff);
}
} else {
alert("Select an object before running this script.");
}
// Artboard bounds helper (used above):
function getArtboardBounds(artboard) {
var bounds = artboard.artboardRect,
left = bounds[0],
top = bounds[1],
right = bounds[2],
bottom = bounds[3],
width = right - left,
height = top - bottom,
props = {
left : left,
top : top,
width : width,
height : height
};
return props;
}
// Bounds difference helper (used at the top):
function itemBoundsDiff(item) {
var itemVB = item.visibleBounds,
itemVW = itemVB[2] - itemVB[0],
itemVH = itemVB[1] - itemVB[3],
itemGB = item.geometricBounds,
itemGW = itemGB[2] - itemGB[0],
itemGH = itemGB[1] - itemGB[3],
deltaX = itemVW - itemGW,
deltaY = itemVH - itemGH,
diff = {
deltaX: deltaX,
deltaY: deltaY
};
return diff;
}
// Fit item helper (used at the top):
function fitItem(item, props, diff) {
// Cache current values:
var oldWidth = item.width; // alert(oldWidth);
var oldHeight = item.height; // alert(oldHeight);
// Wide or tall?
if (item.width > item.height) {
// alert('wide');
item.width = props.width - diff.deltaX;
// Scale height using ratio from width:
var ratioW = item.width / oldWidth;
item.height = oldHeight * ratioW;
} else {
// alert('tall');
item.height = props.height - diff.deltaY;
// Scale width using ratio from height:
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