Skip to content

Instantly share code, notes, and snippets.

@joshualyon
Forked from tlinkner/Output Android Icons.jsx
Last active May 30, 2019 15:47
Show Gist options
  • Save joshualyon/dd89a230be4348e86a62c9e19c1d7d72 to your computer and use it in GitHub Desktop.
Save joshualyon/dd89a230be4348e86a62c9e19c1d7d72 to your computer and use it in GitHub Desktop.
Photoshop script to output Android icons (with XXXHDPI, XXHDPI, XHDPI, HDPI, MDPI support)
// Output Android Icons.jsx
// 2012 Todd Linkner
// License: none (public domain)
// v1.0 - base file by Todd Linkner
// v1.1 - added support for XXHDPI, XXXHDPI and added PNG to the file selector
//
// This script is for Photoshop CS6. It outputs Android icons of the
// following sizes from a source PSD at least 512px x 512px
//
// store:
// Icon.png (512px x 512px)
//
// xxxhdpi icon:
// Icon.png (192px x 192px)
//
// xxhdpi icon:
// Icon.png (144px x 144px)
//
// xhdpi:
// Icon.png (96px x 96px)
//
// hdpi:
// Icon.png (72px x 72px)
//
// mdpi:
// Icon.png (48px x 48px)
//
// ldpi:
// Icon.png (36px x 36px)
/*
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/OutputAndroidIcons/MenuAlt=Output Android Icons</name>
<category>mobile</category>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
*/
// bring Photoshop into focus
#target photoshop
main();
function main() {
var cleanup = confirm("This script outputs Android store, XXXHDPI, XXHDPI, XHDPI,"
+ "HDPI, MDPI, and LDPI icons from a source PSD or PNG at least 512px x "
+ "512px\r\r"
+ "Do you want to delete your original files when "
+ "complete?");
// Ask user for input folder
var inputFile = File.openDialog("Select a PSD or PNG file at least 512px x 512px","PSD File:*.psd,PNG File:*.png");
if (inputFile == null) throw "No file selected. Exting script.";
// Open file
open(inputFile);
var docRef = app.activeDocument;
// Make output folders
var dirstore = Folder(app.activeDocument.path+"/store");
if(!dirstore.exists) dirstore.create();
var dirxxxhdpi = Folder(app.activeDocument.path+"/drawable-xxxhdpi");
if(!dirxxxhdpi.exists) dirxxxhdpi.create();
var dirxxhdpi = Folder(app.activeDocument.path+"/drawable-xxhdpi");
if(!dirxxhdpi.exists) dirxxhdpi.create();
var dirxhdpi = Folder(app.activeDocument.path+"/drawable-xhdpi");
if(!dirxhdpi.exists) dirxhdpi.create();
var dirhdpi = Folder(app.activeDocument.path+"/drawable-hdpi");
if(!dirhdpi.exists) dirhdpi.create();
var dirmdpi = Folder(app.activeDocument.path+"/drawable-mdpi");
if(!dirmdpi.exists) dirmdpi.create();
var dirldpi = Folder(app.activeDocument.path+"/drawable-ldpi");
if(!dirldpi.exists) dirldpi.create();
// Set ruler untis to pixels
app.preferences.typeUnits = TypeUnits.PIXELS
// Store icon:
resize(dirstore,512);
// XXXHDPI icon:
resize(dirxxxhdpi,192);
// XXHDPI icon:
resize(dirxxhdpi,144);
// XHDPI icon:
resize(dirxhdpi,96);
// HDPI icon:
resize(dirhdpi,72);
// MDPI icon:
resize(dirmdpi,48);
// LDPI icon:
resize(dirldpi,36);
// Clean up
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
// Delete the original
if (cleanup) inputFile.remove();
alert("Done!");
}
function resize(dir,size) {
// Setup file name
var fname = app.activeDocument.name.replace(/\s+/g, '_').replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();
// Set export options
var opts, file;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
opts.transparency = true;
opts.interlaced = 0;
opts.includeProfile = false;
opts.optimized = true;
// Duplicate, resize and export
var tempfile = app.activeDocument.duplicate();
tempfile.resizeImage(size+"px",size+"px");
file = new File(dir+"/"+fname);
tempfile.exportDocument(file, ExportType.SAVEFORWEB, opts);
tempfile.close(SaveOptions.DONOTSAVECHANGES);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment