-
Notifications
You must be signed in to change notification settings - Fork 0
/
__grid_layout.jsx
115 lines (100 loc) · 3.36 KB
/
__grid_layout.jsx
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
main();
// general variable
var myLayer;
function main() {
// display dialog box
myDisplayDialog();
}
/* ---- FUNCTIONS ---- */
// Function to display dialog box
function myDisplayDialog() {
var myLabelWidth = 90;
var myDialog = app.dialogs.add({name: "Grid Layout"});
with (myDialog.dialogColumns.add()) {
with (dialogRows.add()) {
with (dialogColumns.add()) {
staticTexts.add({staticLabel: "Rows:", minWidth: myLabelWidth});
staticTexts.add({staticLabel: "Columns:", minWidth: myLabelWidth});
}
with (dialogColumns.add()) {
var myNumberOfRowsField = integerEditboxes.add({editValue: 2});
var myNumberOfColumnsField = integerEditboxes.add({editValue: 2});
}
}
}
var myResult = myDialog.show();
if (myResult == true) {
var myNumberOfRows = myNumberOfRowsField.editValue;
var myNumberOfColumns = myNumberOfColumnsField.editValue;
myDialog.destroy();
// create guide layer
myCreateGuideLayer("Guides_");
// draw grid
myDrawGrid(myNumberOfRows, myNumberOfColumns);
} else {
myDialog.destroy();
}
}
// Function to create guide layer
function myCreateGuideLayer(layerBaseName) {
var layerCounter = 0;
// Create a layer to hold the guides marks (if it does not already exist).
// Try to select layer
myLayer = app.activeDocument.layers.item(layerBaseName + layerCounter);
// while layer exists
while (typeof myLayer != "undefined") {
// Try to select layer
myLayer = app.activeDocument.layers.item(layerBaseName + layerCounter);
try {
var myLayerName = myLayer.name;
// increment layer counter
layerCounter++;
}
// if the layer does not exists
catch (myError) {
break;
}
}
// create layer
myLayer = app.activeDocument.layers.add({name: layerBaseName + layerCounter});
}
// Function to draw a guide grid
function myDrawGrid(rows, columns) {
var myPageHeight = app.activeDocument.documentPreferences.pageHeight;
var myPageWidth = app.activeDocument.documentPreferences.pageWidth;
// draw horizontal guides
for (var i = 0; i < myPageHeight; i++) {
if (i % Math.round(myPageHeight/rows) === 0) {
myDrawGuide(i, 0);
}
}
// draw vertical guides
for (var i = 0; i < myPageWidth; i++) {
if (i % Math.round(myPageWidth/columns) === 0) {
myDrawGuide(i, 1);
}
}
// lock guide layer
myLayer.locked = true;
}
// Function to draw a single guide
function myDrawGuide(myGuideLocation, myGuideOrientation) {
// select guide layer
//var myLayer = app.activeDocument.layers.item("Guides");
with (app.activeWindow.activeSpread) {
// if orientation is 0 -> horizontal
if (myGuideOrientation == 0) {
with (guides.add(myLayer, undefined, undefined)) {
orientation = HorizontalOrVertical.horizontal;
location = myGuideLocation;
}
}
// if orientation is 1 -> vertical
else {
with (guides.add(myLayer, undefined, undefined)) {
orientation = HorizontalOrVertical.vertical;
location = myGuideLocation;
}
}
}
}