-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathMirrorMove.jsx
375 lines (322 loc) · 9.75 KB
/
MirrorMove.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
/*
MirrorMove.jsx for Adobe Illustrator
Description: Mirror movement the object or points using the last values of the Object > Transform > Move...
Date: May, 2022
Modification date: August, 2024
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1.1 Changed silent start with latest settings
0.1.0 Initial version
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2025 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
// Main function
function main() {
var SCRIPT = {
name : 'Mirror Move',
version : 'v0.1.1'
},
CFG = {
aiVers : parseInt(app.version),
axis : 'XY', // XY, X, Y
ratio : 1.0, // Movement ratio
showUI : false // Silent mode or dialog
},
SETTINGS = {
name : SCRIPT.name.replace(/\s/g, '_') + '_data.json',
folder : Folder.myDocuments + '/Adobe Scripts/'
};
if (!documents.length) {
alert('Error\nOpen a document and try again');
return;
}
if (CFG.aiVers < 16) {
alert('Error\nSorry, script only works in Illustrator CS6 and later');
return;
}
if (selection.length == 0 || selection.typename == 'TextRange') {
alert('Error\nPlease, select one or more paths or path points');
return;
}
var isAltPressed = false;
if (ScriptUI.environment.keyboardState.altKey) {
isAltPressed = true;
}
// Show dialog
if ((CFG.showUI && !isAltPressed) || (!CFG.showUI && isAltPressed)) {
invokeUI(SCRIPT, CFG, SETTINGS);
} else {
var params = loadSettings(SETTINGS);
if (params.length) { // Silent mode with the latest settings
process(params[0], params[1]);
} else { // Silent mode with the default settings
process(CFG.axis, CFG.ratio);
}
}
}
/**
* Show UI
* @param {Object} title - The script name
* @param {Object} CFG - Default settings
* @param {Object} cfgFile - Settings file
* @param {Array} paths - Selected paths
*/
function invokeUI(title, cfg, cfgFile) {
var params = loadSettings(cfgFile);
var win = new Window('dialog', title.name + ' ' + title.version);
win.orientation = 'column';
win.alignChildren = ['fill', 'center'];
win.opacity = .98;
var axisPnl = win.add('panel', undefined, 'Movement axis');
axisPnl.orientation = 'row';
axisPnl.margins = [10, 15, 10, 7];
axisPnl.alignChildren = ['fill', 'center'];
var xyRb = axisPnl.add('radiobutton', undefined, 'XY');
var xRb = axisPnl.add('radiobutton', undefined, 'X');
var yRb = axisPnl.add('radiobutton', undefined, 'Y');
if (params.length) {
for (var i = 0; i < axisPnl.children.length; i++) {
if (params[0] == axisPnl.children[i].text)
axisPnl.children[i].value = true;
}
} else {
xyRb.value = true;
}
var ratioGrp = win.add('group');
ratioGrp.alignChildren = ['fill', 'center'];
ratioGrp.add('statictext', undefined, 'Movement ratio');
var ratioInp = ratioGrp.add('edittext', undefined, params.length ? params[1] : cfg.ratio);
ratioInp.preferredSize.width = 60;
var btns = win.add('group');
btns.alignChildren = ['fill', 'center'];
var cancel = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
var ok = btns.add('button', undefined, 'Ok', { name: 'ok' });
var copyright = win.add('statictext', undefined, '\u00A9 Sergey Osokin. Visit Github');
copyright.justify = 'center';
cancel.onClick = win.close;
ok.onClick = function() {
var params = [
xyRb.value ? 'XY' : (xRb.value ? 'X' : 'Y'),
strToAbsNum(ratioInp.text, cfg.ratio)
];
saveSettings(cfgFile, params);
process(params[0], params[1]);
win.close();
}
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold');
});
win.center();
win.show();
}
/**
* Save UI options to file
* @param {Object} cfgFile - Settings file
* @param {Array} params - Options status
*/
function saveSettings(cfgFile, params) {
if(!Folder(cfgFile.folder).exists) Folder(cfgFile.folder).create();
var $file = new File(cfgFile.folder + cfgFile.name);
$file.encoding = 'UTF-8';
$file.open('w');
var pref = {};
pref.axis = params[0];
pref.ratio = params[1];
var data = pref.toSource();
$file.write(data);
$file.close();
}
/**
* Load options from file
* @param {Object} cfgFile - Settings file
* @return {Array} out - Options status
*/
function loadSettings(cfgFile) {
var out = [], $file = File(cfgFile.folder + cfgFile.name);
if ($file.exists) {
try {
$file.encoding = 'UTF-8';
$file.open('r');
var json = $file.readln();
var pref = new Function('return ' + json)();
$file.close();
if (typeof pref != 'undefined') {
out[0] = pref.axis;
out[1] = pref.ratio;
}
} catch (e) {}
}
return out;
}
/**
* Run processing
* @param {string} axis - Movement axis key
* @param {number} ratio - Movement ratio
*/
function process(axis, ratio) {
var items = getItems(selection),
isX = /x/i.test(axis),
isY = /y/i.test(axis),
oldPos = getPositions(items); // Save current point positions
app.executeMenuCommand('transformagain');
// Update items positions
items = getItems(selection);
var newPos = getPositions(items);
// Reduce operations in the Illustrator history
app.undo();
items = getItems(selection);
for (var i = 0, len = oldPos.length; i < len; i++) {
if (!isEqualArr(oldPos[i], newPos[i])) {
move(items[i], oldPos[i], newPos[i], isX, isY, ratio);
}
}
}
/**
* Get single item and points from selection
* @param {(Object|Array)} collection - Set of items
* @return {Array} out - Items
*/
function getItems(collection) {
var out = [];
for (var i = 0; i < collection.length; i++) {
var item = collection[i];
if (item.pageItems && item.pageItems.length) {
out = [].concat(out, getItems(item.pageItems));
} else if (/compound/i.test(item.typename) && item.pathItems.length) {
out = [].concat(out, getItems(item.pathItems));
} else if (/pathitem/i.test(item.typename)) {
out = out.concat(getPoints(item));
} else {
out.push(item);
}
}
return out;
}
/**
* Get selected points
* @param {Object} item - Current object
* @return {Array} out - Selected points
*/
function getPoints(item) {
var out = [];
if (item.pathPoints && item.pathPoints.length > 1) {
var points = item.pathPoints;
for (var i = 0, len = points.length; i < len; i++) {
if (isSelected(points[i])) out.push(points[i]);
}
}
return out;
}
/**
* Check PathPoint
* @param {Object} item - Current object
* @return {boolean} PathPoint or not
*/
function isPoint(item) {
return /point/i.test(item.typename);
}
/**
* Check PathPoint is selected
* @param {Object} point - Current point
* @return {boolean} Selected or not
*/
function isSelected(point) {
return point.selected == PathPointSelection.ANCHORPOINT;
}
/**
* Get selected points on paths
* @param {Array} items - Objects and points
* @return {Array} out - Items coordinate pairs
*/
function getPositions(items) {
var out = [];
for (var i = 0, len = items.length; i < len; i++) {
if (isPoint(items[i])) {
out[i] = items[i].anchor;
} else {
out[i] = items[i].position;
}
}
return out;
}
/**
* Compare arrays
* @param {Array} a
* @param {Array} b
* @return {boolean} Equal or not
*/
function isEqualArr(a, b) {
if (a.length !== 0 && a.length === b.length) {
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
return false;
}
/**
* Move item to position
* @param {Object} item - Object or point
* @param {Array} pos1 - Old position
* @param {Array} pos2 - Current position
* @param {boolean} isX - X-axis move
* @param {boolean} isY - Y-axis move
* @param {number} ratio - Movement ratio
*/
function move(item, pos1, pos2, isX, isY, ratio) {
var x = (isX ? 1 : -1) * ratio * (pos1[0] - pos2[0]),
y = (isY ? 1 : -1) * ratio * (pos1[1] - pos2[1]);
if (isPoint(item)) {
with (item) {
anchor = [anchor[0] + x, anchor[1] + y];
leftDirection = [leftDirection[0] + x, leftDirection[1] + y];
rightDirection = [rightDirection[0] + x, rightDirection[1] + y];
}
} else {
item.translate(x, y);
}
}
/**
* Convert string to absolute number
* @param {string} str - Input data
* @param {number} def - Default value if the string don't contain digits
* @return {number}
*/
function strToAbsNum(str, def) {
if (arguments.length == 1 || def == undefined) def = 1;
str = str.replace(/,/g, '.').replace(/[^\d.]/g, '');
str = str.split('.');
str = str[0] ? str[0] + '.' + str.slice(1).join('') : '';
if (isNaN(str) || !str.length) return parseFloat(def);
else return parseFloat(str);
}
/**
* Open link in browser
* @param {string} url - Website adress
*/
function openURL(url) {
var html = new File(Folder.temp.absoluteURI + '/aisLink.html');
html.open('w');
var htmlBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' + url + '"></head><body> <p></body></html>';
html.write(htmlBody);
html.close();
html.execute();
}
// Run script
try {
main();
} catch (e) {}