forked from UncorkedStudios/export-to-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
us-android-export.jsx
162 lines (132 loc) · 4.52 KB
/
us-android-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
158
159
160
161
162
/*!
* Android Assets for Photoshop
* =============================
*
* Version: 0.0.5
* Author: Gaston Figueroa (Uncorked Studios)
* Site: uncorkedstudios.com
* Licensed under the MIT license
*/
// Photoshop variables
var docRef = app.activeDocument,
activeLayer = docRef.activeLayer,
activeLayer2,
newWidth,
newHeight,
docName = docRef.name;
// Run main function
init();
// The other functions
function init() {
if(!isDocumentNew()) {
saveFunc('xxhdpi');
saveFunc('xhdpi');
saveFunc('hdpi');
saveFunc('mdpi');
saveFunc('ldpi');
} else {
alert("Please save your document before running this script.");
}
}
// Test if the document is new (unsaved)
// http://2.adobe-photoshop-scripting.overzone.net/determine-if-file-has-never-been-saved-in-javascript-t264.html
function isDocumentNew(doc){
// assumes doc is the activeDocument
cTID = function(s) { return app.charIDToTypeID(s); }
var ref = new ActionReference();
ref.putEnumerated( cTID("Dcmn"),
cTID("Ordn"),
cTID("Trgt") ); //activeDoc
var desc = executeActionGet(ref);
var rc = true;
if (desc.hasKey(cTID("FilR"))) { //FileReference
var path = desc.getPath(cTID("FilR"));
if (path) {
rc = (path.absoluteURI.length == 0);
}
}
return rc;
};
function resizeDoc(document, scale) {
var calcWidth = activeLayer.bounds[2] - activeLayer.bounds[0], // Get layer's width
calcHeight = activeLayer.bounds[3] - activeLayer.bounds[1]; // Get layer's height
// newWidth, newHeight;
if(scale === 'xxhdpi') {
newHeight = calcHeight;
newWidth = calcWidth;
} else if(scale === 'xhdpi') {
newHeight = Math.floor(calcHeight / 3 * 2);
newWidth = Math.floor(calcWidth / 3 * 2);
} else if(scale === 'hdpi') {
newHeight = Math.floor(calcHeight / 2);
newWidth = Math.floor(calcWidth / 2);
} else if(scale === 'mdpi') {
newHeight = Math.floor(calcHeight / 3);
newWidth = Math.floor(calcWidth / 3);
} else if(scale === 'ldpi') {
newHeight = Math.floor(calcHeight / 3 * .75);
newWidth = Math.floor(calcWidth / 3 * .75);
}
// Resize temp document using Bicubic interpolation
resizeLayer(newWidth);
// Merge all layers inside the temp document
activeLayer2.merge();
}
// document.resizeImage doesn't seem to support scalestyles so we're using this workaround from http://ps-scripts.com/bb/viewtopic.php?p=14359
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 dupToNewFile() {
var fileName = activeLayer.name.replace(/\.[^\.]+$/, ''),
calcWidth = Math.ceil(activeLayer.bounds[2] - activeLayer.bounds[0]),
calcHeight = Math.ceil(activeLayer.bounds[3] - activeLayer.bounds[1]),
docResolution = docRef.resolution,
document = app.documents.add(calcWidth, calcHeight, docResolution, fileName, NewDocumentMode.RGB,
DocumentFill.TRANSPARENT);
app.activeDocument = docRef;
// Duplicated selection to a temp document
activeLayer.duplicate(document, ElementPlacement.INSIDE);
// Set focus on temp document
app.activeDocument = document;
// Assign a variable to the layer we pasted inside the temp document
activeLayer2 = document.activeLayer;
// Center the layer
activeLayer2.translate(-activeLayer2.bounds[0],-activeLayer2.bounds[1]);
}
function saveFunc(dpi) {
dupToNewFile();
var docRef2 = app.activeDocument;
resizeDoc(docRef2, dpi);
var Name = docRef2.name.replace(/\.[^\.]+$/, ''),
Ext = decodeURI(docRef2.name).replace(/^.*\./,''),
Path = docRef.path,
folder = Folder(Path + '/' + docName + '-assets/' + 'drawable-' + dpi);
if(!folder.exists) {
folder.create();
}
var saveFile = File(folder + "/" + Name + ".png");
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.PNG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = 100;
sfwOptions.PNG8 = false;
// Export the layer as a PNG
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
// Close the document without saving
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}