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

Drawing a rectangle of exact size of the artboard?

New Here ,
Oct 17, 2010 Oct 17, 2010

Copy link to clipboard

Copied

Has anybody written a JS script to to draw a rectangle on an active document which would be exact size as the document's artboard?

I find it very inaccurate and cumbersome trying to draw such rectangle by hand.

Please let me know where such JS code can be found. I have not done any Illustrator scripting beyond the "Hello world".

Thanks

franK

TOPICS
Scripting

Views

13.1K

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
Adobe
Enthusiast ,
Oct 17, 2010 Oct 17, 2010

Copy link to clipboard

Copied

#target illustrator var docRef = app.activeDocument; var artboardRef = docRef.artboards; for(i=0;i<artboardRef.length;i++){      var top=artboardRef.artboardRect[1] ;      var left=artboardRef.artboardRect[0];      var width=artboardRef.artboardRect[2]-artboardRef.artboardRect[0];      var height=artboardRef.artboardRect[1]-artboardRef.artboardRect[3];      var rect = docRef.pathItems.rectangle (top, left, width, height);      rect.fillColor = rect.strokeColor = new NoColor();      }

[CS4/CS5] If the document have 20 artboards, you will get 20 rectangles as well.

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 ,
May 12, 2011 May 12, 2011

Copy link to clipboard

Copied

Cool! Was searching for a while for this now

I set up one for the exact artboard size to use for for instance Layer Clipping masks and a seperate one which includes the size of the bleed for backgrounds and such.

Thanks a lot for the example!

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 ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

The original script works great for creating boxes the size of the artboard. I have not tried the one with the bleed, but I'm wondering how easy it is to edit the original script to make the rectangle 1 inch smaller than the artboard and have that retangled centered in the artbox. So for example, a 6 inch by 6 inch artbox would have a rectangle that's 5x5 centered.

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
Guide ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

LATEST
var inch = 72;
var doc = app.activeDocument;
var AB = doc.artboards[0];
var w = (AB.artboardRect[2]-AB.artboardRect[0])-inch*2;
var h = (AB.artboardRect[1]-AB.artboardRect[3])-inch*2;
var top = left = inch;
var box = doc.pathItems.rectangle(-top, left, w, h);

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 ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

Hi, Is there anyway to make the rectangle target Bleeding Size instead of Artboard Size?

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 ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

Hi PhyonKatsutoshi,

It's been a long time since I was busy with it and have been out of it for a long time but my first thought is, since bleed sizes are pretty constant (I myself always use 3mm) you could just subtract that number from the position and add it 2x to the size.

So, in my 3 mm case, subtract the 3mm from the top and the left equations and add 6 to the width and the height.

Sorry if my suggestion is "too practical"

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 ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

Dude to the reason that my bleeding size are depending on the artboard size, if the artboard is A3 or A4, 5mm would usually do, but if the artboard is 60inches, usually would need 1inch bleeding instead, so it gets very annoying when having too much item getting snapped when trying to draw the circle, plus this would speed up my workflow with script

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 ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

Hmm, understood. Well, it may take a few days, but I'm willing to have a look.What version of Illustrator are you using?

I'll get back to you later, okay?

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 ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

Thank you ,i will wait for 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
Guide ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

here you go, hope this works for you...

pc.JPG

// Create Paths to use for clipping artwork

// 16/4/2015 - Qwertyfly.

var win = new Window ('dialog',"Paths for clipping masks");

win.alignChildren = 'left';

win.spacing = 2;

var chk = win.add('checkbox',undefined,"include artboard sized path");

var units = win.add('panel');

units.alignChildren = 'left';

var unit1 = units.add('radiobutton',undefined,"Millimeters");

var unit2 = units.add('radiobutton',undefined,"Inches");

unit1.value = true;

var bl = win.add('group');

var bleedtext = bl.add('statictext',undefined,"Bleed:");

var bleedvalue = bl.add('edittext',undefined,"3");

bleedvalue.characters = 5;

bleedvalue.active = true;

var btns = win.add('group');

btns.alignChildren = 'center';

btns.margins = [0,10,0,0];

var ok = btns.add('button',undefined,"OK");

var cancel = btns.add('button',undefined,"Cancel");

cancel.onClick = function(){

    win.close();

}

ok.onClick = function(){

    if(chk.value == true){

        draw(0,"Artboard Clip");

     }

    if(unit1.value == true){

        var bleed = bleedvalue.text * 2.83466796875; // mm

     } else {

         var bleed = bleedvalue.text * 72; // inch

     }

     draw(bleed,"Bleed Clip");

     win.close();

}   

   

win.show();

function draw(bleed,name){

// Care of Moluapple -  Oct 18, 2010 5:36 PM (in response to fklatil) - Drawing a rectangle of exact size of the artboard?

var docRef = app.activeDocument; 

var artboardRef = docRef.artboards;

for(i=0;i<artboardRef.length;i++){ 

     var top=artboardRef.artboardRect[1]; 

     var left=artboardRef.artboardRect[0]; 

     var width=artboardRef.artboardRect[2]-artboardRef.artboardRect[0]; 

     var height=artboardRef.artboardRect[1]-artboardRef.artboardRect[3]; 

     var rect = docRef.pathItems.rectangle (top+bleed, left-bleed, width+(bleed*2), height+(bleed*2)); 

     rect.fillColor = rect.strokeColor = new NoColor();

     rect.name = name;

     } 

//

}

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 ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

Cool script
Although I keep wondering if there isn't a way to automatically read-out the bleed size?

Regards

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
Guide ,
Apr 16, 2015 Apr 16, 2015

Copy link to clipboard

Copied

Muppet Mark-QAl63s‌ came up with a solution to getting the bleed box info a few years back.

Read Bleed Settings in AI CS4

its a bit of a hack from what I can glean, looks like he saves file as ai then reads the xml data for the bleed box values.

I've not played with it, but if it works that can be added to the above script.

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
Guide ,
Apr 16, 2015 Apr 16, 2015

Copy link to clipboard

Copied

This one now has the ability to use the documents bleedbox and you can set each bleed independently.

BE WARNED...

It will also save your file to the desktop as Bleed.ai

and will rename your document to Bleed.ai

have not worked out how to "save a copy".

// Create Paths to use for clipping artwork

// 16/4/2015 - Qwertyfly.

// Version 2

//

// Care of Muppet Mark-QAl63s  - Jan 24, 2011 1:32 AM (in response to Muppet Mark-QAl63s) - Read Bleed Settings in AI CS4

var temp = new File('~/Desktop/Bleed.ai');

app.activeDocument.saveAs(temp);

var pdfBoxes = getPDFboxes(temp);

var bleedTopRaw = pdfBoxes.MediaBox[3] - pdfBoxes.TrimBox[3]; 

var bleedBottomRaw = pdfBoxes.TrimBox[1]; 

var bleedLeftRaw = pdfBoxes.TrimBox[0]; 

var bleedRightRaw = pdfBoxes.MediaBox[2] - pdfBoxes.TrimBox[2];

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var bleedTop, bleedBottom, bleedLeft, bleedRight;

var win = new Window ('dialog',"Paths for clipping masks");

win.alignChildren = 'center';

win.spacing = 2;

var chk = win.add('checkbox',undefined,"include artboard sized path");

var units = win.add('panel');

units.alignChildren = 'left';

var unit1 = units.add('radiobutton',undefined,"Millimeters");

var unit2 = units.add('radiobutton',undefined,"Inches");

unit1.value = true;

setUnits(2.83466796875); //mm

var ex = win.add('statictext',undefined,"values below do have some rounding applied");

var exact = win.add('checkbox',undefined,"Use exact values of BleedBox");

exact.value = true;

var bl = win.add('group');

bl.alignChildren = 'right';

bl.orientation = "column";

bl.margins = 2;

var bl1 = bl.add('group');

var bleedtext1 = bl1.add('statictext',undefined,"Bleed Top:");

var bleedvalue1 = bl1.add('edittext',undefined,bleedTop);

bleedvalue1.characters = 5;

bleedvalue1.active = true;

var bl2 = bl.add('group');

var bleedtext2 = bl2.add('statictext',undefined,"Bleed Bottom:");

var bleedvalue2 = bl2.add('edittext',undefined,bleedBottom);

bleedvalue2.characters = 5;

var bl3 = bl.add('group');

var bleedtext3 = bl3.add('statictext',undefined,"Bleed Left:");

var bleedvalue3 = bl3.add('edittext',undefined,bleedLeft);

bleedvalue3.characters = 5;

var bl4 = bl.add('group');

var bleedtext4 = bl4.add('statictext',undefined,"Bleed Right:");

var bleedvalue4 = bl4.add('edittext',undefined,bleedRight);

bleedvalue4.characters = 5;

var btns = win.add('group');

btns.alignChildren = 'center';

btns.margins = [0,10,0,0];

var ok = btns.add('button',undefined,"OK");

var cancel = btns.add('button',undefined,"Cancel");

unit1.onClick = function(){

        setUnits(2.83466796875); //mm

        bleedvalue1.text = bleedTop;

        bleedvalue2.text = bleedBottom;

        bleedvalue3.text = bleedLeft;

        bleedvalue4.text = bleedRight;

};

unit2.onClick =  function(){

        setUnits(72); //inch

        bleedvalue1.text = bleedTop;

        bleedvalue2.text = bleedBottom;

        bleedvalue3.text = bleedLeft;

        bleedvalue4.text = bleedRight;

};

