-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathminusOffset.jsx
237 lines (205 loc) · 9.2 KB
/
minusOffset.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
/*
Program version: Adobe Illustrator CS5+
Name: minusOffset.jsx;
Author: Alexander Ladygin, email: [email protected]
Copyright (c) 2018
www.ladyginpro.ru
*/
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
$.getUnits = function (val, def) {return 'px,pt,mm,cm,in,pc'.indexOf(val.slice(-2)) > -1 ? val.slice(-2) : def;};
$.convertUnits = function (obj, b) {if (obj === undefined) {return obj;}if (b === undefined) {b = 'px';}if (typeof obj === 'number') {obj = obj + 'px';}if (typeof obj === 'string') {var unit = $.getUnits(obj),val = parseFloat(obj);if (unit && !isNaN(val)) {obj = val;}else if (!isNaN(val)) {obj = val; unit = 'px';}}if (((unit === 'px') || (unit === 'pt')) && (b === 'mm')) {obj = parseFloat(obj) / 2.83464566929134;}else if (((unit === 'px') || (unit === 'pt')) && (b === 'cm')) {obj = parseFloat(obj) / (2.83464566929134 * 10);}else if (((unit === 'px') || (unit === 'pt')) && (b === 'in')) {obj = parseFloat(obj) / 72;}else if ((unit === 'mm') && ((b === 'px') || (b === 'pt'))) {obj = parseFloat(obj) * 2.83464566929134;}else if ((unit === 'mm') && (b === 'cm')) {obj = parseFloat(obj) * 10;}else if ((unit === 'mm') && (b === 'in')) {obj = parseFloat(obj) / 25.4;}else if ((unit === 'cm') && ((b === 'px') || (b === 'pt'))) {obj = parseFloat(obj) * 2.83464566929134 * 10;}else if ((unit === 'cm') && (b === 'mm')) {obj = parseFloat(obj) / 10;}else if ((unit === 'cm') && (b === 'in')) {obj = parseFloat(obj) * 2.54;}else if ((unit === 'in') && ((b === 'px') || (b === 'pt'))) {obj = parseFloat(obj) * 72;}else if ((unit === 'in') && (b === 'mm')) {obj = parseFloat(obj) * 25.4;}else if ((unit === 'in') && (b === 'cm')) {obj = parseFloat(obj) * 25.4;}return parseFloat(obj);};
var scriptName = 'minusOffset',
copyright = ' \u00A9 www.ladyginpro.ru',
settingFile = {
name: scriptName + '__setting.json',
folder: Folder.myDocuments + '/LA_AI_Scripts/'
},
firstItemName = '__' + scriptName + '__firstName',
$nesting = 1000;
function getLiveOffsetProps() {
return '<LiveEffect name="Adobe Offset Path"><Dict data="R mlim ' +
parseFloat(__miterlimit.text) + ' R ofst ' +
$.convertUnits(__offset.text, 'px') + ' I jntp ' +
__joins.selection.index +
' "/></LiveEffect>';
}
function liveOffset (item) {
item.applyEffect(getLiveOffsetProps());
app.executeMenuCommand('expandStyle');
return item;
}
function __group (items, placement) {
var i = items.length;
if (i > 0) {
var $group = activeDocument.groupItems.add();
$group.moveBefore(placement);
while (i--) items[i].moveToBeginning($group);
return $group;
}
}
function __ungroup ($group) {
var i = $group.pageItems.length;
if (i > 0) while (i--) $group.pageItems[i].moveBefore($group);
$group.remove();
}
function minusOffset (topItem, items, nestingLevel) {
var l = items.length,
removeCollection = [];
if (l > (!nestingLevel ? 1 : 0)) {
selection = null;
if (!nestingLevel) progressBarCounter = progressBar.maxvalue / l;
for (var i = (!nestingLevel ? 1 : 0); i < l; i++) {
if (__inGroups.value && (nestingLevel < $nesting) && items[i].typename === 'GroupItem') {
if (items[i].pageItems.length) minusOffset(topItem, items[i].pageItems, nestingLevel + 1);
}
else {
var firstItem = liveOffset(topItem.duplicate());
firstItem.name = firstItemName;
__group([items[i], firstItem], items[i]).selected = true;
app.executeMenuCommand('Live Pathfinder Minus Back');
app.executeMenuCommand('expandStyle');
try {
selection[0].pageItems.getByName(firstItemName).remove();
removeCollection.push(selection[0]);
}
catch (e) {
if (selection[0] && selection[0].typename === 'GroupItem') __ungroup(selection[0]);
}
selection = null;
}
if (!nestingLevel) {
progressBar.value += progressBarCounter;
win.update();
}
}
var j = removeCollection.length;
if (j > 0) while (j--) removeCollection[j].remove();
}
return topItem;
}
function inputNumberEvents (ev, _input, min, max, callback){
var step,
_dir = (ev.keyName ? ev.keyName.toLowerCase().slice(0,1) : '#none#'),
_value = parseFloat(_input.text),
units = (',px,pt,mm,cm,in,'.indexOf(_input.text.length > 2 ? (',' + _input.text.replace(/ /g, '').slice(-2) + ',') : ',!,') > -1 ? _input.text.replace(/ /g, '').slice(-2) : '');
min = (min === undefined ? 0 : min);
max = (max === undefined ? Infinity : max);
step = (ev.shiftKey ? 10 : (ev.ctrlKey ? .1 : 1));
if (isNaN(_value)) {
_input.text = min;
}
else {
_value = ( ((_dir === 'u') || (_dir === 'r')) ? _value + step : (((_dir === 'd') || (_dir === 'l')) ? _value - step : false) );
if (_value !== false) {
_value = (_value <= min ? min : (_value >= max ? max : _value))
_input.text = _value;
if (callback instanceof Function) callback(_value, _input, min, max, units);
else if (units) _input.text = parseFloat(_input.text) + ' ' + units;
}
else if (units) _input.text = parseFloat(_input.text) + ' ' + units;
}
}
var win = new Window('dialog', scriptName + copyright);
win.orientation = 'column';
win.alignChildren = 'fill';
win.addEventListener('keydown', function (e) {
if (e.keyName.toLowerCase() === 'enter') {
startAction();
}
});
with (globalGroup = win.add('group')) {
orientation = 'column';
alignChildren = 'fill';
with (add('panel')) {
orientation = 'column';
alignChildren = 'left';
with (add('group')) {
add('statictext', undefined, 'Offset:');
var __offset = add('edittext', [0, 0, 90, 25], '10 px');
__offset.active = true;
__offset.addEventListener('keydown', function (e) { inputNumberEvents(e, this, -Infinity, Infinity) });
}
with (add('group')) {
add('statictext', undefined, 'Joins:');
var __joins = add('dropdownlist', [0, 0, 97, 25], 'Round,Bevel,Miter'.split(','));
__joins.selection = 2;
}
with (add('group')) {
add('statictext', undefined, 'Miter limit:');
var __miterlimit = add('edittext', [0, 0, 70, 25], 4);
__miterlimit.addEventListener('keydown', function (e) { inputNumberEvents(e, this, -Infinity, Infinity) });
}
var __inGroups = add('checkbox', undefined, 'Each in the group');
__inGroups.onClick = function() { nestingUIGroup.enabled = this.value; }
__inGroups.value = false;
with (nestingUIGroup = add('group')) {
enabled = false;
add('statictext', undefined, 'Group nesting:');
var __groupNesting = add('edittext', [0, 0, 45, 25], '1000');
__groupNesting.addEventListener('keydown', function (e) { inputNumberEvents(e, this, 1, Infinity) });
}
}
with (add('group')) {
var cancel = add('button', undefined, 'Cancel');
cancel.helpTip = 'Press Esc to Close';
cancel.onClick = function () { win.close(); }
var ok = add('button', undefined, 'Run');
ok.helpTip = 'Press Enter to Run';
ok.onClick = startAction;
// ok.active = true;
}
}
var progressBar = win.add('progressbar'),
progressBarCounter = 100;
progressBar.value = progressBar.minvalue = 0;
progressBar.maxvalue = progressBarCounter;
progressBar.maximumSize = [1000, 5];
function startAction() {
$nesting = parseInt(__groupNesting.text) || 1000;
globalGroup.enabled = false;
try{
minusOffset(selection[0], selection, 0);
}catch(err){alert(err + '\n' + err.line);}
win.close();
}
function saveSettings() {
var $file = new File(settingFile.folder + settingFile.name),
data = [
__offset.text,
__miterlimit.text,
__joins.selection.index,
__inGroups.value,
__groupNesting.text
].toString();
$file.open('w');
$file.write(data);
$file.close();
}
function loadSettings() {
var $file = File(settingFile.folder + settingFile.name);
if ($file.exists) {
try {
$file.open('r');
var data = $file.read().split('\n'),
$main = data[0].split(',');
__offset.text = $main[0];
__miterlimit.text = $main[1];
__joins.selection.index = parseInt($main[2]);
__inGroups.value = ($main[3] === 'true');
__groupNesting.text = $main[4];
nestingUIGroup.enabled = __inGroups.value;
} catch (e) {}
$file.close();
}
}
win.onClose = function(){
saveSettings();
}
function checkSettingFolder() {
var $folder = new Folder(settingFile.folder);
if (!$folder.exists) $folder.create();
}
checkSettingFolder();
loadSettings();
win.center();
win.show();