Skip to content

Instantly share code, notes, and snippets.

@tlinkner
Last active February 19, 2021 14:15
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save tlinkner/3724588 to your computer and use it in GitHub Desktop.
Save tlinkner/3724588 to your computer and use it in GitHub Desktop.
Photoshop script to output Android icons
// Output Android Icons.jsx
// 2012 Todd Linkner
// License: none (public domain)
// v1.0
//
// 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)
//
// 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, SHDPI, HDPI, "
+ "MDPI, and LDPI icons from a source PSD 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 file at least 512px x 512px","PSD File:*.psd");
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 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);
// 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);
}
@Jambamatt
Copy link

I tried this in Photoshop 2015 and it would only output PSD's.
I could not INPUT a png to see if this helped, because the initial input dialog settings are set to PSD only.
I changed line 47 in 3 places to read:
var inputFile = File.openDialog("Select a PNG file at least 512px x 512px","PNG File:*.png");
and it worked - outputting png's.

@devcang
Copy link

devcang commented Oct 31, 2015

Thanks

var fname = app.activeDocument.name.replace(/\s+/g, '').replace(/([a-z\d])([A-Z])/g, '$1$2').toLowerCase();
fname = fname.substr(0, fname.lastIndexOf('.')) +'.png'; ////add this , will saving to a .png file name

@tapblaze
Copy link

Should update the script to handle xxhdpi and xxxhpdi sizes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment