Skip to main content
Participant
January 24, 2012
Answered

Script for making an object the artboard size.

  • January 24, 2012
  • 4 replies
  • 43645 views

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;
}

This topic has been closed for replies.
Correct answer CarlosCanto

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");

          }

4 replies

Known Participant
May 24, 2019

Object with clipping mask

This script will get an error

There is no correct equal to the size of the artboa

stevem89033964
Participant
December 4, 2016

This stuff doesn't work for me. Maybe it's because I'm using CS2. I wish someting worked for me.

Babymac08
Known Participant
December 2, 2016

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

CarlosCanto
Community Expert
Community Expert
January 24, 2012

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?

Participant
January 24, 2012

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.

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
January 25, 2012

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");

          }