// Grid of Squares — Adobe Illustrator compatible
// Draws a rows x cols grid of squares, colour graded across the grid.
#target illustrator

var doc = app.activeDocument;

var cols = 8, rows = 6;
var size = 40, gap = 10;
var originX = 40, originY = 40;

for (var r = 0; r < rows; r++) {
    for (var c = 0; c < cols; c++) {
        var left = originX + c * (size + gap);
        var top  = originY + r * (size + gap);

        var rect = doc.pathItems.rectangle(top, left, size, size);

        var col = new RGBColor();
        col.red   = Math.round(255 * c / (cols - 1));
        col.green = Math.round(255 * r / (rows - 1));
        col.blue  = 180;
        rect.fillColor = col;
        rect.stroked = false;
    }
}

app.redraw();
$.writeln("Created " + (cols * rows) + " squares.");
