Created
December 1, 2017 21:17
-
-
Save arvkmr/3a571e28c270ac36fe97e8134cf9e1dc to your computer and use it in GitHub Desktop.
Photoshop script to export icons for Android apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Updated version of script by Todd Linkner | |
// This script is for Photoshop CS6. It outputs Android icons of the | |
// xxxhdpi - ldpi from a source PSD at least 512px x 512px | |
/* | |
// 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 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(); | |
fname = fname.substr(0, fname.lastIndexOf('.')) +'.png'; | |
// 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