-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiExportJPEGs.jsx
155 lines (124 loc) · 4.3 KB
/
aiExportJPEGs.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
/**********************************************************
Written by: github.com/moelipp
Version: 2020.2
Date: March 2020
aiExportJPEGs.jsx
DESCRIPTION
This script selects a folder in illustrator, opens the .ai
files in that folder one by one, and saves artboards 2 - 5
out as JPEGs. The naming convention for the JPEGs is the
first 11 digits of the filename, followed by a letter
depending on whether the filepath contains the word
'apparel' or not.
For hardgoods: Artboard 1 = 0 (ignored)
Artboard 2 = A
Artboard 3 = B
Artboard 4 = C
Artboard 5 = D
For apparel: Artboard 1 = 0 (ignored)
Artboard 2 = D
Artboard 3 = A
Artboard 4 = C
Artboard 5 = B
**********************************************************/
"use strict";
var fo = Folder.selectDialog("Choose a folder containing .ai files");
var foOut = Folder.selectDialog("Choose an output folder");
var files;
var n = 0;
var uip;
var result
var errors = [];
var jpegOpts;
function getFiles(fo, aExtensions, bRecursive, aFiles, includeFolder) {
var exts = aExtensions ? aExtensions.join("|") : ".+" ;
var pattern = new RegExp ( "\\."+exts+"$", "g" );
var files = aFiles ? aFiles : [];
var filterFunction = function(file) {
return pattern.test(file.name);
}
if ( bRecursive ) {
var foFiles = fo.getFiles();
while ( f = foFiles.shift() ) {
if ( f instanceof Folder ) {
if (includeFolder===true) files[ files.length ] = f; this.getFiles ( f, aExtensions, true, files );
}
if ( f instanceof File && pattern.test ( f.name ) && f.name!=".DS_Store" ) {
files[ files.length ] = f;
}
}
return files;
}
else {
return fo.getFiles(filterFunction);
}
}
function exportFile(file, jpegOpts) {
var doc = app.open(file);
var data = {ok:true};
var alphabet = [];
var testStr = /.apparel./i;
// search entire filepath (var fo) for string 'apparel' and assign relevant
// department to var dept based on result
testStr.test(fo) ? dept = 'apparel' : dept = 'hardgoods';
// order of artboards is different for apparel and hardgoods, so change
// letters assigned to artboards based value of var dept
if ( dept == 'apparel' ) {
alphabet = ["0","D","A","C","B"];
} else {
alphabet = ["0","A","B","C","D"];
}
try {
// var i = 1 to omit first artboard. To include first artboard change var to 0.
for ( var i = 1; i < doc.artboards.length; i++ ) {
doc.artboards.setActiveArtboardIndex(i);
// doc.exportFile( File ( file.parent + "/" + file.name.substr(0,11) + alphabet[i] + ".jpeg" ), ExportType.JPEG, jpegOpts );
doc.exportFile( File ( foOut + "/" + file.name.substr(0,11) + alphabet[i] + ".jpeg" ), ExportType.JPEG, jpegOpts );
}
}
catch(err) {
data.ok = false;
data.message = "⚠ " + file.name + " couldn't be exported because: " + err.message;
}
doc.close ( SaveOptions.DONOTSAVECHANGES );
return data;
}
function getjpegOpts() {
var opts = new ExportOptionsJPEG();
var scale = 450.0;
opts.antiAliasing = true;
opts.transparency = false;
opts.artBoardClipping = true;
opts.qualitySetting = 100;
opts.horizontalScale = scale;
opts.verticalScale = scale;
return opts;
}
function log(msg) {
var f = File ( Folder.desktop+"/batchExport.txt" );
f.open('a');
f.writeln ( msg );
f.close();
}
function main() {
var dept;
if ( !fo ) return; {
files = getFiles ( fo, ["ai"], true );
n = files.length;
}
if ( !n ) {
alert("No .ai files found!");
return;
}
jpegOpts = getjpegOpts();
uip = app.userInteractionLevel;
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
while (n--) {
result = exportFile ( files[n], jpegOpts );
!result.ok && errors.push ( result.message );
}
app.userInteractionLevel = uip;
errors.length && log ( errors.join("\r") );
errors.length ? alert( "Errors occured. Check log on desktop." ) : alert( "All JPEGs exported successfully." );
}
main();