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?
1 Correct answer
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;
Explore related tutorials & articles
Copy link to clipboard
Copied
Which version of Illustrator are you using?
Copy link to clipboard
Copied
The latest version
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;
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);
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);
Copy link to clipboard
Copied
Is your layer locked?
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.
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?
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;
Copy link to clipboard
Copied
Yes works perfectly now. Thank you
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.
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.
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.
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
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.
Copy link to clipboard
Copied
I know Kurt, I left it out intentionally I'm trying the Teach how to fish thing
Copy link to clipboard
Copied
That is, of course, a smart procedure, Carlos.
Good evening, by the way. 🙂

