We have all seen examples of beautiful images created in Adobe Photoshop or Illustrator with a hand and a mouse (or a tablet). But it’s not the only way to generate artwork in these apps. Enter the scripting world!
While most original artwork will always be created by hand, there are situations when automation comes in handy. All apps in Adobe Creative Suite allow for automation in one way or another (e.g. actions) but scripting is the most powerful. There are many many useful scripts available today for Photoshop, Illustrator or InDesign and it’s worth searching on the Internet before spending precious hours on another mundane task. Chances are someone has already created a script for the same reason.
If you can’t find a script to help with your specific problem, you might even create it yourself! Scripting for Adobe products is quite well documented and you can always turn to forums for additional help.
Below is a simple script written by myself to create a grid of rectangles within bounds of a document or another shape in Adobe Illustrator (CS6 or newer). Copy-paste the below code in a plain text editor and save as ‘SimpleGrid.jsx’. Open a document and run the script from File/Scripts/Other Script… menu. Enter the number of columns and rows and hit OK. The active artboard will be filled with a grid of rectangles, or if you had selected an item, the grid will be created over that item.
The script can be easily modified to add more advanced settings like grid inset or spacing so feel free to experiment. Have fun :)
#target Illustrator
// Simple Grid Generator // by Aleksander Lenart var ver = '0.1';
var doc = app.activeDocument; var itemX, itemY, itemWidth, itemHeight;
function init() {if( app.documents.length > 0 ) {
var sel = app.activeDocument.selection;
if( sel.length == 1 ) {
// Use bounds of the selected item
sel = app.activeDocument.selection[0];
itemX = sel.geometricBounds[0];
itemY = sel.geometricBounds[1];
itemWidth = sel.geometricBounds[2] - sel.geometricBounds[0];
itemHeight = sel.geometricBounds[1] - sel.geometricBounds[3];
sel.selected = false;
} else if( sel.length == 0 ) {
// Use bounds of the active artboard
var art = app.activeDocument.artboards[app.activeDocument.artboards.getActiveArtboardIndex()];
itemX = art.artboardRect[0];
itemY = art.artboardRect[1];
itemWidth = art.artboardRect[2] - art.artboardRect[0];
itemHeight = art.artboardRect[1] - art.artboardRect[3];
} else {
alert( 'Please select only one item!' );
return;
}
// Dialog window
var win = new Window( 'dialog', 'Simple Grid v' + ver );
win.alignChildren = "fill";
win.gridPanel = win.add( 'panel', undefined, 'Grid Properties');
win.gridPanel.rowGroup = win.gridPanel.add( 'group', undefined );
win.gridPanel.rowGroup.infoRows = win.gridPanel.rowGroup.add( 'statictext', undefined, 'Rows');
win.gridPanel.rowGroup.numRows = win.gridPanel.rowGroup.add( 'edittext', undefined, 10);
win.gridPanel.colGroup = win.gridPanel.add( 'group', undefined );
win.gridPanel.colGroup.infoCols = win.gridPanel.colGroup.add( 'statictext', undefined, 'Columns');
win.gridPanel.colGroup.numCols = win.gridPanel.colGroup.add( 'edittext', undefined, 10);
win.fillPanel = win.add( 'panel', undefined, 'Fill Shape');
win.fillPanel.rectShape = win.fillPanel.add( 'radiobutton', undefined, 'Rectangle');
win.fillPanel.rectShape.value = true;
win.btnGroup = win.add( 'group', undefined );
win.btnGroup.alignment = "center";
win.btnGroup.cancelBtn = win.btnGroup.add( 'button', undefined, 'Cancel' );
win.btnGroup.okBtn = win.btnGroup.add( 'button', undefined, 'OK' );
var getValues = function() {
var doRows = Number(win.gridPanel.rowGroup.numRows.text);
var doCols = Number(win.gridPanel.colGroup.numCols.text);
draw( itemX, itemY, itemWidth, itemHeight, doCols, doRows );
}
win.btnGroup.okBtn.onClick = function() {
getValues();
win.close();
}
win.btnGroup.cancelBtn.onClick = function() {
win.close();
}
win.show();
} else {
alert( 'Hey, I need a document here!' );
return;
}}
function draw( x, y, width, height, cols, rows ) {
// Create group
var gridGroup = app.activeDocument.groupItems.add();
for( var c = 0; c < cols; ++c ) {
for( var r = 0; r < rows; ++r ) {
// Draw shape and add to group
var shapePath = gridGroup.pathItems.rectangle( y-height/rows*r, x+width/cols*c, width/cols, height/rows );
shapePath.filled = false;
shapePath.stroked = false;
}
}
// Select newly created items
gridGroup.selected = true;
}init();
