This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
us-android-export.jsx
174 lines (135 loc) · 4.54 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
163
164
165
166
167
168
169
170
171
172
173
174
/*!
* Android Assets for Photoshop
* =============================
*
* Version: 1.0.0
* Author: Gaston Figueroa (Uncorked Studios)
* Site: uncorkedstudios.com
* Licensed under the MIT license
*/
// Photoshop variables
var docRef = app.activeDocument,
activeLayer = docRef.activeLayer,
activeLayer2,
docName = docRef.name,
docPath = docRef.path,
resolutionsObj = {
xxxhdpi : {
density : 1
},
xxhdpi : {
density : 0.75
},
xhdpi : {
density : 0.5
},
hdpi : {
density : 0.375
},
mdpi : {
density : 0.25
}
};
// Initialize
init();
function init() {
// save current ruler unit settings, so we can restore it
var ru = app.preferences.rulerUnits;
// set ruler units to pixel to ensure scaling works as expected
app.preferences.rulerUnits = Units.PIXELS;
if(!isDocumentNew()) {
for(resolution in resolutionsObj) {
saveFunc(resolution);
}
} else {
alert("Please save your document before running this script.");
}
// restore old ruler unit settings
app.preferences.rulerUnits = ru;
}
// 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, resolution) {
var calcWidth = activeLayer.bounds[2] - activeLayer.bounds[0], // Get layer's width
calcHeight = activeLayer.bounds[3] - activeLayer.bounds[1]; // Get layer's height
var newWidth = Math.floor(calcWidth * resolutionsObj[resolution].density);
var newHeight = Math.floor(calcHeight * resolutionsObj[resolution].density);
// 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(resolution) {
dupToNewFile();
var tempDoc = app.activeDocument;
resizeDoc(tempDoc, resolution);
var tempDocName = tempDoc.name.replace(/\.[^\.]+$/, ''),
docFolder = Folder(docPath + '/' + docName + '-assets/' + 'drawable-' + resolution);
if(!docFolder.exists) {
docFolder.create();
}
alert(docFolder);
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;
// Export the layer as a PNG
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
// Close the document without saving
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}