• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Script for making an object the artboard size.

Community Beginner ,
Jan 24, 2012 Jan 24, 2012

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

TOPICS
Scripting

Views

40.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 25, 2012 Jan 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)

          {

            

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 24, 2012 Jan 24, 2012

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 24, 2012 Jan 24, 2012

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2012 Jan 25, 2012

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

          }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 25, 2012 Jan 25, 2012

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

you're welcome, glad to help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 12, 2012 May 12, 2012

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 12, 2012 May 12, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 08, 2012 Jun 08, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 09, 2012 Jun 09, 2012

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 08, 2012 Jun 08, 2012

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

        }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 09, 2012 Jun 09, 2012

Copy link to clipboard

Copied

you're welcome db

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 09, 2012 Jun 09, 2012

Copy link to clipboard

Copied

Carlos, I have CS5.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2012 Aug 05, 2012

Copy link to clipboard

Copied

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

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 10, 2012 Oct 10, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2012 Oct 11, 2012

Copy link to clipboard

Copied

ok, let me work on it...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 16, 2012 Oct 16, 2012

Copy link to clipboard

Copied

Ok thanks i waiting for it...

greetz

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 16, 2012 Oct 16, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 07, 2013 Jan 07, 2013

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 07, 2013 Jan 07, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 07, 2013 Jan 07, 2013

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)

http://pastebin.com/AY7cvuQw

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2013 Jan 07, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 09, 2013 Jan 09, 2013

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?

Oddball-Shape.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 24, 2013 May 24, 2013

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 02, 2016 Dec 02, 2016

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines