Skip navigation
kinderdm357
Currently Being Moderated

Script for making an object the artboard size.

Jan 24, 2012 9:45 AM

Tags: #size #script #artboards #object #illustrator_cs5 #illustrator_artboards

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

 
Replies
  • Currently Being Moderated
    Jan 24, 2012 2:03 PM   in reply to kinderdm357

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2012 12:43 AM   in reply to kinderdm357

    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");
              }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2012 10:08 AM   in reply to kinderdm357

    you're welcome, glad to help

     
    |
    Mark as:
  • Currently Being Moderated
    May 12, 2012 10:33 AM   in reply to CarlosCanto

    That's really great, thank you.

    But my objects cannot be distorted. Is it possible to do that?

     

    Thank you in advance.

     
    |
    Mark as:
  • Currently Being Moderated
    May 12, 2012 4:58 PM   in reply to felipelisboa

    it is possible, but if the art is not proportional to the artboard it will only fit either the width or the height

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 3:19 AM   in reply to CarlosCanto

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 2:55 PM   in reply to felipelisboa

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

            }

     

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2012 12:27 AM   in reply to Stan Kumak

    Stan, if you have CS5 it might be kind of easy, if you have CS4 it might be kind of hard.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2012 12:31 AM   in reply to dbburgess

    you're welcome db

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2012 9:50 AM   in reply to CarlosCanto

    Carlos, I have CS5.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 5, 2012 9:22 PM   in reply to Stan Kumak

    Hi Stan, I posted version 2 of this script over the regular forum. I does what you need.

     

    http://forums.adobe.com/thread/1044937

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 10, 2012 12:48 AM   in reply to CarlosCanto

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 11, 2012 8:41 AM   in reply to digisli

    ok, let me work on it...

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 16, 2012 1:06 AM   in reply to CarlosCanto

    Ok thanks i waiting for it...

    greetz

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 16, 2012 2:32 PM   in reply to digisli

    ...stay tuned, I'll work on it as soon as I have a chance...I haven't forgotten you.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 7, 2013 6:13 PM   in reply to CarlosCanto

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 7, 2013 6:26 PM   in reply to cjamesrun

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 7, 2013 7:50 PM   in reply to cjamesrun

    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)

     

    http://pastebin.com/AY7cvuQw

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 7, 2013 8:56 PM   in reply to cjamesrun

    no problem messing with my script, it works ok with objects taller than wider, check with objects wider than taller

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 9, 2013 8:31 AM   in reply to CarlosCanto

    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?

     

    Oddball-Shape.png

     
    |
    Mark as:
  • Currently Being Moderated
    May 24, 2013 12:08 AM   in reply to klemango

    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!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points