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

How to make a perfect rectangle around a selected object?

Explorer ,
Jul 20, 2020 Jul 20, 2020

Copy link to clipboard

Copied

I am making a transition from Corel Draw, I've always found it difficult because there are so many thing missing from Illustrator that are in Corel and it disrupts my work flow.

One such thing that helps me is to make a box around an object, In Corel, if you select an object and click on the rectangle icon while holding down shift, it will make a perfect rectangle (exact size) around the object.

Is there a way to do this in Illustrator? Even by way of a script?

TOPICS
Scripting

Views

696

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

Guide , Jul 20, 2020 Jul 20, 2020

Try this:

 

var coordinates = app.activeDocument.selection[0].visibleBounds;
var x1 = coordinates[0];
var y1 = coordinates[1];
var x2 = coordinates[2];
var y2 = coordinates[3];
var w = x2 - x1;
var h = y1 - y2;
var rect = app.activeDocument.pathItems.rectangle(y1, x1, w, h);
rect.strokeWidth = 1;
rect.filled = false;

 

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 20, 2020 Jul 20, 2020

Copy link to clipboard

Copied

Which version of Illustrator are you using?

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 ,
Jul 20, 2020 Jul 20, 2020

Copy link to clipboard

Copied

The latest version

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 ,
Jul 20, 2020 Jul 20, 2020

Copy link to clipboard

Copied

Try this:

 

var coordinates = app.activeDocument.selection[0].visibleBounds;
var x1 = coordinates[0];
var y1 = coordinates[1];
var x2 = coordinates[2];
var y2 = coordinates[3];
var w = x2 - x1;
var h = y1 - y2;
var rect = app.activeDocument.pathItems.rectangle(y1, x1, w, h);
rect.strokeWidth = 1;
rect.filled = false;

 

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 ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

I would also suggest getting the absolute value for w and h. This way if one of the y or x points is negative in the cartersian plane, you will still get a positive number.

var w = Math.abs(x2 - x1);
var h = Math.abs(y1 - y2);

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 ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

Thank you for your response, I am getting an error,

Error 8705: Target layer cannot be modified
Line: 9

-> var rect = app.activeDocument.pathItems.rectangle(y1, x1, 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
Guide ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

Is your layer locked?

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 ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

The layer I was working on is not locked, but I had some hidden layers, which I made visible, the script however does not work.

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 ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

Right figured it out.

My object had a fill and no stroke, when I apply a stroke to the object then run the script, it works.

Is there a way to ignore if the object has a stroke or not?

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 ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

Add this to the above to give your rectangle a black stroke:

colour1 = new CMYKColor ();
colour1.cyan = 0;
colour1.magenta = 0;
colour1.yellow = 0;
colour1.black = 100;
rect.strokeColor = colour1;

 

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 ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

LATEST

Yes works perfectly now. Thank 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
Explorer ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

I have fixed the error, all layers need to be visible.

But this does not work, when I apply the script, nothing happens.

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 ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

If you create guides to where you want the rectangle to go.  Then under choose snap to guides.... you can draw the perfect rectangle.  

Other than that you have to eyeball it.  Or bigger how far you want your rectangle to go out from the 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
Explorer ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

Instead of doing all that, I could just look at the size of the object and make a rectangle to that size. I need something that I can just click, like in Corel Draw.

I make signs and these boxes come in handy for all sorts of applications.

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 ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

easy, 

- select object

- apply Effect Convert To Shape, Rectangle (Relative, Extra Width/Height = 0)

- Shift + X to swap Fill/Stroke

- Expand Appearance

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 ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

Carlos is going to make a lonesome rectangle that kills its parent object. In his step-by-step list he forgot the second step (copy and paste in front or back). 8)

 

But if one is cunning, one could set up a graphic style including an additional fill or stroke via Appearance palette.

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 ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

I know Kurt, I left it out intentionally CarlosCanto_0-1595361205116.png I'm trying the Teach how to fish thing

 

 

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 ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

That is, of course, a smart procedure, Carlos.

 

Good evening, by the way. 🙂

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