-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
155 lines (133 loc) · 5.02 KB
/
functions.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// This script contains utility functions used by other scripts, it is not meant to be executed on its own
// Function to create a text frame :
// takes a string, creates a frame of text with this string
function createTextFrame(string, paragraphStyle, layer, orientation, y1, x1) {
//$.writeln(myDocument);
var myFrame = app.activeDocument.layoutWindows[0].activePage.textFrames.add(layer, undefined, undefined, {
geometricBounds: [y1, x1, y1 + 50, x1 + 50],
contents: string,
//rotationAngle: -90,
});
// apply paragraph style
myFrame.texts[0].appliedParagraphStyle = paragraphStyle;
// change anchor point
myFrame.resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.INNER_COORDINATES);
// fit frame to content
//myFrame.fit(FitOptions.FRAME_TO_CONTENT);
// change orientation
if (orientation === 1) {
myFrame.textFramePreferences.verticalJustification = VerticalJustification.BOTTOM_ALIGN;
myFrame.rotationAngle = -90;
}
// Change autosize preferences
//myFrame.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_LEFT_POINT;
//myFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH_PROPORTIONALLY;
// change anchor point
//myFrame.resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.INNER_COORDINATES);
//myFrame.anchoredObjectSettings.anchorPoint = AnchorPoint.TOP_LEFT_ANCHOR;
}
// function to create a flat map array
function createFlatMap(width, height, columnCount, rowCount) {
var map = [];
var counter = 0;
for (var y = 0; y < columnCount + 1; y++) {
//map[x] = []; // set up inner array
for (var x = 0; x < rowCount + 1; x++) {
//addCell(map, x, y);
map[counter] = [
((width / (columnCount)) * x) + myMargins.left,
((height / (rowCount)) * y) + myMargins.top
];
counter++;
}
}
return map;
}
// Function to create a layer
function createNumberedLayer(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
var theLayer = app.activeDocument.layers.add({name: layerBaseName + " " + layerCounter});
return theLayer;
}
// create a circle
function createCircle(layer, y1, x1, radius) {
myDocument.ovals.add(myLayer, undefined, undefined, {
geometricBounds: [y1 - (radius / 2), x1 - (radius / 2), y1 + radius, x1 + radius],
fillColor: "Black",
strokeWeight: 0
});
}
// get random int
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
// Function to draw a guide grid
function drawGuideGrid(layer, width, height, 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) {
drawGuide(i, 0);
}
}
// draw vertical guides
for (var i = 0; i < myPageWidth; i++) {
if (i % Math.round(myPageWidth/columns) === 0) {
drawGuide(i, 1);
}
}*/
// vertical guides
for (var y = 0; y < columns + 1; y++) {
drawGuide(layer, (((width / (columns)) * y) + myMargins.left), 1);
}
// horizontal guides
for (var x = 0; x < rows + 1; x++) {
drawGuide(layer, (((height / (rows)) * x) + myMargins.top), 0);
}
// lock guide layer
//layer.locked = true;
}
// Function to draw a single guide
function drawGuide(layer, 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(layer, undefined, undefined)) {
orientation = HorizontalOrVertical.horizontal;
location = myGuideLocation;
fitToPage = true;
}
}
// if orientation is 1 -> vertical
else {
with (guides.add(layer, undefined, undefined)) {
orientation = HorizontalOrVertical.vertical;
location = myGuideLocation;
fitToPage = true;
}
}
}
}