Last active
October 18, 2024 10:19
-
-
Save tlinkner/3718022 to your computer and use it in GitHub Desktop.
Photoshop script to output Android XHDPI, HDPI, MDPI, and LDPI PNGs
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
// Output Android Assets.jsx | |
// 2012 Todd Linkner | |
// License: none (public domain) | |
// v1.0 | |
// | |
// This scrip is for Photoshop CS6. It outputs Android XHDPI, HDPI, MDPI, | |
// and LDPI PNG assets from HDPI source files. The resulting PNGs will be | |
// placed in sub-folders within your target folder. | |
/* | |
// BEGIN__HARVEST_EXCEPTION_ZSTRING | |
<javascriptresource> | |
<name>$$$/JavaScripts/OutputAndroidAssets/MenuAlt=Output Android Assets</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 XHDPI, HDPI, MDPI, " | |
+ "and LDPI PNG assets from HDPI source files.\r\r" | |
+ "Do you want to delete your original files when " | |
+ "complete?"); | |
// Ask user for input folder | |
var inputFolder = Folder.selectDialog("Select a folder to process"); | |
if (inputFolder == null) throw "No folder selected. Exting script."; | |
// get all files in the input folder | |
var fileList = inputFolder.getFiles("*.png"); | |
// Make output folders | |
var dirxhdpi = Folder(inputFolder+"/drawable-xhdpi"); | |
if(!dirxhdpi.exists) dirxhdpi.create(); | |
var dirhdpi = Folder(inputFolder+"/drawable-hdpi"); | |
if(!dirhdpi.exists) dirhdpi.create(); | |
var dirmdpi = Folder(inputFolder+"/drawable-mdpi"); | |
if(!dirmdpi.exists) dirmdpi.create(); | |
var dirldpi = Folder(inputFolder+"/drawable-ldpi"); | |
if(!dirldpi.exists) dirldpi.create(); | |
// Open each file in turn | |
for (var i = 0; i < fileList.length; i++) { | |
// Open file | |
open(fileList[i]); | |
// Make XHDPI | |
resize(dirxhdpi); | |
// Make HDPI | |
resize(dirhdpi,'75%'); | |
// Make MDPI | |
resize(dirmdpi,'50%'); | |
// Close and do not save | |
// Make LDPI | |
resize(dirldpi,'37.5%'); | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); | |
// Delete the original | |
if (cleanup) fileList[i].remove(); | |
} | |
alert("Done!"); | |
} | |
function resize(dir,percent) { | |
// 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(); | |
if (undefined != percent) tempfile.resizeImage(percent,percent); | |
file = new File(dir+"/"+fname); | |
tempfile.exportDocument(file, ExportType.SAVEFORWEB, opts); | |
tempfile.close(SaveOptions.DONOTSAVECHANGES); | |
} | |
I usually run scripts from the script menu in Photoshop so I never encountered that problem. I changed line 20 so it should work in ExtendScript as well. Thanks for finding the error.
Seems to work great, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi This script didnt work for me it just says Photoshop CS6 target unknown. So I tried changing it to Adobe Photoshop CS6 bit that didnt work :-( thoughts?