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

How to construct a Rectangle element

Explorer ,
May 20, 2011 May 20, 2011

Copy link to clipboard

Copied

I would like to use the Rectangle object described in the help to ExtendScript CS5:

>  Rectangle
>  Adobe Illustrator CS5 Type Library
>  Describes a rectangle. This class is also a four-element collection.

It has nine properties and methods.

This is not the same as PathItems.rectangle().

How can I construct a var of type Rectangle?

                var rect = new Rectangle

results in the error, "Rectangle does not have a constructor"

How can I create a var of type Rectangle and then set the values?  I would like to store it in an Array.

TOPICS
Scripting

Views

5.5K

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
Adobe
Community Expert ,
May 22, 2011 May 22, 2011

Copy link to clipboard

Copied

Hi, try below

var myRectangle = app.activeDocument.pathItems.rectangle(-100,100,150,150);

You can add rectangles use pathItems rectangle method.

PathItem PathItems.rectangle (top: number, left: number, width: number, height: number[, reversed: bool=false])

Ten.

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 ,
May 22, 2011 May 22, 2011

Copy link to clipboard

Copied

I am looking for a Rectangle that I can store in an array, not one in a document.  Maybe such a thing doesn't exist.  Specifically, I am trying to center text labels in circles when they are already in a circle.  I can get the geometricBounds or visibleBounds of each, but I don't know how to get that in an array.  Placing a Rectangle in the array would satisfy the need.  It would be nice to get the functionality of width, height, relative to top, bottom, left, right, that a Rectangle offers.  Oddly enough, text objects have different parameters of these available than do circles.  I would like a class Rectangle that tracks all these when I set them and ask for them.

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 ,
May 23, 2011 May 23, 2011

Copy link to clipboard

Copied

Here is a sample code store rectangles in an Array.

var myRectangle = new Array();

for (i=0;i<10;i++){
    myRectangle = app.activeDocument.pathItems.rectangle(-100,i*15+30,10,10);
    }

var c = new CMYKColor;
c.cyan= 100;
c.magenta = 0;
c.yellow = 0;
c.black = 0;

myRectangle[4].fillColor = c;

Ten

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 ,
May 23, 2011 May 23, 2011

Copy link to clipboard

Copied

Despite your insistence, RA isn't referring to physically drawn rectangles *at all*.

Theoretically, a "rectangle class" would allow one to construct a rectangle in several ways -- origin, size, top left and bottom right points, etc. It seems the built-in "Rect" class is just a thin wrapper around a basic array, so that's not going to work.

Perhaps you could build your own. Javascript allows one to create a new class from scratch and define all of the methods and properties you might need.

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 ,
May 23, 2011 May 23, 2011

Copy link to clipboard

Copied

I can get the geometricBounds or visibleBounds of each, but I don't know how to get that in an array.

... Maybe you want to expand on that. Both *are* returned as a simple 4-element array.

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 ,
May 23, 2011 May 23, 2011

Copy link to clipboard

Copied

True, I could store the geometricBounds or visibleBounds, but these don't have the methods for top, bottom, right, left, width, height.  I tried to make a class, but ExtendScript tells me I'm making "illegal use of reserved word 'class,'"  so I haven't pursued that further yet.

I would like to write a generic "align" routine that would align two objects, but some items have top and height while others have top and bottom, so the procedure to align depends on which kind of object is passed.  Putting a class wrapper around geometricBounds would do it since they all seem to have geometricBounds, but I haven't figured out how to do that either.  I could also use "geometricBounds[0]" etc, but that makes the code big and obscure.

As you can tell, I'm new to javascript and this is my learning process.  Extendscript seems to differ somewhat from conventional javascript, making the effort a bit more difficult.

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
Contributor ,
May 23, 2011 May 23, 2011

Copy link to clipboard

Copied

hi,

you cant use class, try something like that and search for javascript prototyping.

function Point(x,y){

this.x=x;

this.y=y

}

function MyRect(bounds){

this.width= bounds[?]-bounds[?];

and so on

dont forget:

this.center = new Point(a,b);

}

MyRect.prototype.doubleWidth=function(){

return this.width * 2

}

var rect= new MyRect( Item.someBounds);

happy scripting

chris

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 ,
May 23, 2011 May 23, 2011

Copy link to clipboard

Copied

Ah yes -- I didn't remember where I saw this before and had to ask Marc Autret first

Check this page of his: http://www.indiscripts.com/post/2010/05/operator-overloading-with-extendscript

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 ,
May 23, 2011 May 23, 2011

Copy link to clipboard

Copied

Thanks.  I guess I'll have to try something like that.

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 Beginner ,
Nov 11, 2016 Nov 11, 2016

Copy link to clipboard

Copied

You can just create a new Point like this: "var newPoint = Point(leftValue, topValue);" . I could not find a way to create a rect (like for artboard resizing), but you can duplicate the existing artboard's artboardRect into your own variable, change its values, and then set it back to the artboardRect without needing to create your own rect.

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 Beginner ,
Nov 11, 2016 Nov 11, 2016

Copy link to clipboard

Copied

LATEST

(You can create totally unrelated rects if that matters to a solution you are crafting by creating an artboard, getting its rect, and deleting the new artboard.)

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