Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #target Illustrator
- // script.name = fitObjectToArtboardBounds_v2.jsx;
- // script.description = resizes selected object(s) to fit to the Artboard(s) Bounds;
- // script.required = select something before running; CS4 & CS5 Only.
- // script.parent = carlos canto // 08/05/12;
- // script.elegant = false;
- // script.updates = first version fitted a single object to the active artboard.
- // for this version, I added options for fitting selected object to all the artboards.
- // it also works with multiple selections, all selected objects will be processed at once.
- // Bonus: there's an option for adding padding to object's final size.
- // to maintain aspect ..
- // find landscape or portrait (width - margins > height - margins = landscape) (Width - margin < Height -margin = portrait)
- // if portrait scale to height - margins with aspect ratio to desired height
- // if landscape scale to width - margins with aspect ratio
- // center object
- var idoc = app.activeDocument;
- selec = idoc.selection;
- if (selec.length>0) {
- var title = "Fit Selected Object(s) to Artboard(s)";
- var msg = "Enter Margins in points\n";
- msg += "Positive: Outside Artboard, Negative: Inside Artboard";
- msg += "\nEnter 0 to fit exactly to Artboard Bounds";
- var margins = Number(Window.prompt (msg, -20, title));
- var aspect = Window.confirm ("Yes - Maintain Aspect \nNo - Fill Artboard", false, title);
- var allArtboards = Window.confirm ("Yes - All Artboards \nNo - Active Artboard", false, title);
- for (j=0; j<selec.length; j++) {
- // get selection bounds
- var boundsdiff = getPgItemGBoundsvsVBoundsDifference (selec[j]);
- if (allArtboards) {
- //alert(allArtboards);
- if (aspect) {
- // alert(aspect);
- var ABs = idoc.artboards;
- for(i=0; i<ABs.length; i++) {
- var activeAB = ABs[i];
- var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
- var sel = selec[j].duplicate(); // idoc.selection[0];
- fitPgItemRatio (sel, ABprops, boundsdiff);
- }
- } else {
- //alert(aspect);
- var ABs = idoc.artboards;
- for(i=0; i<ABs.length; i++) {
- var activeAB = ABs[i];
- var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
- var sel = selec[j].duplicate(); // idoc.selection[0];
- fitPgItem (sel, ABprops, boundsdiff);
- }
- }
- }
- else {
- //alert(allArtboards);
- if (aspect) {
- var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
- var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
- var sel = selec[j].duplicate(); // idoc.selection[0];
- fitPgItemRatio (sel, ABprops, boundsdiff);
- } else {
- //alert(aspect);
- var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
- var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
- var sel = selec[j].duplicate(); // idoc.selection[0];
- fitPgItem (sel, ABprops, boundsdiff);
- }
- }
- } // end if there's something selected
- //idoc.selection = null;
- //selec[0].selected = true;
- }
- else {
- alert("select something before running");
- }
- //--------------------------------------------------------------------------------------------------------
- // move and resize pgItem
- function fitPgItem(pgItem, containerProps, pgItemBoundsDiff) {
- var sel = pgItem;
- var ABprops = containerProps;
- var boundsdiff = pgItemBoundsDiff;
- sel.width = ABprops.width-boundsdiff.deltaX; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
- sel.height = ABprops.height-boundsdiff.deltaY;
- sel.top = ABprops.top; // Top is actually Visible top
- sel.left = ABprops.left; // dito for Left
- sel.selected = false;
- }
- //--------------------------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------------------------
- // move and resize pgItem
- function fitPgItemRatio(pgItem, containerProps, pgItemBoundsDiff) {
- var sel = pgItem;
- var ABprops = containerProps;
- var boundsdiff = pgItemBoundsDiff;
- var abBounds1 = activeAB.artboardRect;
- var ableft1 = abBounds1[0];
- var abright1 = abBounds1[2];
- var abwidth1 = abright1-ableft1;
- var abtop1 = abBounds1[1];
- var abbottom1 = abBounds1[3];
- var abheight1 = abtop1-abbottom1;
- if (sel.width > sel.height) { // image is landscape scale the image to max width
- alert("Landscape");
- var scale = ( (parseFloat(ABprops.width-boundsdiff.deltaX) / parseFloat(sel.width) * 100).toFixed(2)); // How much do I need to scale
- sel.resize(
- scale , // x
- scale , // y
- true, // changePositions
- true, // changeFillPatterns
- true, // changeFillGradients
- true, // changeStrokePattern
- scale , // changeLineWidths <---- NOTE THIS
- undefined); // scaleAbout
- // Check to see if it vits within the bounds and rescale down if needed.
- if (sel.height > ABprops.height-boundsdiff.deltaY) {
- var scale = ( (parseFloat(ABprops.height-boundsdiff.deltaY)/parseFloat(sel.width) * 100).toFixed(2) ); // How MUCH MORE do I need to scale
- sel.resize(
- scale , // x
- scale , // y
- true, // changePositions
- true, // changeFillPatterns
- true, // changeFillGradients
- true, // changeStrokePattern
- scale , // changeLineWidths <---- NOTE THIS
- undefined); // scaleAbout
- }
- sel.top = (parseInt(abheight1)/2 ) - (parseFloat(sel.height)/2) * -1;
- //alert (parseFloat(ABprops.height-boundsdiff.deltaY)/2 + " - " + parseFloat(sel.height)/2 + "=" +sel.top);
- sel.left = ABprops.left
- sel.selected = false;
- } else { // image is portrait
- //alert("Portrait");
- //alert(ABprops.height-boundsdiff.deltaY + "Bounds diff");
- var scale = ( (parseFloat(ABprops.height-boundsdiff.deltaY)/parseFloat(sel.height) * 100).toFixed(2) ); // How much do I need to scale
- //alert (ABprops.height-boundsdiff.deltaY + " / " + sel.height + "=" +scale);
- sel.resize(
- scale , // x
- scale , // y
- true, // changePositions
- true, // changeFillPatterns
- true, // changeFillGradients
- true, // changeStrokePattern
- scale , // changeLineWidths <---- NOTE THIS
- undefined); // scaleAbout
- // Check to see if it vits within the bounds and rescale down if needed.
- if (sel.width > ABprops.width-boundsdiff.deltaX) {
- var scale = ( (parseFloat(ABprops.width-boundsdiff.deltaX)/parseFloat(sel.width) * 100).toFixed(2) ); // How MUCH MORE do I need to scale
- sel.resize(
- scale , // x
- scale , // y
- true, // changePositions
- true, // changeFillPatterns
- true, // changeFillGradients
- true, // changeStrokePattern
- scale , // changeLineWidths <---- NOTE THIS
- undefined); // scaleAbout
- }
- sel.top = ( (parseInt(abheight1)/2 ) + ((parseInt(sel.height)/2)));
- //alert (parseInt(abheight1) + " - "+ parseInt(sel.height) + " - "+ sel.top);
- sel.left = (parseInt(abwidth1)/2 ) - ((parseInt(sel.width)/2));
- // alert ((ABprops.width) + " - " + parseInt(parseFloat(sel.width)/2) + "=" +sel.left + " Width = "+sel.width);
- sel.selected = false;
- }
- // sel.width = ABprops.width-boundsdiff.deltaX; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
- // sel.height = ABprops.height-boundsdiff.deltaY;
- //alert (ABprops.height-boundsdiff.deltaY);
- //sel.top = ABprops.top; // Top is actually Visible top
- //sel.left = ABprops.left; // dito for Left
- // sel.selected = false;
- }
- //--------------------------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------------------------
- //returns a pageItem's VisibleBounds vs GeometricBounds Difference
- // needed to substract from an item's width/height since they're based on Geometric width/height
- function getPgItemGBoundsvsVBoundsDifference (pgitem) {
- // get selection bounds
- var sel = pgitem;
- var selVB = sel.visibleBounds;
- var selVw = selVB[2]-selVB[0];
- var selVh = selVB[1]-selVB[3];
- var selGB = sel.geometricBounds;
- var selGw = selGB[2]-selGB[0];
- var selGh = selGB[1]-selGB[3];
- // get the difference between Visible & Geometric Bounds
- var deltaX = selVw-selGw;
- var deltaY = selVh-selGh;
- return deltaxy = {deltaX:deltaX, deltaY:deltaY};
- }
- //--------------------------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------------------------
- // returns an Artboard with top, left, width & height properties, including Margins
- function getABbounds (artboard, padding) {
- var activeAB = artboard;
- var margins = padding;
- var abBounds = activeAB.artboardRect; // get bounds [left, top, right, bottom]
- var ableft = abBounds[0]-margins;
- var abtop = abBounds[1]+margins;
- var abright = abBounds[2]+margins;
- var abbottom = abBounds[3]-margins;
- var abwidth = abright-ableft;
- var abheight = abtop-abbottom;
- return AB = {left:ableft, top:abtop, width:abwidth, height:abheight};
- }
- //--------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement