forked from GitBruno/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlign_2_Grid.jsx
More file actions
executable file
·78 lines (67 loc) · 2.16 KB
/
Align_2_Grid.jsx
File metadata and controls
executable file
·78 lines (67 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Align_2_Grid.jsx
Version 1.0
Bruno Herfst 2011
An InDesign javascript to align Rectangles and TextFrames to the Document Grid
*/
#target InDesign;
try {
//global vars
var myDoc = app.activeDocument;
//save measurements units
myOldXUnits = myDoc.viewPreferences.horizontalMeasurementUnits,
OldYUnits = myDoc.viewPreferences.verticalMeasurementUnits;
setRulerUnits(myDoc, MeasurementUnits.points, MeasurementUnits.points);
var G = getGridDivision(myDoc);
if(app.selection.length != 0){
//Get the first item in the selection.
for(var i=0;i<app.selection.length;i++){
var mySelection = app.selection[i];
switch(mySelection.constructor.name){
case "Rectangle": case "TextFrame":
var bounds = mySelection.geometricBounds; //array [y1, x1, y2, x2], [top, left, bottom, right]
mySelection.geometricBounds = [roundTo(bounds[0],G.h), roundTo(bounds[1],G.w), roundTo(bounds[2],G.h), roundTo(bounds[3],G.w)];
break;
default:
var ws = mySelection.constructor.name;
alert("Didn’t do "+ws+" in selection");
}
}
}else{
alert("Please select something first.");
}
} catch(err) {
var txt=err.description;
alert(txt);
exit();
}
function getGridDivision(myDoc){
var G = new Object();
G.w = myDoc.gridPreferences.horizontalGridlineDivision / myDoc.gridPreferences.horizontalGridSubdivision;
G.h = myDoc.gridPreferences.verticalGridlineDivision / myDoc.gridPreferences.verticalGridSubdivision;
return G;
}
function setRulerUnits(myDoc,XUnits,YUnits){
//myUnits choices are:
//MeasurementUnits.picas
//MeasurementUnits.points
//MeasurementUnits.inches
//MeasurementUnits.inchesDecimal
//MeasurementUnits.millimeters
//MeasurementUnits.centimeters
//MeasurementUnits.ciceros
//MeasurementUnits.gates
myDoc.viewPreferences.horizontalMeasurementUnits = XUnits;
myDoc.viewPreferences.verticalMeasurementUnits = YUnits;
}
function roundTo(num,grid){
return doRound(Math.round(num/grid)*grid,3);
}
function doRound(myNum, roundDec) {
var roundMulit = Math.pow(10,roundDec);
return Math.round(myNum*roundMulit)/roundMulit;
}
function exit(){
//reset rulers
setRulerUnits(myDoc, myOldXUnits, OldYUnits);
}