4 Replies Latest reply on Feb 21, 2017 6:54 PM by tulsagraphics

    Add Margins in Illustrator

    Eiranix Level 1

      I have made this little script to quickly add margins to an illustrator document and am posting it here to anyone that might find this helpful.

       

      The script will bring up a dialog where you can enter the sizes. Checking the equal box makes all sizes the same as the first.

      You have the option of using Millimetres, Inches or Pixels for your units and a last option to apply to all artboards or just the active one.

       

      The script then adds the guides to a locked layer called 'Margins' for convenience.

       

      There are two versions for your taste- The first creates 4 seperate guidelines. The second creates one rectangle guide.

       

      #target illustrator
      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, "");
      win.radiopanel = win.add("panel", undefined, "");
      win.radiopanel2 = win.add("panel", undefined, "");
      
      //panel orientation
      win.fieldpanel.orientation='row';
      win.radiopanel.orientation='row';
      win.radiopanel2.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, "0");
      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.radiopanel2.radio1 = win.radiopanel2.add('radiobutton',undefined, "All Artboards"); 
      win.radiopanel2.radio2 = win.radiopanel2.add('radiobutton',undefined, "Active Artboard");
      
      //select first radio buttons
      win.radiopanel.radio1.value = true;
      win.radiopanel2.radio1.value = true;
      
      //ok button
      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;
        
        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
        }
      
        //close window
        win.close();
        
        //make a margins layer
        var guideLayer = docRef.layers.add();
        guideLayer.name = "Margins";
        
        
        if(win.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] ;
      
      
            //create lines
            var lineLeft = docRef.pathItems.add();
            var lineRight = docRef.pathItems.add();
            var lineTop = docRef.pathItems.add();
            var lineBottom = docRef.pathItems.add();
      
            //set line points
            var leftmargin = (leftvalue * multiplier);
            var rightmargin = (rightvalue * multiplier);
            var topmargin = (topvalue * multiplier);
            var bottommargin = (bottomvalue * multiplier);
            
            //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 {
          //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] ;
      
          //create lines
          var lineLeft = docRef.pathItems.add();
          var lineRight = docRef.pathItems.add();
          var lineTop = docRef.pathItems.add();
          var lineBottom = docRef.pathItems.add();
      
          //set line points
          var leftmargin = (leftvalue * multiplier);
          var rightmargin = (rightvalue * multiplier);
          var topmargin = (topvalue * multiplier);
          var bottommargin = (bottomvalue * multiplier);
      
          //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;
        };
        
        //lock margins layer and activate original layer
        guideLayer.zOrder (ZOrderMethod.SENDTOBACK);
        guideLayer.locked = true;
        docRef.activeLayer = workingLayer;
        
       };
      
      win.show();
      

       

       

      Box Margin Version:

      #target illustrator
      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, "");
      win.radiopanel = win.add("panel", undefined, "");
      win.radiopanel2 = win.add("panel", undefined, "");
      
      //panel orientation
      win.fieldpanel.orientation='row';
      win.radiopanel.orientation='row';
      win.radiopanel2.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, "0");
      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.radiopanel2.radio1 = win.radiopanel2.add('radiobutton',undefined, "All Artboards"); 
      win.radiopanel2.radio2 = win.radiopanel2.add('radiobutton',undefined, "Active Artboard");
      
      //select first radio buttons
      win.radiopanel.radio1.value = true;
      win.radiopanel2.radio1.value = true;
      
      //ok button
      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;
        
        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
        }
      
        //close window
        win.close();
        
        //make a margins layer
        var guideLayer = docRef.layers.add();
        guideLayer.name = "Margins";
        
        
        if(win.radiopanel2.radio1.value) {
         
          //repeat for each artboard
          for(i=0;i<artboardRef.length;i++){
      
            //get artboard size
            var top=artboardRef[i].artboardRect[1] ;
            var left=artboardRef[i].artboardRect[0];
            var width=artboardRef[i].artboardRect[2]-artboardRef[i].artboardRect[0];
            var height=artboardRef[i].artboardRect[1]-artboardRef[i].artboardRect[3];
            
            //set margin sizes
            var leftmargin = (leftvalue * multiplier);
            var rightmargin = (rightvalue * multiplier);
            var topmargin = (topvalue * multiplier);
            var bottommargin = (bottomvalue * multiplier);
            
            //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 width = activeAB.artboardRect[2]-activeAB.artboardRect[0];
          var height = activeAB.artboardRect[1]-activeAB.artboardRect[3];
          
          //set margin sizes
          var leftmargin = (leftvalue * multiplier);
          var rightmargin = (rightvalue * multiplier);
          var topmargin = (topvalue * multiplier);
          var bottommargin = (bottomvalue * multiplier);
          
          //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;
        };
        
        //lock margins layer and activate original layer
        guideLayer.zOrder (ZOrderMethod.SENDTOBACK);
        guideLayer.locked = true;
        docRef.activeLayer = workingLayer;
        
       };
      
      win.show();
      
      
        • 1. Re: Add Margins in Illustrator
          CarlosCanto Adobe Community Professional & MVP

          nice, thanks for sharing.

           

          may I suggest something? combine them into one!!

          • 2. Re: Add Margins in Illustrator
            Eiranix Level 1

            Yes that would be sensible! don't know why I didn't...

             

            Here is a combined script:

             

            #target illustrator
            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, "0");
            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.radio1.value = true;
            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;
            
              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
              }
            
              //make a margins layer
              var guideLayer = docRef.layers.add();
              guideLayer.name = "Margins";
            
            
              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;
                };
              };
            
              //lock margins layer and activate original layer
              guideLayer.zOrder (ZOrderMethod.SENDTOBACK);
              guideLayer.locked = true;
              docRef.activeLayer = workingLayer;
            
              //close window
              win.close();
             };
            
            win.show();
            
            1 person found this helpful
            • 3. Re: Add Margins in Illustrator
              CarlosCanto Adobe Community Professional & MVP

              thanks, much more efficient.

               

              thanks

              • 4. Re: Add Margins in Illustrator
                tulsagraphics

                I know it's been a few years, but I just discovered your script.  Very handy.  Thank you!!