Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An adobe illustrator script to export layers into android resources
/**
* Adobe Illustrator Script that exports each layer into PNG files according to its layer name,
* as well as making copies for various screen sizes.
*
* (1)The illustrator document should has dimensions in xxhdpi.
* One xxhdpi screen is nexus5 (480dpi) with dimensions 1920x1080px
* Launcher icon at baseline 48x48dp is 48x48px for mdpi
* so the lanucher_icon layer should be at 144x144px inside the illustrator document to be exported.
*
* (2)Modify the destination android project in line33
*
* (3)you can exclude a layer from being exported by prefixing the name with "-"
*
* (4)Run this from illustrator -> File-> Scripts-> Other scripts (Ctrl +F12)
*/
var doc = app.activeDocument;
// prepare layers
for(var i=0; i<doc.layers.length; i++) {
doc.layers[i].visible = false;
}
// go through each layers
for(var i=0; i<doc.layers.length; i++) {
if(doc.layers[i].name[0] != '-'){ //check for exclusion
doc.layers[i].visible = true;
if (i>0) doc.layers[i-1].visible = false;
var fpath = doc.path; // save to document's folder
var basepath = "C:/Users/kaka/Documents/GitHub/cocross_android/res/";
fpath = File(basepath + 'drawable-mdpi/'+doc.layers[i].name+'.png');
savePNG( fpath , 33.33);
fpath.changePath( basepath + 'drawable-hdpi/'+doc.layers[i].name+'.png');
savePNG( fpath , 50);
fpath.changePath(basepath +'drawable-xhdpi/'+doc.layers[i].name+'.png');
savePNG( fpath , 66.67);
fpath.changePath( basepath + 'drawable-xxhdpi/'+doc.layers[i].name+'.png');
savePNG( fpath , 100);
doc.layers[i].visible = false;
}
}
// return visibility
for(var i=0; i<doc.layers.length; i++) {
doc.layers[i].visible = true;
}
/**
* Save PNG file
* @param file File object
*/
function savePNG( file, scale ) {
// export SAVE-FOR-WEB options
var exp = new ExportOptionsPNG24();
exp.transparency = true;
exp.verticalScale = scale;
exp.horizontalScale = scale;
// export as SAVE-FOR-WEB
doc.exportFile( file, ExportType.PNG24, exp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment