-
Notifications
You must be signed in to change notification settings - Fork 14
/
icns-export.jsx
157 lines (135 loc) · 4.49 KB
/
icns-export.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
/*!
* The MIT License (MIT)
*
* Copyright (c) 2014 Christian Hoffmeister
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
(function () {
var doc = null;
try {
doc = app.activeDocument;
} catch (ex) {
alert('You must have an active document');
return;
}
var ab = doc.artboards[0];
var width = ab.artboardRect[2] - ab.artboardRect[0];
var height = ab.artboardRect[1] - ab.artboardRect[3];
if (doc.path == '' || !doc.saved) {
alert('You must save your document before exporting it');
return;
}
if (width != 1024 || height != 1024) {
alert('Your document has size ' + width + 'x' + height + ' pixels, but must have size 1024x1024 pixels');
return;
}
var file = getTargetFile(doc, '.icns');
if (!file) {
alert('Canceled export');
return;
}
var formats = [
// Quick fix for https://github.com/choffmeister/adobe-illustrator-icnsexport/issues/7
// { type: 'icp4', size: 16 },
// { type: 'icp5', size: 32 },
// { type: 'icp6', size: 64 },
{ type: 'ic07', size: 128 },
{ type: 'ic08', size: 256 },
{ type: 'ic09', size: 512 },
{ type: 'ic10', size: 1024 },
{ type: 'ic11', size: 32 },
{ type: 'ic12', size: 64 },
{ type: 'ic13', size: 256 },
{ type: 'ic14', size: 512 }
]
var totalLength = 0;
for (var i = 0; i < formats.length; i++) {
var format = formats[i];
var filePng = new File(Folder.temp + '/icns-export-temp.png');
exportAsPng(filePng, format.size);
format.png = readFile(filePng);
totalLength += format.png.length;
}
doc.save();
openFile(file, 'w');
writeString(file, 'icns');
writeInt(file, 8 + 8 * formats.length + totalLength);
for (var i = 0; i < formats.length; i++) {
var format = formats[i];
writeString(file, format.type);
writeInt(file, format.png.length + 8);
writeString(file, format.png);
}
closeFile(file);
alert('Exported to ' + decodeURIComponent(file.toString()));
function writeInt(file, i) {
var a = String.fromCharCode((i >> 24) & 255);
var b = String.fromCharCode((i >> 16) & 255);
var c = String.fromCharCode((i >> 8) & 255);
var d = String.fromCharCode((i >> 0) & 255);
return file.write(a + b + c + d);
}
function writeString(file, str) {
file.write(str);
}
function readFile(file) {
openFile(file, 'r');
var result = file.read();
closeFile(file);
file.remove();
return result;
}
function openFile(file, mode) {
file.encoding = 'BINARY';
if (!file.open(mode)) throw new Error('Could not read ' + file);
}
function closeFile(file) {
file.close();
}
function exportAsPng(file, size) {
var expType = ExportType.PNG24;
var exp = new ExportOptionsPNG24();
exp.antiAliasing = true;
exp.transparency = this.transparency;
exp.artBoardClipping = true;
exp.horizontalScale = size / 1024.0 * 100.0;
exp.verticalScale = size / 1024.0 * 100.0;
exp.transparency = true;
doc.exportFile(file, expType, exp);
}
function getTargetFile(doc, ext) {
var destFolder = Folder.selectDialog('Select folder for ' + ext + ' file.', '~');
if (destFolder) {
var newName = '';
// if name has no dot (and hence no extension),
// just append the extension
if (doc.name.indexOf('.') < 0) {
newName = doc.name + ext;
} else {
var dot = doc.name.lastIndexOf('.');
newName += doc.name.substring(0, dot);
newName += ext;
}
// Create the file object to save to
return new File(destFolder + '/' + newName);
} else {
return null;
}
}
})();