Skip to main content
Known Participant
July 20, 2020
Answered

How to make a perfect rectangle around a selected object?

  • July 20, 2020
  • 6 replies
  • 1485 views

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?

This topic has been closed for replies.
Correct answer femkeblanco

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;

 

6 replies

Kurt Gold
Community Expert
Community Expert
July 21, 2020

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

 

Good evening, by the way. 🙂

Kurt Gold
Community Expert
Community Expert
July 21, 2020

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). 😎

 

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

CarlosCanto
Community Expert
Community Expert
July 21, 2020

I know Kurt, I left it out intentionally  I'm trying the Teach how to fish thing

 

 

CarlosCanto
Community Expert
Community Expert
July 21, 2020

easy, 

- select object

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

- Shift + X to swap Fill/Stroke

- Expand Appearance

KShinabery212
Community Expert
Community Expert
July 21, 2020

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.  

Let's connect on LinkedIn. https://www.linkedin.com/in/kshinabery/
Known Participant
July 22, 2020

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.

femkeblanco
femkeblancoCorrect answer
Legend
July 21, 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;

 

Known Participant
July 21, 2020

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);
Kurt Gold
Community Expert
Community Expert
July 20, 2020

Which version of Illustrator are you using?

Known Participant
July 21, 2020

The latest version