Advertisement
Cronos

Illustrator re Arrange Artboards v2 Script

Dec 24th, 2012
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // var script.name = reArrangeArtboards_v2.jsx;
  2. // var script.description = reArranges the Artboards indexes;
  3. // var script.requirement = an open document with at least 2 artboards, CS4 & Up;
  4. // var script.tip = click on the Artboard tool, before running, to see the AB Indexes
  5. //  var script.parent = CarlosCanto; // 12/24/12
  6. //  var script.elegant = false;
  7.  
  8. #target Illustrator
  9.  
  10. var idoc = app.activeDocument;
  11. var abs = idoc.artboards;
  12. var abcount = abs.length; // save the artboard count
  13.  
  14. var abNames = [];    
  15. var abRects = [];
  16.  
  17. // add generic names for CS4. For CS5 and up use the actual names
  18. var ver = Number(app.version.split(".")[0]);
  19. if (ver<15) {
  20.     for (i=0; i<abcount; i++) {
  21.         abNames[i] = "Artboard " + (i+1); // CS4 - there's no way to get the initial order of the artboards, and no AB names in CS4
  22.         abRects[i] = abs[i].artboardRect;
  23.     }
  24. }
  25. else {
  26.     for (i=0; i<abcount; i++) {
  27.         abNames[i] = abs[i].name; // CS5 - to update artboard names
  28.         abRects[i] = abs[i].artboardRect;
  29.     }
  30. }
  31.  
  32. var win = new Window('dialog', 'Arrange Artbooards', undefined, {resizeable:true});
  33. var lblhelp = win.add('statictext', undefined, ' use up and down \r arrows to navigate, \r + Shift to move', {multiline:true});
  34. var ddL = win.add('listbox',undefined,abNames);
  35. addIdxToDDL (ddL); // add a fixed index to each list item
  36. ddL.items[0].selected = true;
  37. ddL.active = true;
  38. var btnArrange = win.add('button', undefined, 'Re Arrange');
  39.  
  40. ddL.addEventListener("keydown", checkSwapping);
  41.  
  42. var currSelection = null;
  43. var prevSelection = null;
  44. var prevSelIndex = null;
  45. var swap = false;
  46.  
  47. ddL.alignment = ["fill","fill"];
  48. win.preferredSize = [100,-1];
  49. win.spacing = 6;
  50. win.margins = 6;
  51. win.alignChildren = ["fill", "bottom"];
  52. win.helpTip = "\u00A9 2012 Carlos Canto";
  53. btnArrange.helpTip = "Press Esc to Close";
  54.  
  55. // add a fixed index to each list item
  56. function addIdxToDDL (ddL) {
  57.     for (m=0; m<ddL.items.length; m++) {
  58.         ddL.items[m].idx = m;
  59.     }
  60. }
  61.  
  62. // allow swapping if the Shift key is pressed
  63. function checkSwapping(event){
  64.     //alert(event.keyName);
  65.     if (event.shiftKey && ((event.keyName=="Up" && ddL.selection.index>0 )|| (event.keyName=="Down" && ddL.selection.index<abcount-1))) {
  66.         prevSelection = ddL.selection.text;
  67.         prevSelIndex = ddL.selection.index;
  68.         prevSelIdx = ddL.selection.idx;
  69.         swap = true;
  70.      }
  71. }
  72.  
  73. ddL.onChange = function () {
  74.     if (swap) {
  75.         ddL.items[prevSelIndex].text = ddL.selection.text;
  76.         ddL.selection.text = prevSelection;
  77.         swap = false;
  78.         ddL.items[prevSelIndex].idx = ddL.selection.idx;
  79.         ddL.selection.idx = prevSelIdx;
  80.     }
  81. }
  82.  
  83. btnArrange.onClick = function(){
  84.     for (j=0, k=abcount-1; j<abcount; j++, k--) {
  85.         var idx = ddL.items[j].idx; // get hardcoded index of the first list item
  86.         var abRect = abRects[idx]; // get artboard rect
  87.         idoc.artboards.remove(k); // remove the last artboard
  88.         var newab = idoc.artboards.add(abRect);
  89.         newab.name = abNames[idx]; // this has no effect in CS4, it does not support name property
  90.     }
  91.     app.redraw();
  92.     win.close();
  93. }
  94.  
  95.  
  96. win.onResizing = function () {this.layout.resize();}
  97. win.onShow = function () {win.layout.resize();}
  98.  
  99. win.center();
  100. win.show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement