here you go, this version will add 3 guides per side, full size as entered in the UI, then 2 more at half the previous size. ie if you enter 1/2", then two more guides will be created at 1/4" and 1/8"
// add guides by eiranix
// https://community.adobe.com/t5/illustrator-discussions/add-margins-in-illustrator/m-p/14543246#M403970
// v2 - this version will create 3 guides per side as per defaultfrzzkd6qqsht request
var docRef = app.activeDocument;
var artboardRef = docRef.artboards;
var workingLayer = docRef.activeLayer;
//window
var win = new Window('dialog', "Add Margins");
this.windowRef = win;
//panels
win.fieldpanel = win.add("panel", undefined, "Sizes");
win.radiopanel = win.add("panel", undefined, "Units");
win.optionpanel = win.add("panel", undefined, "Options");
win.optionpanel.radiopanel2 = win.optionpanel.add("panel", undefined, "Artboard");
win.optionpanel.radiopanel3 = win.optionpanel.add("panel", undefined, "Margin Type");
//panel orientation
win.fieldpanel.orientation='row';
win.radiopanel.orientation='row';
win.optionpanel.radiopanel2.orientation='row';
win.optionpanel.radiopanel3.orientation='row';
win.optionpanel.orientation='row';
//fieldpanel
win.fieldpanel.panel1 = win.fieldpanel.add('panel', undefined, "Left");
win.fieldpanel.panel2 = win.fieldpanel.add('panel', undefined, "Right");
win.fieldpanel.panel3 = win.fieldpanel.add('panel', undefined, "Top");
win.fieldpanel.panel4 = win.fieldpanel.add('panel', undefined, "Bottom");
win.fieldpanel.panel1.left_input = win.fieldpanel.panel1.add('edittext', undefined, "1");
win.fieldpanel.panel2.right_input = win.fieldpanel.panel2.add('edittext', undefined, "0");
win.fieldpanel.panel3.top_input = win.fieldpanel.panel3.add('edittext', undefined, "0");
win.fieldpanel.panel4.bottom_input = win.fieldpanel.panel4.add('edittext', undefined, "0");
win.fieldpanel.panel1.left_input.characters = 5;
win.fieldpanel.panel2.right_input.characters = 5;
win.fieldpanel.panel3.top_input.characters = 5;
win.fieldpanel.panel4.bottom_input.characters = 5;
win.fieldpanel.check1 = win.fieldpanel.add('checkbox', undefined, "Equal");
//radiopanel
win.radiopanel.radio1 = win.radiopanel.add('radiobutton',undefined, "mm");
win.radiopanel.radio2 = win.radiopanel.add('radiobutton',undefined, "in");
win.radiopanel.radio3 = win.radiopanel.add('radiobutton',undefined, "px");
//radiopanel2
win.optionpanel.radiopanel2.radio1 = win.optionpanel.radiopanel2.add('radiobutton',undefined, "All Artboards");
win.optionpanel.radiopanel2.radio2 = win.optionpanel.radiopanel2.add('radiobutton',undefined, "Active Artboard");
//radiopanel3
win.optionpanel.radiopanel3.radio1 = win.optionpanel.radiopanel3.add('radiobutton',undefined, "Margin Lines");
win.optionpanel.radiopanel3.radio2 = win.optionpanel.radiopanel3.add('radiobutton',undefined, "Margin Box");
//select first radio buttons
win.radiopanel.radio2.value = true; // inches
win.optionpanel.radiopanel2.radio1.value = true;
win.optionpanel.radiopanel3.radio1.value = true;
//buttons
win.okbutton = win.add('button', undefined, "Ok");
//disable fields with checkbox and equal values
win.fieldpanel.check1.onClick = function() {
if(win.fieldpanel.check1.value){
var leftvalue = win.fieldpanel.panel1.left_input.text;
win.fieldpanel.panel2.right_input.text = leftvalue;
win.fieldpanel.panel3.top_input.text = leftvalue;
win.fieldpanel.panel4.bottom_input.text = leftvalue;
win.fieldpanel.panel2.right_input.enabled = false;
win.fieldpanel.panel3.top_input.enabled = false;
win.fieldpanel.panel4.bottom_input.enabled = false;
} else {
win.fieldpanel.panel2.right_input.enabled = true;
win.fieldpanel.panel3.top_input.enabled = true;
win.fieldpanel.panel4.bottom_input.enabled = true;
}
};
//sync values while checked
win.fieldpanel.panel1.left_input.onChanging = function (){
if(win.fieldpanel.check1.value){
var leftvalue = win.fieldpanel.panel1.left_input.text;
win.fieldpanel.panel2.right_input.text = leftvalue;
win.fieldpanel.panel3.top_input.text = leftvalue;
win.fieldpanel.panel4.bottom_input.text = leftvalue;
}
};
//event listener for ok button
win.okbutton.onClick = function(){
var leftvalue = win.fieldpanel.panel1.left_input.text;
var rightvalue = win.fieldpanel.panel2.right_input.text;
var topvalue = win.fieldpanel.panel3.top_input.text;
var bottomvalue = win.fieldpanel.panel4.bottom_input.text;
// carlos edit for defaultfrzzkd6qqsht, to add 1" but also 1/2" and 1/4"
var sizehalving = [1,2,2];
//make a margins layer
var guideLayer = docRef.layers.add();
guideLayer.name = "Margins";
for (var bb=0; bb<sizehalving.length; bb++) {
leftvalue = leftvalue/sizehalving[bb];
rightvalue = rightvalue/sizehalving[bb];
topvalue = topvalue/sizehalving[bb];
bottomvalue = bottomvalue/sizehalving[bb];
if(win.radiopanel.radio1.value) {
var multiplier = 2.834645669291339
}
if(win.radiopanel.radio2.value) {
var multiplier = 72
}
if(win.radiopanel.radio3.value) {
var multiplier = 1
}
if(win.optionpanel.radiopanel2.radio1.value) {
//repeat for each artboard
for(i=0;i<artboardRef.length;i++){
//get artboard size
var left = artboardRef[i].artboardRect[0];
var top = artboardRef[i].artboardRect[1] ;
var right = artboardRef[i].artboardRect[2] ;
var bottom = artboardRef[i].artboardRect[3] ;
var width = artboardRef[i].artboardRect[2]-artboardRef[i].artboardRect[0];
var height = artboardRef[i].artboardRect[1]-artboardRef[i].artboardRect[3];
//create lines
var lineLeft = docRef.pathItems.add();
var lineRight = docRef.pathItems.add();
var lineTop = docRef.pathItems.add();
var lineBottom = docRef.pathItems.add();
//set margin values
var leftmargin = (leftvalue * multiplier);
var rightmargin = (rightvalue * multiplier);
var topmargin = (topvalue * multiplier);
var bottommargin = (bottomvalue * multiplier);
if(win.optionpanel.radiopanel3.radio1.value) {
//set line points
lineLeft.setEntirePath([[left + leftmargin, top], [left + leftmargin, bottom]]);
lineRight.setEntirePath([[right - rightmargin, top], [right - rightmargin, bottom]]);
lineTop.setEntirePath([[left, top - topmargin], [right, top - topmargin]]);
lineBottom.setEntirePath([[left, bottom + bottommargin], [right, bottom + bottommargin]]);
//make lines guides
lineLeft.guides = true;
lineRight.guides = true;
lineTop.guides = true;
lineBottom.guides = true;
} else {
//create box
var box = docRef.pathItems.rectangle (top - topmargin, left + leftmargin, width - rightmargin - leftmargin, height - topmargin - bottommargin);
box.fillColor = box.strokeColor = new NoColor();
//make box guides
box.guides = true;
};
};
} else {
//get artboard size
var activeAB = docRef.artboards[docRef.artboards.getActiveArtboardIndex()]; // get active AB
var left = activeAB.artboardRect[0];
var top = activeAB.artboardRect[1] ;
var right = activeAB.artboardRect[2] ;
var bottom = activeAB.artboardRect[3] ;
var width = activeAB.artboardRect[2]-activeAB.artboardRect[0];
var height = activeAB.artboardRect[1]-activeAB.artboardRect[3];
//create lines
var lineLeft = docRef.pathItems.add();
var lineRight = docRef.pathItems.add();
var lineTop = docRef.pathItems.add();
var lineBottom = docRef.pathItems.add();
//set margin values
var leftmargin = (leftvalue * multiplier);
var rightmargin = (rightvalue * multiplier);
var topmargin = (topvalue * multiplier);
var bottommargin = (bottomvalue * multiplier);
if(win.optionpanel.radiopanel3.radio1.value) {
//set line points
lineLeft.setEntirePath([[left + leftmargin, top], [left + leftmargin, bottom]]);
lineRight.setEntirePath([[right - rightmargin, top], [right - rightmargin, bottom]]);
lineTop.setEntirePath([[left, top - topmargin], [right, top - topmargin]]);
lineBottom.setEntirePath([[left, bottom + bottommargin], [right, bottom + bottommargin]]);
//make lines guides
lineLeft.guides = true;
lineRight.guides = true;
lineTop.guides = true;
lineBottom.guides = true;
} else {
//create box
var box = docRef.pathItems.rectangle (top - topmargin, left + leftmargin, width - rightmargin - leftmargin, height - topmargin - bottommargin);
box.fillColor = box.strokeColor = new NoColor();
//make box guides
box.guides = true;
};
};
}; // end carlos edit
//lock margins layer and activate original layer
guideLayer.zOrder (ZOrderMethod.SENDTOBACK);
guideLayer.locked = true;
docRef.activeLayer = workingLayer;
//close window
win.close();
};
win.show();
... View more