Advertisement
cjamesrun

fitObjectToArtboardBounds_v2b

Jan 7th, 2013
1,250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  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. // to maintain aspect ..
  16. // find landscape or portrait (width - margins > height - margins = landscape) (Width - margin < Height -margin = portrait)
  17. // if portrait scale to height - margins with aspect ratio to desired height
  18. // if landscape scale to width - margins with aspect ratio
  19. // center object
  20.  
  21.  
  22. var idoc = app.activeDocument;
  23. selec = idoc.selection;
  24. if (selec.length>0) {
  25.  
  26. var title = "Fit Selected Object(s) to Artboard(s)";
  27. var msg = "Enter Margins in points\n";
  28. msg += "Positive: Outside Artboard, Negative: Inside Artboard";
  29. msg += "\nEnter 0 to fit exactly to Artboard Bounds";
  30.  
  31. var margins = Number(Window.prompt (msg, -20, title));
  32.  
  33. var aspect = Window.confirm ("Yes - Maintain Aspect \nNo - Fill Artboard", false, title);
  34.  
  35. var allArtboards = Window.confirm ("Yes - All Artboards \nNo - Active Artboard", false, title);
  36.  
  37. for (j=0; j<selec.length; j++) {
  38. // get selection bounds
  39. var boundsdiff = getPgItemGBoundsvsVBoundsDifference (selec[j]);
  40.  
  41. if (allArtboards) {
  42. //alert(allArtboards);
  43. if (aspect) {
  44. // alert(aspect);
  45. var ABs = idoc.artboards;
  46. for(i=0; i<ABs.length; i++) {
  47. var activeAB = ABs[i];
  48. var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
  49.  
  50. var sel = selec[j].duplicate(); // idoc.selection[0];
  51. fitPgItemRatio (sel, ABprops, boundsdiff);
  52. }
  53. } else {
  54. //alert(aspect);
  55. var ABs = idoc.artboards;
  56. for(i=0; i<ABs.length; i++) {
  57. var activeAB = ABs[i];
  58. var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
  59.  
  60. var sel = selec[j].duplicate(); // idoc.selection[0];
  61. fitPgItem (sel, ABprops, boundsdiff);
  62. }
  63.  
  64. }
  65. }
  66. else {
  67. //alert(allArtboards);
  68. if (aspect) {
  69.  
  70. var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
  71. var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
  72. var sel = selec[j].duplicate(); // idoc.selection[0];
  73. fitPgItemRatio (sel, ABprops, boundsdiff);
  74.  
  75. } else {
  76. //alert(aspect);
  77. var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
  78. var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
  79. var sel = selec[j].duplicate(); // idoc.selection[0];
  80. fitPgItem (sel, ABprops, boundsdiff);
  81.  
  82. }
  83.  
  84. }
  85. } // end if there's something selected
  86.  
  87. //idoc.selection = null;
  88. //selec[0].selected = true;
  89. }
  90. else {
  91. alert("select something before running");
  92. }
  93.  
  94. //--------------------------------------------------------------------------------------------------------
  95. // move and resize pgItem
  96. function fitPgItem(pgItem, containerProps, pgItemBoundsDiff) {
  97. var sel = pgItem;
  98. var ABprops = containerProps;
  99. var boundsdiff = pgItemBoundsDiff;
  100.  
  101. sel.width = ABprops.width-boundsdiff.deltaX; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
  102. sel.height = ABprops.height-boundsdiff.deltaY;
  103. sel.top = ABprops.top; // Top is actually Visible top
  104. sel.left = ABprops.left; // dito for Left
  105. sel.selected = false;
  106. }
  107. //--------------------------------------------------------------------------------------------------------
  108.  
  109. //--------------------------------------------------------------------------------------------------------
  110. // move and resize pgItem
  111. function fitPgItemRatio(pgItem, containerProps, pgItemBoundsDiff) {
  112. var sel = pgItem;
  113. var ABprops = containerProps;
  114. var boundsdiff = pgItemBoundsDiff;
  115. var abBounds1 = activeAB.artboardRect;
  116. var ableft1 = abBounds1[0];
  117. var abright1 = abBounds1[2];
  118. var abwidth1 = abright1-ableft1;
  119. var abtop1 = abBounds1[1];
  120. var abbottom1 = abBounds1[3];
  121. var abheight1 = abtop1-abbottom1;
  122.  
  123.  
  124.  
  125.  
  126. if (sel.width > sel.height) { // image is landscape scale the image to max width
  127. alert("Landscape");
  128.  
  129.  
  130. var scale = ( (parseFloat(ABprops.width-boundsdiff.deltaX) / parseFloat(sel.width) * 100).toFixed(2)); // How much do I need to scale
  131. sel.resize(
  132. scale , // x
  133. scale , // y
  134. true, // changePositions
  135. true, // changeFillPatterns
  136. true, // changeFillGradients
  137. true, // changeStrokePattern
  138. scale , // changeLineWidths <---- NOTE THIS
  139. undefined); // scaleAbout
  140.  
  141. // Check to see if it vits within the bounds and rescale down if needed.
  142.  
  143. if (sel.height > ABprops.height-boundsdiff.deltaY) {
  144. var scale = ( (parseFloat(ABprops.height-boundsdiff.deltaY)/parseFloat(sel.width) * 100).toFixed(2) ); // How MUCH MORE do I need to scale
  145.  
  146. sel.resize(
  147. scale , // x
  148. scale , // y
  149. true, // changePositions
  150. true, // changeFillPatterns
  151. true, // changeFillGradients
  152. true, // changeStrokePattern
  153. scale , // changeLineWidths <---- NOTE THIS
  154. undefined); // scaleAbout
  155. }
  156.  
  157. sel.top = (parseInt(abheight1)/2 ) - (parseFloat(sel.height)/2) * -1;
  158. //alert (parseFloat(ABprops.height-boundsdiff.deltaY)/2 + " - " + parseFloat(sel.height)/2 + "=" +sel.top);
  159. sel.left = ABprops.left
  160.  
  161.  
  162. sel.selected = false;
  163. } else { // image is portrait
  164. //alert("Portrait");
  165. //alert(ABprops.height-boundsdiff.deltaY + "Bounds diff");
  166. var scale = ( (parseFloat(ABprops.height-boundsdiff.deltaY)/parseFloat(sel.height) * 100).toFixed(2) ); // How much do I need to scale
  167.  
  168. //alert (ABprops.height-boundsdiff.deltaY + " / " + sel.height + "=" +scale);
  169. sel.resize(
  170. scale , // x
  171. scale , // y
  172. true, // changePositions
  173. true, // changeFillPatterns
  174. true, // changeFillGradients
  175. true, // changeStrokePattern
  176. scale , // changeLineWidths <---- NOTE THIS
  177. undefined); // scaleAbout
  178.  
  179. // Check to see if it vits within the bounds and rescale down if needed.
  180.  
  181. if (sel.width > ABprops.width-boundsdiff.deltaX) {
  182. var scale = ( (parseFloat(ABprops.width-boundsdiff.deltaX)/parseFloat(sel.width) * 100).toFixed(2) ); // How MUCH MORE do I need to scale
  183.  
  184. sel.resize(
  185. scale , // x
  186. scale , // y
  187. true, // changePositions
  188. true, // changeFillPatterns
  189. true, // changeFillGradients
  190. true, // changeStrokePattern
  191. scale , // changeLineWidths <---- NOTE THIS
  192. undefined); // scaleAbout
  193. }
  194.  
  195.  
  196.  
  197. sel.top = ( (parseInt(abheight1)/2 ) + ((parseInt(sel.height)/2)));
  198.  
  199. //alert (parseInt(abheight1) + " - "+ parseInt(sel.height) + " - "+ sel.top);
  200. sel.left = (parseInt(abwidth1)/2 ) - ((parseInt(sel.width)/2));
  201.  
  202. // alert ((ABprops.width) + " - " + parseInt(parseFloat(sel.width)/2) + "=" +sel.left + " Width = "+sel.width);
  203.  
  204. sel.selected = false;
  205.  
  206. }
  207.  
  208. // sel.width = ABprops.width-boundsdiff.deltaX; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
  209. // sel.height = ABprops.height-boundsdiff.deltaY;
  210. //alert (ABprops.height-boundsdiff.deltaY);
  211. //sel.top = ABprops.top; // Top is actually Visible top
  212. //sel.left = ABprops.left; // dito for Left
  213. // sel.selected = false;
  214. }
  215. //--------------------------------------------------------------------------------------------------------
  216.  
  217.  
  218. //--------------------------------------------------------------------------------------------------------
  219. //returns a pageItem's VisibleBounds vs GeometricBounds Difference
  220. // needed to substract from an item's width/height since they're based on Geometric width/height
  221. function getPgItemGBoundsvsVBoundsDifference (pgitem) {
  222. // get selection bounds
  223. var sel = pgitem;
  224. var selVB = sel.visibleBounds;
  225. var selVw = selVB[2]-selVB[0];
  226. var selVh = selVB[1]-selVB[3];
  227.  
  228. var selGB = sel.geometricBounds;
  229. var selGw = selGB[2]-selGB[0];
  230. var selGh = selGB[1]-selGB[3];
  231.  
  232. // get the difference between Visible & Geometric Bounds
  233. var deltaX = selVw-selGw;
  234. var deltaY = selVh-selGh;
  235.  
  236. return deltaxy = {deltaX:deltaX, deltaY:deltaY};
  237. }
  238. //--------------------------------------------------------------------------------------------------------
  239.  
  240. //--------------------------------------------------------------------------------------------------------
  241. // returns an Artboard with top, left, width & height properties, including Margins
  242. function getABbounds (artboard, padding) {
  243. var activeAB = artboard;
  244. var margins = padding;
  245.  
  246. var abBounds = activeAB.artboardRect; // get bounds [left, top, right, bottom]
  247. var ableft = abBounds[0]-margins;
  248. var abtop = abBounds[1]+margins;
  249. var abright = abBounds[2]+margins;
  250. var abbottom = abBounds[3]-margins;
  251.  
  252. var abwidth = abright-ableft;
  253. var abheight = abtop-abbottom;
  254.  
  255. return AB = {left:ableft, top:abtop, width:abwidth, height:abheight};
  256. }
  257. //--------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement