Advertisement
Cronos

Illustrator Insert Page Numbers v 2 Script

May 25th, 2012
9,997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #target illustrator
  2. //#targetengine main
  3.  
  4. // script.name = insertPageNumbers_v2.jsx; // works with CS4 & CS5
  5. // script description = Inserts page numbers (or any other text) to all artboards in the active document;
  6. // script.required = at least one open document
  7. // script.parent = CarlosCanto // 5/25/12;
  8. // script.elegant = false;
  9.  
  10. // script.updates = added options to insert Date, Time, Full File Name, & File Name;
  11. //                  added a button to clear previous entered data;
  12. //                   dialog stays open, to be able to input data in multiple locations
  13. //                   only one layer is created on multiple runs
  14.  
  15. // Notes: The script creates a new layer (Page Numbers) then adds a text frame per Artboard that act as footer or header text.
  16. //      Its primary function is to insert Page Numbers, but it could be used to insert any other kind of information.
  17.  
  18.  
  19.  
  20. if (app.documents.length > 0) // continue if there's at leat one document open
  21.     {
  22.         // start building User Interface
  23.         var win = new Window("dialog","mTools - Insert Page Numbers v 2");
  24.         var panelMargins = win.add("panel", undefined, "Margins");
  25.         var lblMargins = panelMargins.add("statictext",undefined,"How far from the edge:");
  26.         var txtMargins = panelMargins.add("edittext",undefined, 0.25);
  27.         var lblUnits = panelMargins.add("statictext",undefined,"inches");
  28.  
  29.         var panelLocation = win.add("panel", undefined, "Location");
  30.         var radTop = panelLocation.add("radiobutton",undefined,"Top");
  31.         var radBottom = panelLocation.add("radiobutton",undefined, "Bottom");
  32.  
  33.         var panelAlignment = win.add("panel", undefined, "Alignment");
  34.         var radLeft = panelAlignment.add("radiobutton",undefined,"Left");
  35.         var radCenter = panelAlignment.add("radiobutton",undefined, "Center");
  36.         var radRight = panelAlignment.add("radiobutton",undefined, "Right");
  37.  
  38.         var panelFooter = win.add("panel", undefined, "Text to insert");
  39.  
  40.         var grpPages = panelFooter.add("group");
  41.         var btnPage = grpPages.add("button",undefined,"P");
  42.         var btnPages = grpPages.add("button",undefined,"Ps");
  43.         var btnDate = grpPages.add("button",undefined,"D");
  44.         var btnTime = grpPages.add("button", undefined, "T");
  45.         var btnFullName = grpPages.add("button", undefined, "fFn");
  46.         var btnFile = grpPages.add("button", undefined, "Fn");
  47.  
  48.         var txtFooter = panelFooter.add("edittext"); //,undefined, "[Type text to insert here]");
  49.         var btnClear = panelFooter.add("button", undefined, "C");
  50.         btnPage.size = btnPages.size = btnDate.size = btnTime.size = btnFullName.size = btnFile.size = btnClear.size = [31,24]; // on Mac [31, 21] = round buttons, [31,24] = square buttons
  51.  
  52.         var btnOk = win.add("button", undefined, "Ok");
  53.  
  54.         radRight.value = radBottom.value = true;
  55.  
  56.         win.alignChildren = panelFooter.alignChildren = "fill";
  57.         btnClear.alignment = "left";
  58.         panelMargins.spacing = 3;
  59.         panelMargins.orientation = panelLocation.orientation = panelAlignment.orientation = "row";
  60.        
  61.         win.helpTip = "\u00A9 2012 Carlos Canto";
  62.          btnOk.helpTip = "Press Esc to Close";
  63.         btnPage.helpTip = "Adds *page* keyword, it represents a single page";
  64.         btnPages.helpTip = "Adds *pages* keyword, it represents total number of pages";
  65.         btnDate.helpTip = "Adds *date* keyword, it represents today's date";
  66.         btnTime.helpTip = "Adds *time* keyword, it represents current time";
  67.         btnFullName.helpTip = "Adds *fname* keyword, it represents Full File Name (including path)";
  68.         btnFile.helpTip = "Adds *file* keyword, it represents File Name";
  69.         btnClear.helpTip = "Clears input text area";
  70.         txtFooter.helpTip = "Type \r\t'Page *page* of *pages*' \rto get \r\t'Page 1 of 3' \rfor example";
  71.  
  72.  
  73.         //-----------------------------------------------------------------------------------------
  74.         // text place holder by Jongware / Marc Autret
  75.         var wgx = win.graphics;
  76.         var grayPen = wgx.newPen(wgx.PenType.SOLID_COLOR,[.67,.67,.67], 1);
  77.  
  78.         txtFooter.onDraw = function(/*DrawState*/)
  79.         {
  80.             var gx = this.graphics;
  81.             gx.drawOSControl();
  82.             this.text || this.active || gx.drawString("[Type text to insert here] Press Esc to close", grayPen, 0, 0);
  83.         };     
  84.        
  85.         //-----------------------------------------------------------------------------------------
  86.                         btnOk.onClick = function(){
  87.                             if (txtFooter.text != "")
  88.                             doSomething(); // call main function
  89.                             //win.close(); // close when done
  90.                          }
  91.         //-----------------------------------------------------------------------------------------
  92.        
  93.         //-----------------------------------------------------------------------------------------
  94.                         btnClear.onClick = function(){
  95.                             txtFooter.text = ""; // call main function
  96.                             //win.close(); // close when done
  97.                          }
  98.         //-----------------------------------------------------------------------------------------    
  99.         //-----------------------------------------------------------------------------------------
  100.                         btnPage.onClick = function(){
  101.                             footer("*page*");
  102.                          }
  103.         //-----------------------------------------------------------------------------------------
  104.         //-----------------------------------------------------------------------------------------
  105.                         btnPages.onClick = function(){
  106.                             footer("*pages*");
  107.                          }
  108.         //-----------------------------------------------------------------------------------------
  109.         //-----------------------------------------------------------------------------------------
  110.                         btnDate.onClick = function(){
  111.                             footer("*date*");
  112.                          }
  113.         //-----------------------------------------------------------------------------------------
  114.         //-----------------------------------------------------------------------------------------
  115.                         btnTime.onClick = function(){
  116.                             footer("*time*");
  117.                          }
  118.         //-----------------------------------------------------------------------------------------
  119.         //-----------------------------------------------------------------------------------------
  120.                         btnFullName.onClick = function(){
  121.                             footer("*fname*");
  122.                          }
  123.         //-----------------------------------------------------------------------------------------
  124.         //-----------------------------------------------------------------------------------------
  125.                         btnFile.onClick = function(){
  126.                             footer("*file*");
  127.                          }
  128.         //-----------------------------------------------------------------------------------------
  129.                         win.center();
  130.                         win.show();
  131.                        
  132.         //-----------------------------------------------------------------------------------------
  133.  
  134.  
  135.         function footer (page) //
  136.             {
  137.                 txtFooter.text = txtFooter.text + page;
  138.             }
  139.  
  140.         function doSomething()
  141.             {
  142.                 //alert("I'm doing something");
  143.                 var idoc = app.activeDocument;
  144.                 try {
  145.                     var ilayer = idoc.layers["Page Numbers"];
  146.                 } catch(e) {
  147.                     var ilayer = idoc.layers.add();
  148.                     ilayer.name = "Page Numbers";
  149.                 }
  150.  
  151.                 var pages = idoc.artboards.length; // number of artboards
  152.                 var datee = getdate ();
  153.                 var timee = gettime ();
  154.                 var fname = idoc.path == '' ? "Full Name: <unsaved document>" : idoc.fullName;
  155.                 var file = idoc.name;
  156.                
  157.                 var footerPages = (txtFooter.text).replace("*pages*",pages);
  158.                 //$.writeln(msg);
  159.                 footerPages = footerPages.replace("*pages*",pages); // replace the "*pages*" keyword with the actual number fo pages (artboards)
  160.                 footerPages = footerPages.replace("*date*",datee);
  161.                 footerPages = footerPages.replace("*time*",timee);
  162.                 footerPages = footerPages.replace("*fname*",fname);
  163.                 footerPages = footerPages.replace("*file*",file);
  164.                
  165.                 var margins = Number(txtMargins.text)*72; // get margins in points
  166.                 //$.writeln(margins);
  167.                
  168.                 for (i = 0; i<idoc.artboards.length; i++) // loop thru all artboards, and add input text from UI
  169.                     {
  170.                         footerPage = footerPages.replace("*page*",i+1); // replace "*page*" keyword with the actual page Number
  171.                    
  172.                         var itext = ilayer.textFrames.add();
  173.                         itext.contents = footerPage; //"page 1 of 1";              
  174.                         var fontSize = itext.textRange.characterAttributes.size;
  175.                        
  176.                         var activeAB = idoc.artboards[i];
  177.  
  178.                         var iartBounds = activeAB.artboardRect;
  179.                        
  180.                         var ableft = iartBounds[0]+margins;
  181.                         var abtop = iartBounds[1]-margins;
  182.                         var abright = iartBounds[2]-margins;
  183.                         var abbottom = iartBounds[3]+margins+fontSize;
  184.                        
  185.                         var abcenter = ableft+(abright-ableft)/2;
  186.                                
  187.  
  188.                
  189.                         if (radRight.value == true)
  190.                             {
  191.                                 //var msg = "right";
  192.                                 itext.left = abright;
  193.                                 itext.textRange.paragraphAttributes.justification = Justification.RIGHT;
  194.                             }
  195.                         else if (radCenter.value == true)
  196.                             {
  197.                                 //var msg = "center";
  198.                                 itext.left = abcenter;
  199.                                 itext.textRange.paragraphAttributes.justification = Justification.CENTER;
  200.                             }
  201.                         else
  202.                             {
  203.                                 //var msg = "Left";
  204.                                 itext.left = ableft;
  205.                                 itext.textRange.paragraphAttributes.justification = Justification.LEFT;
  206.                             }
  207.  
  208.                         if (radTop.value == true)
  209.                             {
  210.                                 //var msg = "top";
  211.                                 itext.top = abtop;
  212.                             }
  213.                         else
  214.                             {
  215.                                 //var msg = "bottom";
  216.                                 itext.top = abbottom;
  217.                             }
  218.                     } // end for loop thru all artboards
  219.                 app.redraw();
  220.             } // end function doSomething();
  221.      }
  222.  else
  223.     {
  224.         alert ("there's no open documents");
  225.     }
  226.  
  227. function getdate() {
  228.     var date = new(Date);
  229.     var m = date.getMonth()+1;
  230.     var d = date.getDate();
  231.     var y = date.getFullYear();
  232.     var datemdy = m+"/"+d+"/"+y;
  233.     return datemdy;
  234. //alert(date);
  235. //alert(m+"/"+d+"/"+y);
  236. }
  237.  
  238. function gettime() {
  239.     var time = new(Date);
  240.     var hours = time.getHours();
  241.     var minutes = time.getMinutes();
  242.     if (minutes < 10){
  243.         minutes = "0" + minutes
  244.     }
  245.  
  246.     if(hours > 11){
  247.         ampm = "PM";
  248.     } else {
  249.         ampm = "AM";
  250.     }  
  251.     var curtime = hours + ":" + minutes + " " + ampm;
  252.     return curtime;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement