-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathMoveToGroup.jsx
225 lines (190 loc) · 6.62 KB
/
MoveToGroup.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
/*
MoveToGroup.jsx for Adobe Illustrator
Description: Move the selected items to the first upper or lower group
Date: September, 2022
Modification date: February, 2024
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1.2 Removed radiobutton activation on Windows OS below CC v26.4
0.1.1 Fixed radiobutton activation in Windows OS
0.1 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
$.localize = true; // Enabling automatic localization
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
var SCRIPT = {
name: 'Move To Group',
version: 'v0.1.2'
},
CFG = {
aiVers: parseFloat(app.version),
isMac: /mac/i.test($.os)
},
LANG = {
errDoc: { en: 'Error\nOpen a document and try again',
ru: 'Ошибка\nОткройте документ и запустите скрипт' },
errSel: { en: 'Error\nPlease, select multiple items with group',
ru: 'Ошибка\nВыделите несколько объектов с группой' },
errGroup: { en: 'Error\nThe selection does not have a group',
ru: 'Ошибка\nСреди выделенных объектов нет группы' },
pnlTitle: { en: 'Target', ru: 'Назначение' },
top: { en: 'Top group', ru: 'Верхняя группа' },
bottom: { en: 'Bottom group', ru: 'Нижняя группа' },
cancel: { en: 'Cancel', ru: 'Отмена' },
ok: { en: 'Ok', ru: 'Готово' }
};
// Main function
function main() {
if (!documents.length) {
alert(LANG.errDoc);
return;
}
if (!selection.length || selection.typename == 'TextRange') {
alert(LANG.errSel);
return;
}
var groupCount = countGroups();
try {
switch (groupCount) {
case 0: // No group
alert(LANG.errGroup);
return;
case 1: // The selection has one group
moveItems();
break;
case 2: // The selection has many group
// INTERFACE
var dialog = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version);
dialog.orientation = 'column';
dialog.alignChildren = ['fill', 'fill'];
dialog.opacity = .97;
var pnlTarget = dialog.add('panel', undefined, LANG.pnlTitle);
pnlTarget.orientation = 'column';
pnlTarget.alignChildren = ['left', 'top'];
pnlTarget.margins = [10, 20, 10, 10];
var rbTop = pnlTarget.add('radiobutton', undefined, LANG.top);
var rbBottom = pnlTarget.add('radiobutton', undefined, LANG.bottom);
rbBottom.value = true;
if (CFG.isMac || CFG.aiVers >= 26.4 || CFG.aiVers <= 17) {
rbBottom.active = true;
}
var btns = dialog.add('group');
btns.orientation = 'column';
btns.alignChildren = ['fill', 'center'];
var cancel = btns.add('button', undefined, LANG.cancel, { name: 'cancel' });
var ok = btns.add('button', undefined, LANG.ok, { name: 'ok' });
var copyright = dialog.add('statictext', undefined, 'Visit Github');
copyright.justify = 'center';
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold');
});
cancel.onClick = dialog.close;
ok.onClick = okClick;
dialog.center();
dialog.show();
break;
}
} catch (e) {}
function okClick() {
moveItems(rbTop.value);
dialog.close();
}
}
/**
* Search for groups in the selection
* @return {number} number of groups
*/
function countGroups() {
var count = 0;
for (var i = 0, len = selection.length; i < len; i++) {
if (count == 2) return count; // Enough to display the dialog
if (selection[i].typename === 'GroupItem') count++;
}
return count;
}
/**
* Move items to a group in their order
* @param {boolean} isTop - move to the top group or bottom group
*/
function moveItems(isTop) {
if (!arguments.length) isTop = true;
var data = isTop ? collectForTop() : collectForBottom();
for (var i = data.before.length - 1; i >= 0; i--) {
data.before[i].move(data.target, ElementPlacement.PLACEATBEGINNING);
}
for (var j = data.after.length - 1; j >= 0; j--) {
data.after[j].move(data.target, ElementPlacement.PLACEATEND);
}
}
/**
* Search for a top target group and collect items before and after it
* @return {object} top group and items arrays
*/
function collectForTop() {
var arrAfter = [],
arrBefore = [];
for (var i = 0, len = selection.length; i < len; i++) {
if (selection[i].typename === 'GroupItem') {
var target = selection[i];
groupIdx = i;
break;
} else {
// Get the items above the group
arrBefore.push(selection[i]);
}
}
// Get items under a group
for (var j = selection.length - 1; j > groupIdx; j--) arrAfter.push(selection[j]);
return { 'target': target, 'before': arrBefore, 'after': arrAfter };
}
/**
* Search for a bottom target group and collect items before and after it
* @return {object} bottom group and items arrays
*/
function collectForBottom() {
var arrAfter = [],
arrBefore = [];
for (var i = selection.length - 1; i >= 0; i--) {
if (selection[i].typename === 'GroupItem') {
var target = selection[i];
groupIdx = i;
break;
} else {
// Get items under a group
arrAfter.push(selection[i]);
}
}
// Get the items above the group
for (var j = 0; j < groupIdx; j++) arrBefore.push(selection[j]);
return { 'target': target, 'before': arrBefore, 'after': arrAfter };
}
/**
* 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) {}