-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateVerticalPoleBanner.jsx
398 lines (335 loc) · 12.8 KB
/
createVerticalPoleBanner.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "jsx_modules.jsx"
// Copies the selection into the verticalPoleBanner.ait template and sets it up to be print-ready.
// This script asks for a 26 inch wide item to fit into the template which was made for that specific dimension.
// Author: Ariff Jeff
// Contact: [email protected]
// TODO:
// VARIABLE 26 INCH SIZE AND DOCUMENTATION ON HOW TO CHANGE THE TEMPLATE
BridgeTalk.bringToFront("illustrator") // comment out if any dialog inputs are active by default
main()
function main() {
if(documents.length < 1) {
alert("Open a document and try again.");
return
}
sourceDoc = activeDocument
if(selection.length < 1) {
alert("Select an object to layout into a print file.")
return
} else if(selection.length > 1) {
alert("Too many objects selected! Select only one object to layout into a print file.\n\nDid you mean to group the selection first?")
return
}
xmlConfig = getXMLConfig()
// sourceDoc not saved
save = true
if(sourceDoc.path.name.length == 0) {
if(!dialog_saveTheSourceFile()) {
return
}
save = false
}
// validate template file path, open new template
sourceDocIsTemplate = false
var fileFound = false
// identify UID, active doc is a template
var UID0 = "238957340_VERTICAL-POLE-BANNER_69721-086134-823509285108109"
for(textFrame = sourceDoc.textFrames.length - 1; textFrame >= 0; textFrame--) {
if(sourceDoc.textFrames[textFrame].contents == UID0) {
sourceDocIsTemplate = true
fileFound = true
templateDoc = sourceDoc
break
}
}
// open new template
while(!fileFound) {
var filePath_verticalPoleBanner = xmlConfig.xml.filePaths.ait.verticalPoleBanner
if(validateFilePath(filePath_verticalPoleBanner)) {
copy(sourceDoc.selection)
templateDoc = open(new File(filePath_verticalPoleBanner))
fileFound = true
paste()
} else { // file not found
var inputFilePath = dialog_missingTemplateFile("Vertical Pole Banner", xmlConfig.xml.filePaths.ait.verticalPoleBanner.toString())
if(inputFilePath === false) return false
if(inputFilePath.length > 0 && validateFilePath(inputFilePath)) { // check user input for new valid proof doc path and try to continue
if(inputFilePath.slice(inputFilePath.length-4) != ".ait") { // file exits but not .ait
if(dialog_confirmProofTemplate(inputFilePath)) { // continue with non .ait path
xmlConfig.xml.filePaths.ait.verticalPoleBanner = inputFilePath
writeXML(xmlConfig)
}
} else {
xmlConfig.xml.filePaths.ait.verticalPoleBanner = inputFilePath
writeXML(xmlConfig)
}
}
}
}
var docUnits = getDocUnit()
if(docUnits === false) return
artObject = selection[0]
var widthGoal = 26 * 72 // inches
// ensure correct art object scale
if(prec(artObject.width / docUnits.multiplier, 3) != prec(26 * 72 / docUnits.multiplier, 3)) {
scaleFitMultiplier = widthGoal / artObject.width
var result = scaleToStandardWidth(docUnits)
if(result == 2) {
artObject.resize(scaleFitMultiplier * 100, scaleFitMultiplier * 100, true, true, true, true, scaleFitMultiplier * 100, undefined)
} else if (result == false) {
return
}
}
// position
var pageItem
pageItem = getPageItemByName("center", "Markers")
if(pageItem != false) {
artObject.position = pageItem.position
} else {
return
}
var artLayer = getLayerByName("Art")
artObject.position = [artObject.position[0] - artObject.width / 2, artObject.position[1] + artObject.height / 2]
var artObject_marginTop = artObject.duplicate()
var artObject_marginBottom = artObject.duplicate()
artObject_marginTop.rotate(180)
artObject_marginBottom.rotate(180)
pageItem = getPageItemByName("margin_top_fold", "Markers")
if(pageItem != false) {
artObject_marginTop.position = arrayOffset(pageItem.position, [0, artObject_marginTop.height], "add")
} else {
return
}
pageItem = getPageItemByName("margin_bottom_fold", "Markers")
if(pageItem != false) {
artObject_marginBottom.position = pageItem.position
} else {
return
}
// clipMask
pageItem = getPageItemByName("clipMask_top", "clipMasks").duplicate(artLayer)
if(pageItem != false) {
var clipMask_top = pageItem
} else {
return
}
pageItem = getPageItemByName("clipMask_bottom", "clipMasks").duplicate(artLayer)
if(pageItem != false) {
var clipMask_bottom = pageItem
} else {
return
}
selection = [artObject_marginTop, clipMask_top]
redraw()
executeMenuCommand('makeMask')
selection = [artObject_marginBottom, clipMask_bottom]
redraw()
executeMenuCommand('makeMask')
if(save) saveForPrint()
}
function saveForPrint() {
// prevent loss of unsaved .ai changes on template save
if(sourceDocIsTemplate) {
sourceDoc.save()
}
var sourceDoc_fileName = sourceDoc.name.split('.').slice(0, -1).join('.')
if(sourceDocIsTemplate) {
filePath = new File(sourceDoc.path + "/" + sourceDoc.name)
} else {
var i = 0
while(true) {
if(!(validateFilePath(sourceDoc.path + "/" + sourceDoc_fileName + "_PRINT_" + i + ".eps") || validateFilePath(sourceDoc.path + "/" + sourceDoc_fileName + "_PRINT_" + i + ".pdf"))) { // check for iteration
filePath = new File(sourceDoc.path + "/" + sourceDoc_fileName + "_PRINT_" + i)
break
} else {
i += 1
}
}
}
var options = new EPSSaveOptions();
options.cmykPostScript = true
options.embedAllFonts = true
templateDoc.saveAs(filePath, options) // eps
templateDoc.close(SaveOptions.DONOTSAVECHANGES)
}
function getLayerByName(layerName, doc) {
var doc = doc !== undefined ? doc : activeDocument
for(var layer = 0; layer < activeDocument.layers.length; layer++) {
if(doc.layers[layer].name == layerName) {
return doc.layers[layer]
}
}
alert("Could not find required layer: \"" + layerName + "\"\n\nDid you rename/delete the layer?")
return false
}
function getPageItemByName(pageItemName, layerName, doc) {
var doc = doc !== undefined ? doc : activeDocument
if(layerName == undefined) {
for(var pageItem = 0; pageItem < doc.pageItems.length; pageItem++) {
if(doc.pageItems[pageItem].name == pageItemName) {
return doc.pageItems[pageItem]
}
}
alert("Could not find pageItem: \"" + pageItemName + "\" in document: \"" + doc.name + "\"")
return false
} else {
var layer = getLayerByName(layerName, doc)
for(var pageItem = 0; pageItem < layer.pageItems.length; pageItem++) {
if(layer.pageItems[pageItem].name == pageItemName) {
return layer.pageItems[pageItem]
}
}
alert("Could not find pageItem: \"" + pageItemName + "\" in layer: \"" + layerName + "\" in document: \"" + doc.name + "\"")
return false
}
}
function arrayOffset(arr0, arr1, op) {
if(arr0.length == arr1.length) {
var arrTemp = []
for(var i = 0; i < arr0.length; i++) {
switch(op) {
case "add":
arrTemp[i] = arr0[i] + arr1[i];
break;
case "sub":
arrTemp[i] = arr0[i] - arr1[i];
break;
case "mult":
arrTemp[i] = arr0[i] * arr1[i];
break;
case "div":
arrTemp[i] = arr0[i] / arr1[i];
}
}
return arrTemp
} else {
throw EvalError("Array lengths differ")
}
}
function scaleToStandardWidth(docUnits) {
// https://scriptui.joonas.me
// WARNING_ARTOBJWIDTH
// ===================
var warning_artObjWidth = new Window("dialog");
warning_artObjWidth.text = "Vertical Pole Banner - Warning: Art object width";
warning_artObjWidth.orientation = "column";
warning_artObjWidth.alignChildren = ["center","top"];
warning_artObjWidth.spacing = 10;
warning_artObjWidth.margins = 16;
// TEXTINFO
// ========
var textInfo = warning_artObjWidth.add("group", undefined, {name: "textInfo"});
textInfo.orientation = "column";
textInfo.alignChildren = ["left","center"];
textInfo.spacing = 10;
textInfo.margins = 0;
var warning = textInfo.add("statictext", undefined, undefined, {name: "warning"});
warning.text = "The width of the selected art object isn't the standard 26 inches for this print file template.";
var question = textInfo.add("statictext", undefined, undefined, {name: "question"});
question.text = "Do you want the script to uniformly scale the art object to a width of 26 inches? ";
// DIMS
// ====
var dims = warning_artObjWidth.add("group", undefined, {name: "dims"});
dims.orientation = "row";
dims.alignChildren = ["left","center"];
dims.spacing = 10;
dims.margins = 0;
// CURRENT
// =======
var current = dims.add("panel", undefined, undefined, {name: "current"});
current.text = "Current dimensions";
current.orientation = "column";
current.alignChildren = ["left","top"];
current.spacing = 10;
current.margins = 10;
var artObjWidth1 = current.add("statictext", undefined, undefined, {name: "artObjWidth1"});
artObjWidth1.text = "Width: " + prec(artObject.width / docUnits.multiplier, 4) + " " + docUnits.unit_short;
var artObjHeight1 = current.add("statictext", undefined, undefined, {name: "artObjHeight1"});
artObjHeight1.text = "Height: " + prec(artObject.height / docUnits.multiplier, 4) + " " + docUnits.unit_short;
// PROJECTED
// =========
var projected = dims.add("panel", undefined, undefined, {name: "projected"});
projected.text = "Scaled Dimensions";
projected.orientation = "column";
projected.alignChildren = ["left","top"];
projected.spacing = 10;
projected.margins = 10;
var artObjWidth2 = projected.add("statictext", undefined, undefined, {name: "artObjWidth2"});
artObjWidth2.text = "Width: " + prec(artObject.width * scaleFitMultiplier / docUnits.multiplier, 4) + " " + docUnits.unit_short;
var artObjHeight2 = projected.add("statictext", undefined, undefined, {name: "artObjHeight2"});
artObjHeight2.text = "Height: " + prec(artObject.height * scaleFitMultiplier / docUnits.multiplier, 4) + " " + docUnits.unit_short;
// SCALECHOICE
// =========
var scaleChoice = dims.add("group", undefined, undefined, {name: "scaleChoice"});
scaleChoice.orientation = "column";
scaleChoice.alignChildren = ["left","top"];
var radiobutton_scale = scaleChoice.add("radiobutton", undefined, undefined, {name: "radiobutton_scale"});
radiobutton_scale.text = "Scale";
radiobutton_scale.value = true;
var radiobutton_noScale = scaleChoice.add("radiobutton", undefined, undefined, {name: "radiobutton_noScale"});
radiobutton_noScale.text = "Don't scale";
// USERBTNS
// ========
var userBtns = warning_artObjWidth.add("group", undefined, {name: "userBtns"});
userBtns.orientation = "row";
userBtns.alignChildren = ["left","center"];
userBtns.spacing = 10;
userBtns.margins = 0;
var cancel = userBtns.add("button", undefined, undefined, {name: "cancel"});
cancel.text = "Stop script";
var ok = userBtns.add("button", undefined, undefined, {name: "ok"});
ok.text = "Continue";
var choice = warning_artObjWidth.show();
switch (choice) {
case 1: // continue
if(radiobutton_scale.value) {
return 2
} else {
return
}
case 2: // cancel
return false
default:
break;
}
}
function dialog_saveTheSourceFile() {
// https://scriptui.joonas.me
// SAVETHESOURCEFILE
// =================
var saveTheSourceFile = new Window("dialog", undefined, undefined, {closeButton: false});
saveTheSourceFile.text = "Save the Source File?";
saveTheSourceFile.orientation = "column";
saveTheSourceFile.alignChildren = ["center","top"];
saveTheSourceFile.spacing = 11;
saveTheSourceFile.margins = 16;
var statictext1 = saveTheSourceFile.add("group");
statictext1.preferredSize.width = 335;
statictext1.orientation = "column";
statictext1.alignChildren = ["left","center"];
statictext1.spacing = 0;
statictext1.alignment = ["left","top"];
statictext1.add("statictext", undefined, "You should probably save the source file first in case", {name: "statictext1"});
statictext1.add("statictext", undefined, "something causes Illustrator to crash.", {name: "statictext1"});
statictext1.preferredSize.width = 335;
// GROUP1
// ======
var group1 = saveTheSourceFile.add("group", undefined, {name: "group1"});
group1.orientation = "row";
group1.alignChildren = ["left","center"];
group1.spacing = 10;
group1.margins = 0;
var cancel = group1.add("button", undefined, undefined, {name: "cancel"});
cancel.text = "Stop Script";
var ok = group1.add("button", undefined, undefined, {name: "ok"});
ok.text = "Continue without saving";
var choice = saveTheSourceFile.show();
switch (choice) {
case 1: // continue
return true
case 2: // cancel
return false
default:
break;
}
}