-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resize page items by style 1.0.2.js
397 lines (302 loc) · 8.87 KB
/
Resize page items by style 1.0.2.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
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
/**
* Resize page items by style
* @version 1.0.2
* @author McShaman
* @todo Implement a solution for absolutely adjusting objects in only one direction
*/
#targetengine "session";
/**
* Get first instance of ancestor object of another object
*/
var getAncestor = function(p_object, p_ancestor) {
var parent = p_object.parent;
while(parent.constructor != p_ancestor && parent.constructor != Application) {
parent = parent.parent;
}
if(parent.constructor === Application && p_ancestor !== Application) {
return false;
} else {
return parent;
}
};
/**
* Get ruler unit type of view preferences measurement units of the orientation passed as an
* argument
*/
var getRulerUnit = function(p_viewPreferences, p_orientation) {
var units;
if(p_orientation == 'horizontal') {
units = p_viewPreferences.horizontalMeasurementUnits;
} else {
units = p_viewPreferences.verticalMeasurementUnits;
}
switch (units) {
case 2051106676:
return 'ag';
case 2053336435:
return 'cm';
case 2053335395:
return 'c';
case 2053729891:
return 'in';
case 2053729892:
return 'in';
case 2053991795:
return 'mm';
case 2054187363:
return 'p';
case 2054188905:
return 'pt';
}
};
/**
* Get page items in document with the same object style as page item argument
*/
var getPageItemsByObjectStyle = function(p_pageItem) {
var objectStyle,
document,
pageItems,
pageItem,
i = 0,
leng,
matches = [];
objectStyle = p_pageItem.appliedObjectStyle;
document = getAncestor(objectStyle, Document);
pageItems = document.allPageItems;
for(leng = pageItems.length; i < leng; i++) {
pageItem = pageItems[i];
if(pageItem.appliedObjectStyle === objectStyle) {
matches.push(pageItem);
}
}
return matches;
};
/**
* Get width and height of a page item
*/
var getPageItemSize = function(p_item) {
var bounds = p_item.geometricBounds,
width = bounds[3] - bounds[1],
height = bounds[2] - bounds[0];
return [width, height];
};
/**
* Set the size of a page item relatively or absolutely depending on the relative argument passed
*/
var setPageItemSize = function(p_pageItem, p_measurements, p_relative) {
var method,
i = 0,
unitValue,
document = getAncestor(p_pageItem, Document),
directions = ['horizontal', 'vertical'],
points = [];
if(p_relative) {
method = ResizeMethods.ADDING_CURRENT_DIMENSIONS_TO;
} else {
method = ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH;
}
for(; i < 2; i++) {
unitValue = getUnitValue(p_measurements[i], getRulerUnit(document.viewPreferences, directions[i]));
points[i] = unitValue.as('pt');
}
try {
p_pageItem.resize(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.centerAnchor, method, points);
return true;
} catch(p_error) {
return false;
}
};
/**
* Attempt to create an Adobe UnitValue object from value with an option of a fallback unit type
* where value passed is a number without appended unit type
* @todo Implement better solution for managing unit types unsupported by the UnitValue object
*/
var getUnitValue = function(p_value, p_fallbackUnit) {
var pattern = /^ *([\-+]?(?:\d+(?:\.\d*)?|\.\d+)) ?(\w+)* *$/,
string = p_value + '',
match = string.match(pattern),
unitType,
unitValue;
if(match) {
if(match[2]) {
unitType = match[2];
} else {
unitType = p_fallbackUnit;
}
try {
unitValue = UnitValue(match[1], unitType);
} catch (p_error) {
// Catch unsuported unit value types like picas and ciceros
unitValue = UnitValue(match[1], 'pt');
}
if(unitValue.type !== '?') {
return unitValue;
}
}
return false;
};
/**
* Make user interface using ScriptUI layout engine returning object literal containing references
* to key interface elements for hooking on to
*/
var layoutInterface = function(p_message) {
var palette,
group,
radiosRelative,
radiosAbsolute,
staticText,
widthEditText,
heightEditText,
resizeButton;
palette = new Window('palette', p_message);
palette.alignChildren = ['', 'center'];
group = palette.add('group');
group = group.add('panel');
group.alignChildren = ['left', 'center'];
radiosRelative = group.add('radiobutton', undefined, 'Relative');
radiosAbsolute = group.add('radiobutton', undefined, 'Absolute');
group = group.parent;
group = group.add('group');
group.orientation = 'column';
group.alignChildren = ['right', 'center'];
group = group.add('group');
staticText = group.add('statictext');
staticText.text = 'Width:';
widthEditText = group.add('edittext');
widthEditText.characters = 10;
group = group.parent;
group = group.add('group');
staticText = group.add('statictext');
staticText.text = 'Height:';
heightEditText = group.add('edittext');
heightEditText.characters = 10;
resizeButton = palette.add('button', undefined, 'Resize', {name: 'ok'});
return {
palette: palette,
radios: {
relative: radiosRelative,
absolute: radiosAbsolute
},
editTexts: {
width: widthEditText,
height: heightEditText
},
resizeButton: resizeButton
};
};
/**
* Event listener for when palette show event that initialises the user interface elements
*/
var paletteShow = function(p_editTexts, p_radios) {
p_radios.relative.value = true;
radioClick(p_editTexts, p_radios);
p_editTexts.width.active = true;
};
/**
* Event listener for edit text defocusing event that updates the text in the edit text element to a
* valid measurement that reflects the measurement unit type of the active document or application
*/
var editTextBlur = function(p_editText, p_orientation) {
var text = p_editText.text,
viewPreferences,
direction,
unitValue;
if (app.documents.length) {
viewPreferences = app.activeDocument.viewPreferences;
} else {
viewPreferences = app.viewPreferences;
}
unitValue = getUnitValue(text, getRulerUnit(viewPreferences, p_orientation));
if (unitValue) {
p_editText.text = unitValue;
} else {
alert(text + ' is not a valid measurment');
p_editText.active = true;
}
};
/**
* Event listener for radio button click event that sets the default values for edit text elements
* based on the radio button triggering the event
*/
var radioClick = function(p_editTexts, p_radios) {
var document,
viewPreferences,
size = [0, 0]; // Default values for edit text elements
if(app.documents.length) {
document = app.activeDocument;
viewPreferences = document.viewPreferences;
if (p_radios.absolute.value && document.selection.length === 1) {
// Set to the dimensions of the selected object
size = getPageItemSize(document.selection[0]);
}
} else {
// No documents open use application view preferences
viewPreferences = app.viewPreferences;
}
p_editTexts.width.text = getUnitValue(size[0], getRulerUnit(viewPreferences, 'horizontal'));
p_editTexts.height.text = getUnitValue(size[1], getRulerUnit(viewPreferences, 'vertical'));
};
/**
* Event listener for resize button click event that attempts to resize page items based on user
* inputted measurements
*/
var resizeButtonClick = function(p_editTexts, p_radios) {
var document,
pageItems,
i = 0,
leng,
relative,
error = false;
if(!app.documents.length) {
alert('There are currently no open InDesign documents.');
return;
}
document = app.activeDocument;
if(document.selection.length !== 1) {
alert('A single page item with the object style of the page items you would like to resize must be selected.');
return;
}
pageItems = getPageItemsByObjectStyle(document.selection[0]);
leng = pageItems.length;
relative = p_radios.relative.value || false;
for(; i < leng; i++) {
if (!setPageItemSize(pageItems[i], [p_editTexts.width.text, p_editTexts.height.text], relative)) {
error = true;
}
}
if (error) {
alert('Some pate items were unable to be resized because the resulting measurement is not a legal dimension.');
}
};
/**
* Add event listeners to the user interface elements
*/
var addEventListeners = function(p_elements) {
var editTexts = p_elements.editTexts,
radios = p_elements.radios
p_elements.palette.onShow = function() {
paletteShow(editTexts, radios);
}
radios.relative.onClick = function() {
radioClick(editTexts, radios);
};
radios.absolute.onClick = function() {
radioClick(editTexts, radios);
};
editTexts.width.addEventListener('blur', function() {
editTextBlur(editTexts.width, 'horizontal');
});
editTexts.height.addEventListener('blur', function() {
editTextBlur(editTexts.height, 'vertical');
});
p_elements.resizeButton.onClick = function() {
resizeButtonClick(editTexts, radios);
};
};
// Check if user interface already loaded into memory
if(typeof interfaceElements === 'undefined') {
interfaceElements = layoutInterface('Resize page items by style');
addEventListeners(interfaceElements);
}
// Show user interface
interfaceElements.palette.show();