//Hello! This script tries to make your pages nice and neat, by fitting your selected objects to their nearest margins/baselines. I think this doesn't work right if you're using a fluid layout, but it DOES work with bleeds. But it doesn't work over a double page spread. So, swings and roundabouts. var selection = app.selection; var selectNo = 0; for (selectNo = 0; selectNo < selection.length; selectNo++){ var selectedObject = app.selection[selectNo]; try { var doc = app.activeDocument; var pageNo = selectedObject.parentPage.name; var page = selectedObject.parentPage; app.viewPreferences.rulerOrigin = RulerOrigin.SPINE_ORIGIN; app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.SPINE_ORIGIN; app.activeDocument.zeroPoint = [0,0]; var insideBounds = myGetBounds(doc, page); var margins = page.marginPreferences; var clmnCount = margins.columnCount; var clmnGutter = margins.columnGutter; var docPrefs = doc.documentPreferences; var startBounds = selectedObject.geometricBounds; var startBoundsHeight = (startBounds[2] - startBounds[0]); var startBoundsWidth = (startBounds[3] - startBounds[1]); var endBounds; var graphicsCount = 0; if ('graphics' in selectedObject && selectedObject.graphics.count() > 0){ var imageBool = true; graphicsCount = selectedObject.graphics.length; } else { var imageBool = false; } if (imageBool){ if (graphicsCount > 0){ var ib = selectedObject.graphics[0].geometricBounds; var difY = startBounds[0] - ib[0]; var difX = startBounds[1] - ib[1]; var gWidth = ib[3] - ib[1]; var gHeight = ib[2] - ib[0]; } } var midPointA = startBounds[3] - startBounds[1]; var midPointB = midPointA / 2; var midPoint = midPointB + startBounds[1]; var cCount = (clmnCount - 1); var cGutter = (clmnGutter * cCount); var cWidthA = (insideBounds[3] - insideBounds[1]); var cWidthB = (cWidthA - cGutter); var cWidth = (cWidthB / clmnCount); var gridPrefs = doc.gridPreferences; var baseDiv = gridPrefs.baselineDivision; var baseStart = gridPrefs.baselineStart; var baseGridRel = gridPrefs.baselineGridRelativeOption; var colBounds = [startBounds[0], insideBounds[1], startBounds[2], (insideBounds[1] + cWidth)]; //If page is on right hand side if(midPoint > 0){ var leftMargins = [insideBounds[1] - margins.left, insideBounds[1]]; } else { var leftMargins = [insideBounds[1] - margins.right - docPrefs.documentBleedOutsideOrRightOffset, insideBounds[1]]; } var rightMargins = [colBounds[3]]; for (i = 0; i < (clmnCount - 1); i++){ colBounds = [colBounds[0], (colBounds[1] + cWidth + clmnGutter), colBounds[2], (colBounds[3] + cWidth + clmnGutter)]; leftMargins.push(colBounds[1]); rightMargins.push(colBounds[3]); } if(midPoint > 0){ rightMargins.push(insideBounds[3] + margins.right + docPrefs.documentBleedOutsideOrRightOffset ); } else { rightMargins.push(insideBounds[3] + margins.left); } var leftWinnerValue = 1000; var rightWinnerValue = 1000; var leftWinnerID = 0; var rightWinnerID = 0; for (i = 0; i < leftMargins.length; i++){ if (leftMargins[i] < startBounds[1]){ var testNo = startBounds[1] - leftMargins[i]; } else { var testNo = leftMargins[i] - startBounds[1]; } if (testNo < leftWinnerValue){ leftWinnerID = i; leftWinnerValue = testNo; } } for (i = 0; i < rightMargins.length; i++){ if (rightMargins[i] < startBounds[3]){ var testNo = startBounds[3] - rightMargins[i]; } else { var testNo = rightMargins[i] - startBounds[3]; } if (testNo < rightWinnerValue){ rightWinnerID = i; rightWinnerValue = testNo; } } colBounds = [colBounds[0], leftMargins[leftWinnerID], colBounds[2], rightMargins[rightWinnerID]]; endBounds = colBounds; if (baseGridRel == BaselineGridRelativeOption.TOP_OF_PAGE_OF_BASELINE_GRID_RELATIVE_OPTION){ var gridCount = 0; } else { var gridCount = page.marginPreferences.top; } var topCheckA = (endBounds[0] - gridCount); var topCheckB = (topCheckA - baseStart); var topCheck = (topCheckB / baseDiv); if (!isInteger(topCheck)){ var floorCheck = Math.floor(topCheck); var ceilingCheck = Math.ceil(topCheck); var floorDif = topCheck - floorCheck; var ceilingDif = ceilingCheck - topCheck; if (floorDif < ceilingDif){ topCheck = Math.floor(topCheck); } else { topCheck = Math.ceil(topCheck); } } var newGridTop = (topCheck * baseDiv); endBounds[0] = newGridTop + gridCount + baseStart; if (endBounds[0] < 0){ endBounds[0] = 0 - docPrefs.documentBleedTopOffset; } var botCheckA = (endBounds[2] - gridCount); var botCheckB = (botCheckA - baseStart); var botCheck = (botCheckB / baseDiv); if (!isInteger(botCheck)){ var floorCheck = Math.floor(botCheck); var ceilingCheck = Math.ceil(botCheck); var floorDif = botCheck - floorCheck; var ceilingDif = ceilingCheck - botCheck; if (floorDif < ceilingDif){ botCheck = Math.floor(botCheck); } else { botCheck = Math.ceil(botCheck); } } var newGridBot = (botCheck * baseDiv); endBounds[2] = newGridBot + gridCount + baseStart; if (endBounds[2] > page.bounds[2]){ endBounds[2] = page.bounds[2] + docPrefs.documentBleedBottomOffset; } selectedObject.geometricBounds = endBounds; if (imageBool){ var endBoundsHeight = endBounds[2] - endBounds[0]; var endBoundsWidth = endBounds[3] - endBounds[1]; var incrRatio = endBoundsWidth / startBoundsWidth; var newDifY = difY * incrRatio; var newDifX = difX * incrRatio; var newWidth = gWidth * incrRatio; var newHeight = gHeight * incrRatio; var newX = selectedObject.geometricBounds[1] - newDifX var newY = selectedObject.geometricBounds[0] - newDifY; if (graphicsCount > 0){ selectedObject.graphics[0].geometricBounds = [newY, newX, newY + newHeight, newX + newWidth]; } // var incrRatioY = startBoundsHeight / endBoundsHeight; } } catch (e) { alert ("Something went wrong. Are you in text mode? You might be in text mode."); } //selectedObject.fit(FitOptions.FILL_PROPORTIONALLY); } function isInteger(x) { return x % 1 === 0; } function myGetBounds(myDocument, myPage){ var startBounds = selectedObject.geometricBounds; var midPointA = startBounds[3] - startBounds[1]; var midPointB = midPointA / 2; var midPoint = midPointB + startBounds[1]; var myPageWidth = myDocument.documentPreferences.pageWidth; var myPageHeight = myDocument.documentPreferences.pageHeight if(midPoint > 0){ var otherWidth = (0 + myDocument.documentPreferences.pageWidth); var myX1 = myPage.marginPreferences.left; var myX2 = otherWidth - myPage.marginPreferences.right; // var myX2 = myPageWidth - myX2; } else{ var otherWidth = (0 - myDocument.documentPreferences.pageWidth); var myX1 = otherWidth + myPage.marginPreferences.right; var myX2 = 0 - myPage.marginPreferences.left; //var myX1 = myPage.marginPreferences.left; //var myX2 = myPage.marginPreferences.right; } var myY1 = myPage.marginPreferences.top; //var myX2 = myPageWidth - myX2; var myY2 = myPageHeight - myPage.marginPreferences.bottom; return [myY1, myX1, myY2, myX2]; }