cancel.onClick = function(){

    win.close();

}

ok.onClick = function(){

     

    if(chk.value == true){

        draw(0,0,0,0,"Artboard Clip");

     }

    if(exact.value == true){

        var bleed1 = bleedTopRaw;

        var bleed2 = bleedBottomRaw;

        var bleed3 = bleedLeftRaw;

        var bleed4 = bleedRightRaw;

    }else if(unit1.value == true){

        var bleed1 = bleedvalue1.text * 2.83466796875; // mm

        var bleed2 = bleedvalue2.text * 2.83466796875; // mm

        var bleed3 = bleedvalue3.text * 2.83466796875; // mm

        var bleed4 = bleedvalue4.text * 2.83466796875; // mm

     } else {

         var bleed1 = bleedvalue1.text * 72; // inch

         var bleed2 = bleedvalue2.text * 72; // inch

         var bleed3 = bleedvalue3.text * 72; // inch

         var bleed4 = bleedvalue4.text * 72; // inch

     }

     draw(bleed1,bleed2,bleed3,bleed4,"Bleed Clip");

     win.close();

}   

   

win.show();

function setUnits(Unit){

    bleedTop = (bleedTopRaw / Unit); 

    bleedBottom = (bleedBottomRaw / Unit); 

    bleedLeft = (bleedLeftRaw / Unit); 

    bleedRight = (bleedRightRaw / Unit);

    if(Unit == 72){

        bleedTop = bleedTop.toFixed(3);

        bleedBottom = bleedBottom.toFixed(3);

        bleedLeft = bleedLeft.toFixed(3);

        bleedRight = bleedRight.toFixed(3);

    }else{

        bleedTop = Math.round(bleedTop);

        bleedBottom = Math.round(bleedBottom);

        bleedLeft = Math.round(bleedLeft);

        bleedRight = Math.round(bleedRight);

    }

}

function draw(bleedTop,bleedBottom,bleedLeft,bleedRight,name){

// Care of Moluapple -  Oct 18, 2010 5:36 PM (in response to fklatil) - Drawing a rectangle of exact size of the artboard?

var docRef = app.activeDocument; 

var artboardRef = docRef.artboards;

for(i=0;i<artboardRef.length;i++){ 

     var top=artboardRef.artboardRect[1]; 

     var left=artboardRef.artboardRect[0]; 

     var width=artboardRef.artboardRect[2]-artboardRef.artboardRect[0]; 

     var height=artboardRef.artboardRect[1]-artboardRef.artboardRect[3];

     var rect = docRef.pathItems.rectangle (top+bleedTop, left-bleedLeft, width+(+bleedLeft)+(+bleedRight), height+(+bleedTop)+(+bleedBottom)); 

     rect.fillColor = rect.strokeColor = new NoColor();

     rect.name = name;

     } 

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

}

// Care of Muppet Mark-QAl63s  - Jan 24, 2011 1:32 AM (in response to Muppet Mark-QAl63s) - Read Bleed Settings in AI CS4

function getPDFboxes(f) { 

     var pdfBoxes = new Object(); 

     try { 

          f.open('r');

          var str = f.read(); 

          var b = str.match(/BleedBox\[\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\]/g); 

          if (b != null) pdfBoxes.BleedBox = b[0].match(/\d{1,5}\.\d{1,5}/g); 

          var a = str.match(/ArtBox\[\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\]/g); 

          if (a != null) pdfBoxes.ArtBox = a[0].match(/\d{1,5}\.\d{1,5}/g);

          var m = str.match(/MediaBox\[\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\]/g); 

          if (m != null) pdfBoxes.MediaBox = m[0].match(/\d{1,5}\.\d{1,5}/g); 

          var t = str.match(/TrimBox\[\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\]/g); 

          if (t != null) pdfBoxes.TrimBox = t[0].match(/\d{1,5}\.\d{1,5}/g); 

          var c = str.match(/CropBox\[\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\s\d{1,5}\.\d{1,5}\]/g); 

          if (c != null) pdfBoxes.CropBox = c[0].match(/\d{1,5}\.\d{1,5}/g);

          f.close(); 

     } catch(err) { 

          f.close(); 

     }

     return pdfBoxes; 

} //------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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
Guide ,
Apr 15, 2015 Apr 15, 2015

Copy link to clipboard

Copied

if you use Jaxiter‌'s solution but precede it with a prompt

var bleed = prompt("Enter amount of required bleed','5');

var TOmm = 2.83466796875;

bleed = bleed*TOmm;

then remove bleed from top and left and add Bleed*2 to width and height.

don't hold me to the above code as I have not tested it.

if you want to add radio buttons for mm inch px etc then you will need a ScriptUI window

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