-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddPadding.jsx
382 lines (286 loc) · 12.2 KB
/
AddPadding.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
// 📄 AddPadding.jsx
// Adobe Illustrator Script
// Add padding around your content in an artboard.
//
// ✨ Usage:
// 1. Open your illustrator document.
// [Optional] Rearrange your artboards to avoid overlapping.
// Select the "Artboard Tool" or press [Shift] + [O].
// Then go to, Properties → Quick Actions → Rearrange All.
// 2. Run the script, File → Scripts → AddPadding.jsx.
// 3. [Optional] By default, all the artboards will be affected.
// Alternatively, you can affect desired artboards by choosing
// the "Custom" option. Then input the custom number of artboards
// separated by a comma like '1, 3, 5' or a range of artboards
// like '1, 3, 5-8'.
// 4. [Optional] If you desire to add different paddings for odd
// and even numbered artboards, then select the "Alternate"
// option. Then input the "Left" and "Right" padding for the
// "Odd" and "Even" numbered artboard respectively.
// 5. Input the padding value in the "Padding" text-field.
// 6. [Optional] Equal paddings will be added in all the directions.
// Instead if you want to add different paddings for different
// directions, then check the "Different paddings" box, and input
// the padding values for top, left, right, and bottom.
// 7. Finally to render the paddings, press the "Add Padding" button.
//
// Copyright © 2022 Rahul Mula
// https://github.com/mulaRahul
// This script is distributed under the MIT License.
// See the LICENSE file for details.
//@target illustrator
app.preferences.setBooleanPreference("ShowExternalJSXWarning", false);
// config data
var configData = {
medic: 15,
spacing: 10,
fillAlignment: ["fill", "center"],
leftAlignment: ["left", "center"],
};
// * UI * //
// root window
var dlg = new Window("dialog", "Add Padding");
dlg.alignChildren = ["fill", "fill"];
// artboard options layout
var artboardOptionsContainer = dlg.add("panel", undefined, "Artboards");
artboardOptionsContainer.orientation = "row";
artboardOptionsContainer.paddings = configData.medic;
artboardOptionsContainer.alignChildren = configData.fillAlignment;
var allArtboardOption = artboardOptionsContainer.add("radiobutton", undefined, "All");
var customArtboardOption = artboardOptionsContainer.add("radiobutton", undefined, "Custom");
var customArtboardInput = artboardOptionsContainer.add("edittext", undefined, "1, 3, 5-8");
customArtboardInput.minimumSize = [100, 0];
// disable/enable [customArtboardInput] text-field
allArtboardOption.onClick = function () {
customArtboardInput.enabled = false;
};
customArtboardOption.onClick = function () {
customArtboardInput.enabled = true;
};
// padding input
var paddingContainer = dlg.add("panel", undefined, "Padding");
paddingContainer.paddings = configData.medic;
paddingContainer.alignment = configData.fillAlignment;
paddingContainer.alignChildren = configData.leftAlignment;
var paddingRadiobuttonGroup = paddingContainer.add("group", undefined);
paddingRadiobuttonGroup.orientation = "column";
paddingRadiobuttonGroup.alignChildren = configData.leftAlignment;
var allPaddingWrapper = paddingRadiobuttonGroup.add("group", undefined);
var allPaddingBtn = allPaddingWrapper.add("radiobutton", undefined, "All");
var paddingAll = allPaddingWrapper.add("edittext", undefined);
paddingAll.minimumSize = [60, 0];
var unitOptions = allPaddingWrapper.add("dropdownlist", undefined, ["pixels", "inches"]);
var directionalPadding = paddingRadiobuttonGroup.add("radiobutton", undefined, "Directional");
// toggle radiobutton states
allPaddingBtn.onClick = function() {
// radiobuttons
this.value = true;
directionalPadding.value = false;
// edittexts
topPadding.enabled = false;
leftPadding.enabled = false;
rightPadding.enabled = false;
bottomPadding.enabled = false;
paddingAll.enabled = true;
}
directionalPadding.onClick = function() {
// radiobuttons
this.value = true;
allPaddingBtn.value = false;
// edittexts
topPadding.enabled = true;
leftPadding.enabled = true;
rightPadding.enabled = true;
bottomPadding.enabled = true;
paddingAll.enabled = false;
}
// directional padding
var directionalPaddingPanel = paddingContainer.add("group", undefined);
directionalPaddingPanel.orientation = "row";
directionalPaddingPanel.alignment = configData.leftAlignment;
// var directionalPadding = dlg.add("checkbox", undefined, "Different paddings");
// directionalPadding.alignment = configData.leftAlignment;;
var topWrapper = directionalPaddingPanel.add("group", undefined);
topWrapper.orientation = "column";
topWrapper.alignChildren = configData.leftAlignment;;
topWrapper.add("statictext", undefined, "Top ");
var topPadding = topWrapper.add("edittext", undefined, "0");
topPadding.minimumSize = [50, 0];
var rightWrapper = directionalPaddingPanel.add("group", undefined);
rightWrapper.orientation = "column";
rightWrapper.alignChildren = configData.leftAlignment;;
rightWrapper.add("statictext", undefined, "Right ");
var rightPadding = rightWrapper.add("edittext", undefined, "0");
rightPadding.minimumSize = [50, 0];
var leftWrapper = directionalPaddingPanel.add("group", undefined);
leftWrapper.orientation = "column";
leftWrapper.alignChildren = configData.leftAlignment;;
leftWrapper.add("statictext", undefined, "Left ");
var leftPadding = leftWrapper.add("edittext", undefined, "0");
leftPadding.minimumSize = [50, 0];
var bottomWrapper = directionalPaddingPanel.add("group", undefined);
bottomWrapper.orientation = "column";
bottomWrapper.alignChildren = configData.leftAlignment;;
bottomWrapper.add("statictext", undefined, "Bottom ");
var bottomPadding = bottomWrapper.add("edittext", undefined, "0");
bottomPadding.minimumSize = [50, 0];
// method options layout
var methodOptionsContainer = dlg.add("panel", undefined, "Method");
methodOptionsContainer.paddings = configData.medic;
methodOptionsContainer.alignChildren = configData.leftAlignment;
var methodOptionsWrapper = methodOptionsContainer.add("group", undefined);
methodOptionsWrapper.orientation = "row";
var similarOption = methodOptionsWrapper.add("radiobutton", undefined, "Similar");
similarOption.helpTip = "Same padding on all artboards";
var alternateOption = methodOptionsWrapper.add("radiobutton", undefined, "Alternate");
alternateOption.helpTip = "Alternate between odd and even numbered artboards";
// alternate options input layout
var alternateOddContainer = methodOptionsContainer.add("panel", undefined, "Odd");
alternateOddContainer.orientation = "row";
alternateOddContainer.paddings = configData.medic;
alternateOddContainer.alignment = configData.fillAlignment;
alternateOddContainer.alignChildren = configData.fillAlignment;
var alternateOddLeftWrapper = alternateOddContainer.add("group", undefined);
alternateOddLeftWrapper.add("statictext", undefined, "Left");
var oddLeftPadding = alternateOddLeftWrapper.add("edittext", undefined);
oddLeftPadding.minimumSize = [40, 0];
var alternateOddRightWrapper = alternateOddContainer.add("group", undefined);
alternateOddRightWrapper.add("statictext", undefined, "Right");
var oddRightPadding = alternateOddRightWrapper.add("edittext", undefined);
oddRightPadding.minimumSize = [40, 0];
var alternateEvenContainer = methodOptionsContainer.add("panel", undefined, "Even");
alternateEvenContainer.orientation = "row";
alternateEvenContainer.paddings = configData.medic;
alternateEvenContainer.alignment = configData.fillAlignment;
alternateEvenContainer.alignChildren = configData.fillAlignment;
var alternateEvenLeftWrapper = alternateEvenContainer.add("group", undefined);
alternateEvenLeftWrapper.add("statictext", undefined, "Left");
var evenLeftPadding = alternateEvenLeftWrapper.add("edittext", undefined);
evenLeftPadding.minimumSize = [40, 0];
var alternateEvenRightWrapper = alternateEvenContainer.add("group", undefined);
alternateEvenRightWrapper.add("statictext", undefined, "Right");
var evenRightPadding = alternateEvenRightWrapper.add("edittext", undefined);
evenRightPadding.minimumSize = [40, 0];
// [alternateOptionsContainer] visibility toggling
similarOption.onClick = function() {
alternateEvenContainer.enabled = false;
alternateOddContainer.enabled = false;
};
alternateOption.onClick = function() {
alternateEvenContainer.enabled = true;
alternateOddContainer.enabled = true;
};
// * Main Scripting * //
//utility functions
function contains(array, element) {
return array.toString().indexOf( element.toString() ) !== -1;
}
function parseInputRange(query) {
// takes string like "0, 2, 4 - 6" as query
// and returns Array like [ 0, 2, 4, 5, 6 ]
var lst = query.split(',');
var newLst = [];
for(var i = 0; i < lst.length; i++) {
// remove spaces
var val = lst[i].replace(" ", "");
// if it's nested range parse it
if(val.indexOf("-") !== -1) {
var _lst = val.split('-');
var start = parseInt(_lst[0]);
var end = parseInt(_lst[1]);
for(var i = start; i <= end; i++) {
newLst.push(i - 1);
}
} else {
newLst.push(parseInt(val) - 1);
}
}
return newLst;
}
function artboardAddPadding(allArtboards, idx, layer) {
// adds padding to the artboard with given [idx]
// padding calc
var _paddingLeft = parseFloat(paddingAll.text);
var _paddingTop = _paddingLeft;
var _paddingRight = _paddingLeft;
var _paddingBottom = _paddingLeft;
if (directionalPadding.value) {
// different paddings for different direction
_paddingLeft = parseFloat(leftPadding.text);
_paddingTop = parseFloat(topPadding.text);
_paddingRight = parseFloat(rightPadding.text);
_paddingBottom = parseFloat(bottomPadding.text);
}
if (alternateOption.value) {
if ((idx + 1) % 2 == 0) {
// even index number means odd artboard number
_paddingLeft = parseFloat(evenLeftPadding.text);
_paddingRight = parseFloat(evenRightPadding.text);
}
else {
// odd index number means even artboard number
_paddingLeft = parseFloat(oddLeftPadding.text);
_paddingRight = parseFloat(oddRightPadding.text);
}
}
if (unitOptions.selection.text == "inches") {
// unit conversion from inches to pixels
// 1 in = 72 px here
_paddingLeft *= 72;
_paddingRight *= 72;
_paddingTop *= 72;
_paddingBottom *= 72;
}
// points of the artboard
var _left = allArtboards[idx].artboardRect[0];
var _top = allArtboards[idx].artboardRect[1];
var _right = allArtboards[idx].artboardRect[2];
var _bottom = allArtboards[idx].artboardRect[3];
allArtboards[idx].artboardRect = [
_left - _paddingLeft,
_top + _paddingTop,
_right + _paddingRight,
_bottom - _paddingBottom
];
}
function renderPadding() {
// getting active document
try {
var curDoc = app.activeDocument;
} catch (_) {
alert ("No active illustrator document found. Please open one!", "Error");
return;
}
// input params check
var customArtboardLst;
if (customArtboardOption.value) {
var customArtboardLst = parseInputRange(customArtboardInput.text);
}
var allArtboards = curDoc.artboards;
for(var idx = 0; idx < allArtboards.length; idx++) {
// all artboards
if (allArtboardOption.value) {
artboardAddPadding(allArtboards, idx);
}
// custom artboards ... roundabout way as Array.indexOf() doesn't work
else if (contains(customArtboardLst, idx)) {
artboardAddPadding(allArtboards, idx);
}
}
alert("Padding Added! Please close the dialog to view the result.", "Success");
}
// add padding button
var renderBtn = dlg.add("button", undefined, "Add Padding");
renderBtn.minimumSize = [0, 40];
renderBtn.onClick = renderPadding;
renderBtn.alignment = configData.fillAlignment;
renderBtn.helpTip = "to the active illustrator document";
// default options selections
allArtboardOption.value = true;
customArtboardInput.enabled = false;
similarOption.value = true;
similarOption.onClick();
unitOptions.selection = "pixels";
directionalPadding.value = false;
allPaddingBtn.onClick();
dlg.show();