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

Illustrator Javascript to create rectangle the size of the artboard, then one 6mm less wide and tall

New Here ,
Apr 02, 2025 Apr 02, 2025

Hi there everyone!

 

I have managed to write a script to create a rectangle the size of whatever the artboard is, but I also need to create additional rectangles that are 6mm less wide and tall than the artboard, and then 12mm less wide and tall than the artboard.

 

My current script to do this is:

function createRectOnArtboard() { var doc = app.activeDocument; var artboard = doc.artboards[ doc.artboards.getActiveArtboardIndex() ]; var bounds = artboard.artboardRect; var rect = doc.pathItems.rectangle( bounds[0], bounds[1], bounds[2], bounds[3] * -1 ); rect.name = artboard.name; }; createRectOnArtboard();


I am not bothered if the solution is an addition/amendment to the above script, or completely entirely new code... just as long as I end up with 3 separate rectangles at artboard size, artboard size -6x-6, artboard size -12x-12.

Thank you so much in advance, and have a lovely week!

TOPICS
Scripting
131
Translate
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
Participant ,
Apr 02, 2025 Apr 02, 2025

Hopefully this helps. it could be written better, but this should get you started in the right direction

function createRectOnArtboard() { var doc = app.activeDocument;
var artboard = doc.artboards[ doc.artboards.getActiveArtboardIndex() ];
var bounds = artboard.artboardRect;
/* right here you are adding a rectangle and giving it dimensions
 Rect(TOP,LEFT,WIDTH,HEIGHT) you are multiplying the height by a -1 to make it positive value
 i would change it to Math.abs(value) to make sure its always positive
 to make the rectangle smaller just need to do some math*/
var rect = doc.pathItems.rectangle( bounds[0], bounds[1], bounds[2], Math.abs(bounds[3]));
rect.name = artboard.name;
/*all values are needed as pts. i use UnitValue.as to convert
sub 6 from the top, add 6 to the left, sub 12 from the width and sub 12 from the height */
var Rect6less = doc.pathItems.rectangle( bounds[0]-UnitValue("6","mm").as("pt"), bounds[1]+UnitValue("6","mm").as("pt"), bounds[2]-UnitValue("12","mm").as("pt"), Math.abs(bounds[3])-UnitValue("12","mm").as("pt") );
Rect6less.name ="6mm less"
var Rect12less = doc.pathItems.rectangle( bounds[0]-UnitValue("12","mm").as("pt"), bounds[1]+UnitValue("12","mm").as("pt"), bounds[2]-UnitValue("24","mm").as("pt"), Math.abs(bounds[3])-UnitValue("24","mm").as("pt") );
Rect12less.name ="12mm less"
};
createRectOnArtboard();
Translate
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 02, 2025 Apr 02, 2025

Thank you so much for your speedy reply, that's definitely in the correct direction.. for some reason the resultant rectangles are -12mm in size on both axes... and admittedly I don't think I'm smart enough to edit it (as fool proof as you've made it for me with comment lines and all)...

 

So starting with an artboard at 200mm say, I end up with the outer one correctly at 200mm x 200mm, the next one is actually 188mm x 188mm (I need it to be just -6mm on each, so should be 194 x 194) and then the next is 176mm x 176mm (again, it's -12 rather than just -6, the third innermost rectangle should be 188 x 188, in this example).

 

Thank you so much for tolerating this old fool!

Translate
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 ,
Apr 02, 2025 Apr 02, 2025

ahhh misunderstood. you need the first one inset 3 on all sides for a total of 6 reduction from width and 6 from height. i was doing 6 on all sides for 12 in width and height. that one would actually be valid for the third rectangle. but to edit the rectangles just need to change the value in the UnitValue("#","mm").as("pt").

whatever value you put in there will reduce or increase the rectangles for your instance

var Rect6less = doc.pathItems.rectangle( bounds[0]-UnitValue("3","mm").as("pt"), bounds[1]+UnitValue("3","mm").as("pt"), bounds[2]-UnitValue("6","mm").as("pt"), Math.abs(bounds[3])-UnitValue("6","mm").as("pt") );
Rect6less.name ="6mm less"
var Rect12less = doc.pathItems.rectangle( bounds[0]-UnitValue("6","mm").as("pt"), bounds[1]+UnitValue("6","mm").as("pt"), bounds[2]-UnitValue("12","mm").as("pt"), Math.abs(bounds[3])-UnitValue("12","mm").as("pt") );
Rect12less.name ="12mm less"

maybe writing it like this would be easier to understand. less wordy

var InsetVal = UnitValue("6","mm").as("pt") //will inset all sides by this value
var Rect = doc.pathItems.rectangle( bounds[0]-InsetVal, bounds[1]+InsetVal, bounds[2]-(InsetVal*2), Math.abs(bounds[3])-(InsetVal*2) );
Rect.name ="NAME ME"
Translate
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
Enthusiast ,
Apr 03, 2025 Apr 03, 2025
LATEST
Translate
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