-
Notifications
You must be signed in to change notification settings - Fork 5
/
BestPhotoshopToAndroidExporterScript.jsx
318 lines (275 loc) · 7.6 KB
/
BestPhotoshopToAndroidExporterScript.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
*** Author: Mohammad Yaghobi (https://github.com/myaghobi)
*/
var targetImageTypes = [
{
name: "Launcher icons(standard icons - target mdpi width=48px)",
mdpi: 48
},
{
name: "Action bar, Dialog & Tab icons(target mdpi width=32px)",
mdpi: 32
},
{
name: "Small Contextual Icons(target mdpi width=16px)",
mdpi: 16
},
{
name: "Notification icons(target mdpi width=24px)",
mdpi: 24
},
{
name: "Custom size(target mdpi width=entered custom number)",
mdpi: 2
},
{
name: "Free size drawable(target mdpi width=free)",
mdpi: 1
}
];
var targetSuffixs = [
{
name: "ldpi",
multiplayer: 0.75
},
{
name: "mdpi",
multiplayer: 1
},
{
name: "hdpi",
multiplayer: 1.5
},
{
name: "xhdpi",
multiplayer: 2
},
{
name: "xxhdpi",
multiplayer: 3
},
{
name: "xxxhdpi",
multiplayer: 4
}
];
var targetAddress = [
{
name: "Mipmap",
address: "mipmap-"
},
{
name: "Drawable",
address: "drawable-"
}
];
var document = app.activeDocument;
var width = document.width;
var targetFolder;
var selectedImageTypes = {};
var selectedSuffixs = {};
var selectedAddress = "";
var customSizeField;
// variables
var ru = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
try{
var rootDoc = app.activeDocument, activeLayer = rootDoc.activeLayer, activeLayer2, docName = rootDoc.name, docPath = rootDoc.path;
showOptionsPanel();
}
catch(error){
alert("First save your document.");
}
app.preferences.rulerUnits = ru;
function showOptionsPanel(){
var primeWin = new Window("dialog","Export options");
var veiwGroup = primeWin.add("group");
var targetImageTypesBox = createOneItemSelectionPanel0(targetImageTypes, veiwGroup);
var targetAddressBox = createOneItemSelectionPanel2(targetAddress, veiwGroup);
var targetSuffixsBox = createMultiSelectionPanel1(targetSuffixs, veiwGroup);
var btnGroup = primeWin.add("group");
var ok_ = btnGroup.add("button", undefined, "Export");
var cancel_ = btnGroup.add("button", undefined, "Cancel");
ok_.onClick = function() {
var validIntFlag = true;
var iconType=0;
for (var key in selectedImageTypes) {
if (selectedImageTypes.hasOwnProperty(key)) {
iconType = selectedImageTypes[key];
}
}
if (iconType==0 || (iconType.mdpi==2 && !isInteger(customSizeField.text))) {
validIntFlag =false;
alert("Please enter valid Integer number into 'Custom Size'");
return ;
}
targetFolder = Folder.selectDialog("Select target folder to export");
if (!targetFolder)
alert("Selected target folder is not valid!")
if (validIntFlag && targetFolder) {
for (var key in selectedSuffixs) {
if (selectedSuffixs.hasOwnProperty(key)) {
var item = selectedSuffixs[key];
makeOutPut(item.multiplayer, iconType.mdpi, item.name, selectedAddress);
}
}
this.parent.parent.close();
}
};
cancel_.onClick = function () {
this.parent.parent.close();
};
primeWin.show();
}
function makeOutPut(multiplayer, mdpi, suffix, resAddress) {
var calcWidth = app.activeDocument.width,
calcHeight = app.activeDocument.height;
duplicateFile(calcWidth, calcHeight, multiplayer);
var tempDoc = app.activeDocument;
resizeDoc(calcWidth, mdpi, multiplayer);
var tempDocName = tempDoc.name.replace(/\.[^\.]+$/, ''), docFolder = Folder(targetFolder.fsName + "/" + resAddress + suffix);
if(!docFolder.exists) {
docFolder.create();
}
var saveFile = File(docFolder + "/" + tempDocName + ".png");
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.PNG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = 100;
sfwOptions.PNG8 = false;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function createOneItemSelectionPanel0(array, parent) {
var panel = parent.add("panel", undefined, "Target type");
panel.alignment = "top";
panel.alignChildren = "left";
for(var i = 0; i < array.length; i++) {
if (i==4) {
var cbr=panel.add("panel", [0, 0, 300, 1], "");
}
var cb = panel.add("radiobutton", undefined, "\u00A0" + array[i].name);
cb.item = array[i];
if (i==0) {
cb.value = true;
selectedImageTypes[cb.item.name] = cb.item;
}
if (i==4)
cb.onClick = function() {
customSizeField.enabled = true;
if(this.value) {
selectedImageTypes[this.item.name] = this.item;
} else {
delete selectedImageTypes[this.item.name];
}
};
else
cb.onClick = function() {
customSizeField.enabled = false;
if(this.value) {
selectedImageTypes[this.item.name] = this.item;
} else {
delete selectedImageTypes[this.item.name];
}
};
}
};
function createMultiSelectionPanel1(array, parent) {
var panel = parent.add("panel", undefined, "Icon Density");
panel.alignChildren = "left";
for(var i = 0; i < array.length; i++) {
var cb = panel.add("checkbox", undefined, "\u00A0" + array[i].name);
cb.item = array[i];
if (i>0 && i<6) {
cb.value = true;
selectedSuffixs[cb.item.name] = cb.item;
}
cb.onClick = function() {
if(this.value) {
selectedSuffixs[this.item.name] = this.item;
} else {
delete selectedSuffixs[this.item.name];
}
};
}
};
function createOneItemSelectionPanel2(array, parent) {
var group = parent.add("group", undefined, "");
group.alignment = "top";
group.orientation = "column";
panel = group.add("panel", undefined, "Target path");
panel.alignment = "top";
panel.alignChildren = "left";
for(var i = 0; i < array.length; i++) {
var cb = panel.add("radiobutton", undefined, "\u00A0" + array[i].name);
cb.item = array[i];
if (i==1) {
cb.itemId = 1;
cb.value = true;
selectedAddress = array[1].address;
cb.onClick = function() {
selectedAddress = targetAddress[1].address;
};
} else {
cb.itemId = 0;
selectedAddress = array[0].address;
cb.onClick = function() {
selectedAddress = targetAddress[0].address;
};
}
}
panel = group.add("panel", undefined, "Custom Size");
customSizeField = panel.add("edittext", [0, 0, 69, 20], "48");
customSizeField.enabled=false;
customSizeField.name="sizeField";
customSizeField.text = 48;
};
function resizeDoc(width, mdpi, multiplayer) {
var newWidth = 1;
if (mdpi>2) {
var imageBaseRateWidth = mdpi/width;
newWidth = multiplayer*mdpi;
} else if (mdpi==1) {
newWidth = multiplayer*width;
} else if (mdpi==2) {
newWidth = multiplayer*customSizeField.text;
}
resizeLayer(newWidth);
activeLayer2.merge();
}
// third party
function resizeLayer(newWidth) {
var idImgS = charIDToTypeID( "ImgS" );
var desc2 = new ActionDescriptor();
var idWdth = charIDToTypeID( "Wdth" );
var idPxl = charIDToTypeID( "#Pxl" );
desc2.putUnitDouble( idWdth, idPxl, newWidth);
var idscaleStyles = stringIDToTypeID( "scaleStyles" );
desc2.putBoolean( idscaleStyles, true );
var idCnsP = charIDToTypeID( "CnsP" );
desc2.putBoolean( idCnsP, true );
var idIntr = charIDToTypeID( "Intr" );
var idIntp = charIDToTypeID( "Intp" );
var idBcbc = charIDToTypeID( "Bcbc" );
desc2.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc2, DialogModes.NO );
}
function duplicateFile(width, height, multiplayer) {
var fileName = activeLayer.name.replace(/\.[^\.]+$/, '');
var document = app.documents.add(width, height, rootDoc.resolution, fileName, NewDocumentMode.RGB,DocumentFill.TRANSPARENT);
app.activeDocument = rootDoc;
activeLayer.duplicate(document, ElementPlacement.INSIDE);
app.activeDocument = document;
activeLayer2 = document.activeLayer;
}
function isInteger(str) {
var status = true
for (var i=0,len=str.length;i<len;i++){
if((str.charCodeAt(i)<48 ) || (str.charCodeAt(i)>57)){
return false;
}
};
return true;
};