|
try { |
|
var xxhdpiImage = File.openDialog("Select xxxhdpi file.", "*.png", false); |
|
|
|
if (xxhdpiImage == null) { |
|
throw ""; |
|
} |
|
|
|
var doc = open(xxhdpiImage, OpenDocumentType.PNG); |
|
|
|
if (doc == null) { |
|
throw "Something is wrong with the file. Make sure it's a valid PNG file."; |
|
} |
|
|
|
var startState = doc.activeHistoryState; // save for undo |
|
var initialPrefs = app.preferences.rulerUnits; // will restore at end |
|
app.preferences.rulerUnits = Units.PIXELS; // use pixels |
|
|
|
// Folder selection dialog |
|
var destFolder = Folder.selectDialog("Choose an output folder"); |
|
|
|
if (destFolder == null) { |
|
throw ""; |
|
} |
|
|
|
// Save icons in PNG using Save for Web. |
|
var sfw = new ExportOptionsSaveForWeb(); |
|
sfw.format = SaveDocumentType.PNG; |
|
sfw.PNG8 = false; // use PNG-24 |
|
sfw.transparency = true; |
|
doc.info = null; // delete metadata |
|
|
|
var baseWidth = Math.floor(doc.width / 4); |
|
var baseHeight = Math.floor(doc.height / 4); |
|
var filename = doc.name; |
|
|
|
var images = [ |
|
{"folder": "xxxhdpi", "width": baseWidth * 4, "height": baseHeight * 4}, |
|
{"folder": "xxhdpi", "width": baseWidth * 3, "height": baseHeight * 3}, |
|
{"folder": "xhdpi", "width": baseWidth * 2, "height": baseHeight * 2}, |
|
{"folder": "hdpi", "width": Math.floor(baseWidth * 1.5), "height": Math.floor(baseHeight * 1.5)}, |
|
{"folder": "mdpi", "width": baseWidth, "height": baseHeight}, |
|
]; |
|
|
|
for (i = 0; i < images.length; i++) { |
|
var image = images[i]; |
|
doc.resizeImage(image.width, image.height, null, ResampleMethod.BICUBICSHARPER); |
|
|
|
var folder = new Folder(destFolder + "/" + image.folder); |
|
if (!folder.exists) { |
|
folder.create(); |
|
} |
|
|
|
doc.exportDocument(new File(destFolder + "/" + image.folder + "/" + filename), ExportType.SAVEFORWEB, sfw); |
|
doc.activeHistoryState = startState; // undo resize |
|
} |
|
|
|
alert("drawable created!"); |
|
|
|
} catch (exception) { |
|
// Show degbug message and then quit |
|
if ((exception != null) && (exception != "")) { |
|
alert(exception); |
|
} |
|
} finally { |
|
if (doc != null) { |
|
doc.close(SaveOptions.DONOTSAVECHANGES); |
|
} |
|
app.preferences.rulerUnits = initialPrefs; // restore prefs |
|
} |
|
|