Question

I am writing a few helper classes for scripting Adobe Illustrator.

My problem starts in the Eyelet object. When I instantiate it it fails at the first new Group() because apparently Group doesn't have a constructor.

Here is a stripped down version of my code:

/****************** Collection Class **********************/

function Collection() {
    this.parent = app.activeDocument;
    this.typename = "Collection";
}

    Collection.prototype.setName = function(name) {
        this.instance.name = name;
    };

/****************** Group (extends collection) *****************/

function Group(name, parent) {
    this.parent = parent || this.parent;
    this.instance = this.parent.groupItems.add();
    if(name) {
        this.setName(name);
    } else {
        this.setName("Group");
    }
}

Group.prototype = new Collection();

/****************** Shape Class **********************/

function Shape() {
    this.parent = app.activeDocument;
    this.typename = "Shape";
}

Shape.prototype.setName = function(name) {
        this.instance.name = name;
    };

Shape.prototype.stroke = function(width, color) {
        this.instance.stroked = true;
        this.instance.strokeWeight = width;
        this.instance.strokeColor = color;
    };

/****************** Line (extends Shape) **********************/

function Line(parent, start, end) {

    this.instance = parent.pathItems.add();
    // [ [startX, startY], [endX, endY] ]
    this.instance.setEntirePath([start,end]);

}

Line.prototype = new Shape();

/****************** Eyelet (extends Shape) **********************/

function Eyelet(parent, position) {
    this.instance = new Group("Eyelet", parent);
    var whiteCross = new Group("White", this.instance);
    var blackCross = new Group("Black", this.instance);

    var build = function(group, color, width) {
        var vertical = new Line( group , [0 , 0] , [0 , 50] );
        vertical.setName("vertical");
        vertical.stroke(width, color);
        var horizontal = new Line( group , [0 , 50] , [50 , 0] );
        horizontal.setName("horizontal");
        horizontal.stroke(width, color);
    };

    build(whiteCross.instance, white, (3 * scale) );
    build(blackCross.instance, black, (1 * scale) );

    this.instance.position = position;

}

Eyelet.prototype = new Shape();

When I write

var eyelet = new Eyelet(layer2, [10,10]);

I get

Group does not have a constructor

I've ran this code through jslint and I can't see why this isn't working. Any help would be much appreciated.

Was it helpful?

Solution

It turns out the fact this is in Adobe Illustrator is relevant. I renamed the Group object to MyGroup as Dan Breslau suggested and it worked as I expected. It seems Illustrator has a global Group object which was causing the problem. Thanks for your help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top