Skip to content

Instantly share code, notes, and snippets.

@tlinkner
Last active July 24, 2018 16:54
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save tlinkner/3718022 to your computer and use it in GitHub Desktop.
Save tlinkner/3718022 to your computer and use it in GitHub Desktop.
Photoshop script to output Android XHDPI, HDPI, MDPI, and LDPI PNGs
// 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);
}
@petermarkellis
Copy link

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?

@tlinkner
Copy link
Author

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.

@meganvanwelie
Copy link

Seems to work great, thanks!

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