Advertisement
Cronos

Illustrator Fit Selected Object(s) to Artboard(s) v 2 Script

Aug 5th, 2012
1,383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #target Illustrator
  2.  
  3. //  script.name = fitObjectToArtboardBounds_v2.jsx;
  4. //  script.description = resizes selected object(s) to fit to the Artboard(s) Bounds;
  5. //  script.required = select something before running; CS4 & CS5 Only.
  6. //  script.parent = carlos canto // 08/05/12;
  7. //  script.elegant = false;
  8.  
  9.  
  10. // script.updates = first version fitted a single object to the active artboard.
  11. //                  for this version, I added options for fitting selected object to all the artboards.
  12. //                   it also works with multiple selections, all selected objects will be processed at once.
  13. //                   Bonus: there's an option for adding padding to object's final size.
  14.  
  15.  
  16. var idoc = app.activeDocument;
  17. selec = idoc.selection;
  18. if (selec.length>0) {
  19.    
  20.     var title = "Fit Selected Object(s) to Artboard(s)";
  21.     var msg = "Enter Margins in points\n";
  22.     msg += "Positive: Outside Artboard, Negative: Inside Artboard";
  23.     msg += "\nEnter 0 to fit exactly to Artboard Bounds";
  24.  
  25.     var margins = Number(Window.prompt (msg, 0, title));
  26.    
  27.     var allArtboards = Window.confirm ("Yes - All Artboards \nNo - Active Artboard", false, title);
  28.    
  29.     for (j=0; j<selec.length; j++) {
  30.         // get selection bounds
  31.         var boundsdiff = getPgItemGBoundsvsVBoundsDifference (selec[j]);
  32.        
  33.         if (allArtboards) {
  34.             //alert(allArtboards);
  35.  
  36.             var ABs = idoc.artboards;
  37.             for(i=0; i<ABs.length; i++) {
  38.                 var activeAB = ABs[i];
  39.                 var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
  40.                
  41.                 var sel = selec[j].duplicate(); // idoc.selection[0];
  42.                 fitPgItem (sel, ABprops, boundsdiff);
  43.             }
  44.         }
  45.         else {
  46.             //alert(allArtboards);
  47.             var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
  48.             var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
  49.             var sel = selec[j].duplicate(); // idoc.selection[0];
  50.             fitPgItem (sel, ABprops, boundsdiff);
  51.         }
  52.     } // end if there's something selected
  53.  
  54.     //idoc.selection = null;
  55.     //selec[0].selected = true;
  56. }
  57. else {
  58.     alert("select something before running");
  59. }
  60.  
  61. //--------------------------------------------------------------------------------------------------------
  62. // move and resize pgItem
  63. function fitPgItem(pgItem, containerProps, pgItemBoundsDiff) {
  64.     var sel = pgItem;
  65.     var ABprops = containerProps;
  66.     var boundsdiff = pgItemBoundsDiff;
  67.    
  68.     sel.width = ABprops.width-boundsdiff.deltaX; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
  69.     sel.height = ABprops.height-boundsdiff.deltaY;
  70.     sel.top = ABprops.top; // Top is actually Visible top
  71.     sel.left = ABprops.left; // dito for Left
  72.     sel.selected = false;
  73. }
  74. //--------------------------------------------------------------------------------------------------------
  75.  
  76. //--------------------------------------------------------------------------------------------------------
  77. //returns a pageItem's VisibleBounds vs GeometricBounds Difference
  78. // needed to substract from an item's width/height since they're based on Geometric width/height
  79. function getPgItemGBoundsvsVBoundsDifference (pgitem) {
  80.     // get selection bounds
  81.     var sel = pgitem;
  82.     var selVB = sel.visibleBounds;
  83.     var selVw = selVB[2]-selVB[0];
  84.     var selVh = selVB[1]-selVB[3];
  85.  
  86.     var selGB = sel.geometricBounds;
  87.     var selGw = selGB[2]-selGB[0];
  88.     var selGh = selGB[1]-selGB[3];
  89.  
  90.     // get the difference between Visible & Geometric Bounds
  91.     var deltaX = selVw-selGw;
  92.     var deltaY = selVh-selGh;
  93.    
  94.     return deltaxy = {deltaX:deltaX, deltaY:deltaY};
  95. }
  96. //--------------------------------------------------------------------------------------------------------
  97.  
  98. //--------------------------------------------------------------------------------------------------------
  99. // returns an Artboard with top, left, width & height properties, including Margins
  100. function getABbounds (artboard, padding) {
  101.     var activeAB = artboard;
  102.     var margins = padding;
  103.    
  104.     var abBounds = activeAB.artboardRect; // get bounds [left, top, right, bottom]
  105.     var ableft = abBounds[0]-margins;
  106.     var abtop = abBounds[1]+margins;
  107.     var abright = abBounds[2]+margins;
  108.     var abbottom = abBounds[3]-margins;
  109.  
  110.     var abwidth = abright-ableft;
  111.     var abheight = abtop-abbottom;
  112.  
  113.     return AB = {left:ableft, top:abtop, width:abwidth, height:abheight};
  114. }
  115. //--------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement