Highlighted

How to make a perfect rectangle around a selected object?

Explorer ,
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?

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;

 

Topics

Scripting

Views

187

Likes

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

How to make a perfect rectangle around a selected object?

Explorer ,
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?

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;

 

Topics

Scripting

Views

188

Likes

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

Copy link to clipboard

Copied

Which version of Illustrator are you using?

Likes

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
Reply
Loading...
Explorer ,
Jul 20, 2020

Copy link to clipboard

Copied

The latest version

Likes

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
Reply
Loading...
Contributor ,
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;

 

Likes

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
Reply
Loading...
Explorer ,
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);

Likes

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
Reply
Loading...
Explorer ,
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);

Likes

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
Reply
Loading...
Contributor ,
Jul 22, 2020

Copy link to clipboard

Copied

Is your layer locked?

Likes

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
Reply
Loading...
Explorer ,
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.

Likes

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
Reply
Loading...
Explorer ,
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?

Likes

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
Reply
Loading...
Contributor ,
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;

 

Likes

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
Reply
Loading...
Explorer ,
Jul 22, 2020

Copy link to clipboard

Copied

Yes works perfectly now. Thank you

Likes

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
Reply
Loading...
Explorer ,
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.

Likes

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
Reply
Loading...
Adobe Community Professional ,
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.  

Likes

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
Reply
Loading...
Explorer ,
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.

Likes

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
Reply
Loading...
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Adobe Community Professional ,
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.

Likes

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
Reply
Loading...
Adobe Community Professional ,
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

 

 

Likes

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
Reply
Loading...