Skip to content

Instantly share code, notes, and snippets.

@frederickk
Created February 3, 2012 12:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save frederickk/1730055 to your computer and use it in GitHub Desktop.
Save frederickk/1730055 to your computer and use it in GitHub Desktop.
a script which allows one to create a grid (margins, rows, and columns) in illustrator similar to "create guides..." in indesign.
/**
* Create Grid
*
* Ken Frederick
* ken.frederick@gmx.de
*
* http://cargocollective.com/kenfrederick/
* http://kenfrederick.blogspot.com/
*
*/
// ------------------------------------------------------------------------
// Properties
// ------------------------------------------------------------------------
/**
* Note from the Scriptographer.org Team
*
* In Scriptographer 2.9, we switched to a top-down coordinate system and
* degrees for angle units as an easier alternative to radians.
*
* For backward compatibility we offer the possibility to still use the old
* bottom-up coordinate system and radians for angle units, by setting the two
* values bellow. Read more about this transition on our website:
* http://scriptographer.org/news/version-2.9.064-arrived/
*/
script.coordinateSystem = 'bottom-up';
script.angleUnits = 'radians';
// document properties
var sel;
var group;
var groupMargins;
var groupRowsCols;
var palette;
// conversions
var ptToMm = 0.352777778;
var mmToPt = 2.83464567;
var ptToIn = 0.0138888889;
var inToPt = 72;
var ptToPi = 0.0833333333;
var piToPt = 12;
// default unit of measure
var unit = 'millimeter';
var valDef = 6*mmToPt;
// values
var values = {
// ------------------------------------
// margins
// ------------------------------------
marginTop: valDef,
marginBottom: valDef,
marginLeft: valDef,
marginRight: valDef,
// ------------------------------------
// rows
// ------------------------------------
rows: 0,
rowGutter: valDef,
// ------------------------------------
// cols
// ------------------------------------
cols: 0,
colGutter: valDef,
// ------------------------------------
// options
// ------------------------------------
bRemoveExist: false,
bAllArtboards: true
};
// gui components
var components = {
// ------------------------------------
// flyout menu
// ------------------------------------
bMmUnit: {
type: 'menu-entry',
value: 'millimeter',
onSelect: function() {
unit = 'millimeter';
valDef = 6*mmToPt;
updatePalette(unit, valDef);
}
},
bInUnit: {
type: 'menu-entry',
value: 'inch',
onSelect: function() {
unit = 'inch';
valDef = 0.5*inToPt;
updatePalette(unit, valDef);
}
},
bPiUnit: {
type: 'menu-entry',
value: 'pica',
onSelect: function() {
unit = 'pica';
valDef = 3*piToPt;
updatePalette(unit, valDef);
}
},
bPtUnit: {
type: 'menu-entry',
value: 'point/pixel',
onSelect: function() {
unit = 'point';
valDef = 36;
updatePalette(unit, valDef);
}
},
// ------------------------------------
// margins
// ------------------------------------
marginRule: {
type: 'ruler',
label: 'Margins',
fullSize: true,
},
marginTop: {
type: 'number',
label: 'Top',
units: unit,
steppers: true,
fractionDigits: 2,
},
marginBottom: {
type: 'number',
label: 'Bottom',
units: unit,
steppers: true,
fractionDigits: 2,
},
marginLeft: {
type: 'number',
label: 'Left',
units: unit,
steppers: true,
fractionDigits: 2,
},
marginRight: {
type: 'number',
label: 'Right',
units: unit,
steppers: true,
fractionDigits: 2,
},
// ------------------------------------
// rows
// ------------------------------------
rowRule: {
type: 'ruler',
label: 'Rows',
fullSize: true,
},
rows: {
type: 'number',
label: 'Number',
units: 'none',
fractionDigits: 0,
},
rowGutter: {
type: 'number',
label: 'Gutter',
units: unit,
fractionDigits: 2,
},
// ------------------------------------
// cols
// ------------------------------------
colRule: {
type: 'ruler',
label: 'Columns',
fullSize: true,
},
cols: {
type: 'number',
label: 'Number',
units: 'none',
fractionDigits: 0,
},
colGutter: {
type: 'number',
label: 'Gutter',
units: unit,
fractionDigits: 2,
},
// ------------------------------------
// options
// ------------------------------------
optionsRule: {
type: 'ruler',
label: 'Options',
fullSize: true,
},
bRemoveExist: {
type: 'checkbox',
label: 'Remove Existing\nMargins/Grid',
},
bAllArtboards: {
type: 'checkbox',
label: 'All Artboards',
},
// ------------------------------------
// apply that shit!
// ------------------------------------
applyRule: {
type: 'ruler',
fullSize: true,
},
submit: {
type: 'button',
value: 'Generate',
fullSize: true,
onClick: function() {
Main();
}
}
};
// ------------------------------------------------------------------------
// Setup
// ------------------------------------------------------------------------
function Setup() {
// initialize the palette window
palette = new Palette('Create Grid', components, values);
}
// ------------------------------------------------------------------------
// Update
// ------------------------------------------------------------------------
function Update() {
}
// ------------------------------------------------------------------------
// Main
// ------------------------------------------------------------------------
function Main() {
// create grid layer
var layer;
for(i in activeDocument.layers) {
var l = activeDocument.layers[i];
l.locked = false;
if(l.name == '_grid_') {
layer = l;
if(values.bRemoveExist) {
for(j in l.children) {
if(l.children[j].name == '_created_grid_') l.children[j].remove();
}
}
//break;
}
}
if(layer == null) {
layer = new Layer();
layer.name = '_grid_';
}
// create guides
var theArtboard;
group = new Group();
group.name = '_created_grid_';
if(values.bAllArtboards) {
// across all artboards
for(i in activeDocument.artboards) {
//print('all artboards', i);
theArtboard = activeDocument.artboards[i];
group.appendTop( createMargins(theArtboard) );
group.appendTop( createRowsCols(theArtboard) );
}
} else {
theArtboard = activeDocument.activeArtboard;
// active artboard
group.appendTop( createMargins(theArtboard) );
group.appendTop( createRowsCols(theArtboard) );
}
layer.appendTop(group);
layer.locked = true;
}
// ------------------------------------------------------------------------
// methods
// ------------------------------------------------------------------------
function createMargins(theArtboard) {
var bounds = theArtboard.bounds;
var active = activeDocument.activeArtboard;
var bw = bounds.width;
var bh = bounds.height;
var bx = bounds.x - active.bounds.x;
var by = bounds.y - active.bounds.y;
// ------------------------------------
// create margins
// ------------------------------------
groupMargins = new Group();
/**
* top & bottom margins
*/
var mTop = new Path.Line( new Point(bx, by+(bh-values.marginTop)), new Point(bx+bw, by+(bh-values.marginTop)) );
mTop.guide = true;
groupMargins.appendTop(mTop);
var mBottom = new Path.Line( new Point(bx, by+values.marginBottom), new Point(bx+bw, by+values.marginBottom) );
mBottom.guide = true;
groupMargins.appendTop(mBottom);
/**
* left & right margins
*/
var mLeft = new Path.Line( new Point(bx+values.marginLeft, by), new Point(bx+values.marginLeft, by+bh) );
mLeft.guide = true;
groupMargins.appendTop(mLeft);
var mRight = new Path.Line( new Point(bx+(bw-values.marginRight), by), new Point(bx+(bw-values.marginRight), by+bh) );
mRight.guide = true;
groupMargins.appendTop(mRight);
return groupMargins;
}
// ------------------------------------------------------------------------
function createRowsCols(theArtboard) {
var bounds = theArtboard.bounds;
var active = activeDocument.activeArtboard;
var bw = bounds.width;
var bh = bounds.height;
var bx = bounds.x - active.bounds.x;
var by = bounds.y - active.bounds.y;
var activeSpace = new Point( bw-(values.marginLeft+values.marginRight), bh-(values.marginTop+values.marginBottom) );
// ------------------------------------
// create columns & rows
// ------------------------------------
groupRowsCols = new Group();
/**
* cols
*/
if( values.cols > 0 ) {
var colGutterCombined = Math.abs( values.colGutter*(values.cols-1) );
activeSpace.x -= colGutterCombined;
activeSpace.x /= values.cols;
var x = values.marginLeft;
for(var i=0; i<values.cols-1; i++) {
x += activeSpace.x;
var gutterLeft = new Path.Line( new Point(bx+x, by), new Point(bx+x,by+bh) );
gutterLeft.guide = true;
groupRowsCols.appendTop(gutterLeft);
x += values.colGutter;
var gutterRight = new Path.Line( new Point(bx+x, by), new Point(bx+x,by+bh) );
gutterRight.guide = true;
groupRowsCols.appendTop(gutterRight);
}
}
/**
* rows
*/
if( values.rows > 0 ) {
var rowGutterCombined = Math.abs( values.rowGutter*(values.rows-1) );
activeSpace.y -= rowGutterCombined;
activeSpace.y /= values.rows;
var y = values.marginTop;
for(var i=0; i<values.rows-1; i++) {
y += activeSpace.y;
var gutterTop = new Path.Line( new Point(bx, by+y), new Point(bx+bw, by+y) );
gutterTop.guide = true;
groupRowsCols.appendTop(gutterTop);
y += values.rowGutter;
var gutterBottom = new Path.Line( new Point(bx, by+y), new Point(bx+bw, by+y) );
gutterBottom.guide = true;
groupRowsCols.appendTop(gutterBottom);
}
}
return groupRowsCols;
}
// ------------------------------------------------------------------------
function roundDecimal(orig,deci) {
var multi = Math.pow(10,deci);
var num = Math.round(orig * multi)/multi;
return num;
}
// ------------------------------------------------------------------------
function updatePalette(theUnit, theValDef) {
// print('unit', theUnit);
// print('val', theValDef);
// unit
palette.getComponent('marginTop').units = theUnit;
palette.getComponent('marginBottom').units = theUnit;
palette.getComponent('marginLeft').units = theUnit;
palette.getComponent('marginRight').units = theUnit;
palette.getComponent('rowGutter').units = theUnit;
palette.getComponent('colGutter').units = theUnit;
// value
palette.getComponent('marginTop').value = theValDef;
palette.getComponent('marginBottom').value = theValDef;
palette.getComponent('marginLeft').value = theValDef;
palette.getComponent('marginRight').value = theValDef;
palette.getComponent('rowGutter').value = theValDef;
palette.getComponent('colGutter').value = theValDef;
// reset
//palette.reset();
}
// ------------------------------------------------------------------------
// Execution
// ------------------------------------------------------------------------
Setup();
Update();
//Main();
@reneandritsch
Copy link

Dear @frederickk is this script still supposed to work in AI CC2019? I get an error. As I am not a developer and just have basic knowledge of scripts through the use in InDesign, changing the line 29 to “top-down” does not make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment