Copy link to clipboard
Copied
I am looking for some help on trying to make an object the exact size of the artboard. This is something I do on a daily basis for several different reasons and it would be very helpful if this can happen automatically for whatever size the artboard may be. As I understand it the only way is with a script but I have no experience with making illustrator scripts, im definately no programmer. I have set up quickkeys in the past to copy from the artboard inputs when you are on the artboard tool but these round to the nearest .01 and this is not accurate enough for what I am working with. Also if I do this with multiple pages open illustrator is very slow to respond to the artboard tool. If anyone has any idea where to start or has seen other such scripts I would greatly appreciate it. Thank you.
Below is a script that I saw on here that I believe may contain what I need but now knowing programming I have no idea where to start on editing. All I need is the part where an object is placed on the artboard that is the exact same size as the artboard. If anyone can advise on editing I would greatly apprecaite it.
#target illustrator
function main() {
if (app.documents.length == 0) {
alert('Open a document before running this script');
return; // Stop script here no doc open…
} else {
var docRef = app.activeDocument;
with (docRef) {
if (selection.length == 0) {
alert('No items are selected…');
return; // Stop script here with no selection…
}
if (selection.length > 1) {
alert('Too many items are selected…');
return; // Stop script here with selection Array…
} else {
var selVB = selection[0].visibleBounds;
var rectTop = selVB[1] + 36;
var rectLeft = selVB[0] - 36;
var rectWidth = (selVB[2] - selVB[0]) + 72;
var rectHeight = (selVB[1] - selVB[3]) + 72;
selection[0].parent.name = 'CC';
selection[0].filled = false;
selection[0].stroked = true;
var ccColor = cmykColor(0, 100, 0, 0);
var ccCol = spots.add()
ccCol.name = 'CC';
ccCol.color = ccColor;
ccCol.tint = 100;
ccCol.colorType = ColorModel.SPOT;
var cc = new SpotColor();
cc.spot = ccCol;
selection[0].strokeColor = cc;
selection[0].strokeWidth = 1;
var tcLayer = layers.add();
tcLayer.name = 'TC';
var padBox = pathItems.rectangle(rectTop, rectLeft, rectWidth, rectHeight, false);
padBox.stroked = false;
padBox.filled = true;
var tcColor = cmykColor(0, 100, 90, 0);
var tcCol = spots.add()
tcCol.name = 'TC';
tcCol.color = tcColor;
tcCol.tint = 100;
tcCol.colorType = ColorModel.SPOT;
var tc = new SpotColor();
tc.spot = tcCol;
padBox.fillColor = tc;
padBox.move(docRef, ElementPlacement.PLACEATEND);
artboards[0].artboardRect = (padBox.visibleBounds);
redraw();
rectWidth = (rectWidth-72)/72;
rectWidth = roundToDP(rectWidth,1);
rectHeight = (rectHeight-72)/72;
rectHeight = roundToDP(rectHeight,1);
var textString = rectWidth + ' x ' + rectHeight;
prompt('Copy Me', textString);
}
}
}
}
main();
function roundToDP(nbr, dP) {
dpNbr = Math.round(nbr*Math.pow(10,dP))/Math.pow(10,dP);
return dpNbr;
}
function cmykColor(c, m, y, k) {
var newCMYK = new CMYKColor();
newCMYK.cyan = c;
newCMYK.magenta = m;
newCMYK.yellow = y;
newCMYK.black = k;
return newCMYK;
}
here you go, select one object before running, if your art has more than one object, make a group first.
...#target Illustrator
// script.name = fitObjectToArtboardBounds.jsx;
// script.description = resizes selected object to fit exactly to Active Artboard Bounds;
// script.required = select ONE object before running; CS4 & CS5 Only.
// script.parent = carlos canto // 01/25/12;
// script.elegant = false;
var idoc = app.activeDocument;
selec = idoc.selection;
if (selec.length==1)
{
Copy link to clipboard
Copied
is the object proportional to the artboard? if it is not, then do you need your object to be proportional? or can it be distorted to fit the artboard?
Copy link to clipboard
Copied
Object can be distorted. I want it to be the exact size of the artboard in both dimensions so if i say have a 4x5 artboard then I want my object to be 4x5. I currently plan on creating a new object each time I use this as Im essentially making a border for my artwork that fits inside of the artboard. Thank you for any help you can provide.
Copy link to clipboard
Copied
here you go, select one object before running, if your art has more than one object, make a group first.
#target Illustrator
// script.name = fitObjectToArtboardBounds.jsx;
// script.description = resizes selected object to fit exactly to Active Artboard Bounds;
// script.required = select ONE object before running; CS4 & CS5 Only.
// script.parent = carlos canto // 01/25/12;
// script.elegant = false;
var idoc = app.activeDocument;
selec = idoc.selection;
if (selec.length==1)
{
// get document bounds
var docw = idoc.width;
var doch = idoc.height;
var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
docLeft = activeAB.artboardRect[0];
docTop = activeAB.artboardRect[1];
// get selection bounds
var sel = idoc.selection[0];
var selVB = sel.visibleBounds;
var selVw = selVB[2]-selVB[0];
var selVh = selVB[1]-selVB[3];
var selGB = sel.geometricBounds;
var selGw = selGB[2]-selGB[0];
var selGh = selGB[1]-selGB[3];
// get the difference between Visible & Geometric Bounds
var deltaX = selVw-selGw;
var deltaY = selVh-selGh;
sel.width = docw-deltaX; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
sel.height = doch-deltaY;
sel.top = docTop; // Top is actually Visible top
sel.left = docLeft; // dito for Left
}
else
{
alert("select ONE object before running");
}
Copy link to clipboard
Copied
Ahh, very awesome. It works perfectly. I can't thank you enough for helping me out with this. I know a couple of people that will be very happy to have this. And this will help me out a lot. Finally I can get the detail that is needed by automation. As much as I use quickeys and Apple's automator I really do need to learn how to write scripts. This just shows how much more powerful and elegant they can be. And once again, thank you very much.
Copy link to clipboard
Copied
you're welcome, glad to help
Copy link to clipboard
Copied
That's really great, thank you.
But my objects cannot be distorted. Is it possible to do that?
Thank you in advance.
Copy link to clipboard
Copied
it is possible, but if the art is not proportional to the artboard it will only fit either the width or the height
Copy link to clipboard
Copied
HI,
@Carlos - Great work.
Maybe you can help me a little more, I have a few artboards for example A5 A4 A3 ... , on every of them I have one visible object, I want to scale all at once to the sizes of artboards.
Your script works great but I have almost 100 artboards and using script 100 times is killer for me .
I think something like loop for every visible object could help but I'm not a programmer.
Thanks
Copy link to clipboard
Copied
Stan, if you have CS5 it might be kind of easy, if you have CS4 it might be kind of hard.
Copy link to clipboard
Copied
Thanks to CarlosCanto for the original script, it was very a very helpful starting point to optimize one of our workflows. We customized it a bit to maintain the aspect ratio (just takes the greater of the width / height and scales proportionally to the artboard size), and also center up the object in the middle of the artboard once scaling is complete. I hope it is helpful to someone:
#target Illustrator
// script.name = fitObjectToArtboardBounds.jsx;
// script.description = resizes selected object to fit exactly to Active Artboard Bounds;
// script.required = select ONE object before running; CS4 & CS5 Only.
// script.parent = carlos canto // 01/25/12;
// script.elegant = false;
var idoc = app.activeDocument;
selec = idoc.selection;
if (selec.length==1)
{
// get document bounds
var docw = idoc.width;
var doch = idoc.height;
var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
docLeft = activeAB.artboardRect[0];
docTop = activeAB.artboardRect[1];
// get selection bounds
var sel = idoc.selection[0];
var selVB = sel.visibleBounds;
var selVw = selVB[2]-selVB[0];
var selVh = selVB[1]-selVB[3];
var selGB = sel.geometricBounds;
var selGw = selGB[2]-selGB[0];
var selGh = selGB[1]-selGB[3];
// get the difference between Visible & Geometric Bounds
var deltaX = selVw-selGw;
var deltaY = selVh-selGh;
if (sel.width > sel.height) {
var newWidth = docw-deltaX;
var ratio = sel.width / newWidth;
sel.width = newWidth; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
sel.height = sel.height * ratio;
} else {
var newHeight = doch-deltaY;
var ratio = sel.height / newHeight;
sel.height = newHeight;
sel.width = sel.width / ratio;
}
sel.top = (doch / 2) - (sel.height / 2);
sel.left = (docw / 2) - (sel.width / 2);
}
else
{
alert("select ONE object before running");
}
Copy link to clipboard
Copied
you're welcome db
Copy link to clipboard
Copied
Carlos, I have CS5.
Copy link to clipboard
Copied
Hi Stan, I posted version 2 of this script over the regular forum. I does what you need.
Copy link to clipboard
Copied
Great scripts. Thanks.
But i am looking for fit object to artboard PROPORTIONAL to the max. size of the artboard.
- Create new artboard size value X by Y or Change artboard size to value X by Y (value edit in script or popup field)
- Select all objects
- Scale object to max. arboard size (proportional)
Option:
- Rotate 90dec.
example:
artboard is 400x600
object is 100x100
new object is 400x400
example 2:
artboard is 400x600
object is 200x100
new object is 400x200
rotated opbject is 100x200 new rotated opject is 300x600
Copy link to clipboard
Copied
ok, let me work on it...
Copy link to clipboard
Copied
Ok thanks i waiting for it...
greetz
Copy link to clipboard
Copied
...stay tuned, I'll work on it as soon as I have a chance...I haven't forgotten you.
Copy link to clipboard
Copied
I have been using the script as well. But I too was looking for something proportional. I tried digging into the code to see if I could figure out the math, but it is a little beyond my capabilities. Digisli, CarlosCanto, did you find a solution?
Copy link to clipboard
Copied
I have an incling that it would have to follow:
// find landscape or portrait (width - margins > height - margins = landscape) (Width - margin < Height -margin = portrait)
// if portrait scale to height - margins with aspect ratio to desired height
// if landscape scale to width - margins with aspect ratio
// center object
Copy link to clipboard
Copied
Okay, CarlosCanto, I messed with your script, please correct me if I am wrong but this seems to maintain aspect ratio, and leave the margins (defaulted to 10)
Copy link to clipboard
Copied
no problem messing with my script, it works ok with objects taller than wider, check with objects wider than taller
Copy link to clipboard
Copied
Hello,
This is a very interesting topic for me. Is there any way to modify the script to fill areas other than squares or rectangles? I've been able to scale artwork to this shape based on percentages of width/height but only if the artwork is square or rectangular. I've never been able to account for "round" artwork because of the control handles vs. visible art. Is there some way to account for the decreasing area in this shape when the artwork is a circle?
Copy link to clipboard
Copied
Sorry to resurrect this however I am looking to add one step before auto resizing the art to the artboard which is to specify the artboard size. I have a bunch of art that I need to specify the artboard to be exactly 10cmx10cm prior to resizing the art to the artboard. How do i execute this from a Javascript? I found a few posts that show how to expand the artboard size slightly but not how to specify exact dimensions to resize to.
Thanks!
Copy link to clipboard
Copied
Question re: this thread... Can you take the elements of this script (resizing Proportionally) and instead of doing it by the size of the artboard... Make it a set size.. (ie: if Larger than 7"w x 5"h) Scale down to fit Proportionally to which ever direction will fit?? - Thanks