-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathExport-selection-as-AI.jsx
executable file
·267 lines (225 loc) · 8.03 KB
/
Export-selection-as-AI.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
/*
Export-selection-as-AI.jsx for Adobe Illustrator
Description: Exports all selected objects to AI files
Date: October, 2022
Author: Sergey Osokin, email: [email protected]
Based on Layers to SVG 0.1 by Anton Ball
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
0.1.1 Minor improvements
0.2 Minor improvements
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: 'Export Selection As Ai',
version: 'v.0.2'
},
CFG = {
isMac: /mac/i.test($.os),
sep: '-', // Separator symbol
isSep: true, // Default save each object
isFit: true, // Default fit artboard to artwork
ext: '.ai', // File extension
uiMargin: [10, 15, 10, 10],
};
if (!isCorrectEnv('version:16', 'selection')) return;
var doc = app.activeDocument,
fName = getFileName(doc),
outFol = (doc.path != '') ? doc.path : Folder.desktop,
sel = selection;
// DIALOG
var win = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version);
win.alignChildren = 'center';
var folPnl = win.add('panel', undefined, 'Output folder');
folPnl.orientation = 'row';
folPnl.margins = CFG.uiMargin;
var folBtn = folPnl.add('button', undefined, 'Select');
var folInp = folPnl.add('edittext', undefined);
folInp.text = decodeURI(outFol);
folInp.characters = 20;
var fNameGrp = win.add('group');
var namePnl = fNameGrp.add('panel', undefined, 'File name prefix');
namePnl.orientation = 'row';
namePnl.margins = CFG.uiMargin;
var namePre = namePnl.add('edittext', undefined, fName);
namePre.characters = 20;
var sepPnl = fNameGrp.add('panel', undefined, 'Separator');
sepPnl.margins = CFG.uiMargin;
var sym = sepPnl.add('edittext', undefined, CFG.sep);
sym.characters = 4;
sym.enabled = CFG.isSep;
var prgPnl = win.add('panel', undefined, 'Progress');
prgPnl.margins = CFG.uiMargin;
var progBar = prgPnl.add('progressbar', [20, 15, 276, 20], 0, 100);
var optPnl = win.add('group');
optPnl.orientation = 'column';
optPnl.alignChildren = 'left';
var isSeparate = optPnl.add('checkbox', undefined, 'Save each object to a separate file');
isSeparate.value = CFG.isSep;
var isFitAb = optPnl.add('checkbox', undefined, 'Fit artboard to selected art');
isFitAb.value = CFG.isFit;
var btns = win.add('group');
var cancel, ok;
if (CFG.isMac) {
cancel = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
ok = btns.add('button', undefined, 'Export', { name: 'ok' });
} else {
ok = btns.add('button', undefined, 'Export', { name: 'ok' });
cancel = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
}
var copyright = win.add('statictext', undefined, '\u00A9 Sergey Osokin. Visit Github');
copyright.justify = 'center';
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold');
});
// Click functions
isSeparate.onClick = function () {
sym.enabled = this.value;
}
folBtn.onClick = function () {
var fol = Folder.selectDialog('Select a folder to export');
if (fol != null) {
folInp.text = decodeURI(fol);
outFol = fol;
}
}
cancel.onClick = win.close;
ok.onClick = start;
function start() {
var colMode = doc.documentColorSpace;
outFol = decodeURI(folInp.text);
if (isEmpty(outFol.toString())) {
folInp.text = (doc.path != '') ? doc.path : Folder.desktop;
return;
}
if (!Folder(outFol).exists) {
alert("This folder doesn't exist");
folInp.text = (doc.path != '') ? doc.path : Folder.desktop;
return;
}
if (!isEmpty(namePre.text)) {
fName = namePre.text.trim();
} else {
alert('Please enter file name prefix');
return;
}
if (isSeparate.value) {
if (!isEmpty(sym.text)) CFG.sep = sym.text;
fName = fName + CFG.sep;
var outFile;
for (var i = 0, len = sel.length; i < len; i++) {
progBar.value = i * (100.0 / (len - 1)); // Change progress bar
var item = sel[i];
outFile = File(outFol + '/' + fName + (i + 1) + CFG.ext);
saveToDoc(item, outFile, colMode, isFitAb.value, isSeparate.value);
}
} else {
if ((outFol == doc.path) && (fName == getFileName(doc))) fName += '_copy';
outFile = File(outFol + '/' + fName + CFG.ext);
saveToDoc(sel, outFile, colMode, isFitAb.value, isSeparate.value);
}
win.close();
}
win.center();
win.show();
}
// Check the script environment
function isCorrectEnv() {
var args = ['app', 'document'];
args.push.apply(args, arguments);
for (var i = 0; i < args.length; i++) {
var arg = args[i].toString().toLowerCase();
switch (true) {
case /app/g.test(arg):
if (!/illustrator/i.test(app.name)) {
alert('Error\nRun script from Adobe Illustrator');
return false;
}
break;
case /version/g.test(arg):
var rqdVers = parseFloat(arg.split(':')[1]);
if (parseFloat(app.version) < rqdVers) {
alert('Error\nSorry, script only works in Illustrator v.' + rqdVers + ' and later');
return false;
}
break;
case /document/g.test(arg):
if (!documents.length) {
alert('Error\nOpen a document and try again');
return false;
}
break;
case /selection/g.test(arg):
if (!selection.length || selection.typename === 'TextRange') {
alert('Error\nPlease, select at least one object');
return false;
}
break;
}
}
return true;
}
// Get document name without extension
function getFileName(f) {
return decodeURI(f.name.replace(/\.[^\.]+$/, ''));
}
// Check empty string
function isEmpty(str) {
return str.replace(/\s/g, '').length == 0;
}
// Remove whitespaces from start and end of string
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
}
// Copy selection to a new document, and save it as an AI file
function saveToDoc(items, f, col, isFit, isSep) {
var doc = app.documents.add(col);
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
copyObjectsTo(items, doc);
// Resize the artboard to the object
if (isFit) app.executeMenuCommand('Fit Artboard to artwork bounds');
try {
doc.saveAs(f, IllustratorSaveOptions);
doc.close();
} catch (e) { }
}
// Duplicate objects and add them to a document
function copyObjectsTo(items, doc) {
if (items instanceof Array) {
for (var i = 0, len = items.length; i < len; i++) {
items[i].duplicate(doc.activeLayer, ElementPlacement.PLACEATEND);
}
} else {
items.duplicate(doc.activeLayer, ElementPlacement.PLACEATBEGINNING);
}
}
// Open link in browser
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) {}