-
Notifications
You must be signed in to change notification settings - Fork 8
/
icon-exporter.jsx
198 lines (165 loc) · 5.39 KB
/
icon-exporter.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
#target Illustrator
//https://developer.apple.com/library/content/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/ImageSetType.html#//apple_ref/doc/uid/TP40015170-CH25-SW2
//https://developer.apple.com/library/content/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/AppIconType.html#//apple_ref/doc/uid/TP40015170-CH22-SW1
/*
includes
*/
//json
#include "json2.js"
//Illustrator's interpreter is not support map function
Array.prototype.map = function (callback) {
var obj = Object(this);
if (obj.length === 0) return null;
if (typeof(callback) === 'undefined') return null;
for (var i = 0, o; o = obj[i]; i++) {
obj[i] = callback(o);
}
return obj;
};
//Illustrator's interpreter is not support indexOf function. what??? why????
Array.prototype.indexOf = Array.prototype.indexOf || function(value, start) {
for (var i = 0, length = this.length; i < length; i++) {
if (this[i] == value) {
return i;
}
}
return -1;
}
if (app.documents.length > 0) {
main();
}
else alert('Cancelled by user');
/*
parser
*/
function parseFromIcon(icon){
var ALLOWED_IDIOMS = ['iphone','ipad','universal','ios-marketing'];
/*
appLauncher
An image shown app launcher on watchOS
companionSettings
An image for the Apple Watch Settings app
ios-marketing
An image for the App Store icon
iphone
The image is for iPhone devices.
ipad
The image is for iPad devices.
mac
The image is for Mac computers.
notificationCenter
An image for the notification center on watchOS.
quickLook
An image used for a long look on watchOS.
tv
The image is for Apple TV.
universal
The image works on any device and platform.
watch
The image is for the Apple Watch devices.
watch-marketing
An image for the App Store icon.
Tag not included
Same as specifying universal.
*/
var ALLOWED_PLATFORM = ['ios','macos','tvos','watchos'];
var filenameStr = icon.split("@");
var iconName = filenameStr[0].split("_");
if(iconName.length!=2){
alert("Invalid icon name. See Readme for more information.", iconName);
return null;
}
var sizeArr = iconName[1].split("x");
if(sizeArr.length>2){
alert("Invalid size. See Readme for more information.", sizeArr.join(''));
return null;
}
if(sizeArr.length==1){
sizeArr = [sizeArr[0],sizeArr[0]];
}
var scale = filenameStr.length == 2 ? filenameStr[1] : '1x';
var scaleFloat = parseFloat(scale.replace('x',''));
var idiom = ALLOWED_IDIOMS.indexOf(iconName[0])>-1 ? iconName[0] : "universal";
var platform = (idiom=="universal" /* || idiom=="ios-marketing"*/) && ALLOWED_PLATFORM.indexOf(PLATFORM)>-1 ? PLATFORM : null
return {
"idiom": idiom,
"platform": platform,
"size": sizeArr.join('x'),
"scaledSizeArr": [sizeArr[0] * scaleFloat, sizeArr[1] * scaleFloat],
"filename": icon ? icon+".png" : null,
"scale": scale,
"scaleFloat": scaleFloat
};
}
function makeContentJs(icons){
return {
images: icons.map(function(icon){
var iconDict = parseFromIcon(icon);
var content = {};
content["idiom"] = iconDict.idiom;
content["filename"] = iconDict.filename;
content["scale"] = iconDict.scale;
content["size"] = iconDict.size;
if(iconDict.platform){
content["platform"] = iconDict.platform;
}
return content;
}),
info: {
version: 1,
author: "xcode"
}
}
}
function main() {
var document = app.activeDocument;
var afile = document.fullName;
var filename = afile.name.split('.')[0];
var folder = afile.parent.selectDlg("Export as CSS Layers (images only)...");
if(folder != null)
{
var activeABidx = document.artboards.getActiveArtboardIndex();
var activeAB = document.artboards[activeABidx]; // get active AB
var abBounds = activeAB.artboardRect;// left, top, right, bottom
var options = new ExportOptionsPNG24();
options.antiAliasing = true;
options.transparency = true;
options.artBoardClipping = true;
/*
export image files
*/
for(var i = 0; i < ICONS.length; i++){
var iconDict = parseFromIcon(ICONS[i]);
var file = new File(folder.fsName + '/' + iconDict.filename);
options.horizontalScale = 100 * (iconDict.scaledSizeArr[0] / document.width);
options.verticalScale = 100 * (iconDict.scaledSizeArr[1] / document.height);
document.exportFile(file,ExportType.PNG24,options);
}
/*
export file
*/
var contentsJson = new File(folder.fsName + "/Contents.json");
contentsJson.open("w");
contentsJson.write(JSON.stringify(makeContentJs(ICONS), null, 2));
contentsJson.close();
}
}
/*
ref to format
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html#//apple_ref/doc/uid/TP40006556-CH27-SW1
{
"idiom": "iphone",
"size": "29x29",
"filename": "[email protected]",
"scale": "2x"
},
{
"idiom" : "universal",
"scale" : "2x",
"filename" : "[email protected]"
},
{
"idiom" : "universal",
"scale" : "3x"
}
*/