Skip to content

Instantly share code, notes, and snippets.

@hhsaez
Last active January 26, 2016 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hhsaez/cdb1cf3cb1e86c03bb48 to your computer and use it in GitHub Desktop.
Save hhsaez/cdb1cf3cb1e86c03bb48 to your computer and use it in GitHub Desktop.
Adobe Illustrator script for exporting icons to iOS and Android in all supported sizes.
/**
* Remixer 1: @herkulano (http://www.herkulano.com)
* Remixer 2: @hotappsfactory (http://www.hotappsfactory.com)
* Thanks to: Niels Bosma (niels.bosma@motorola.com)
*
* Install at <Illustrator_Path>/Presets/en_US/Scripts
*/
var folder = Folder.selectDialog();
var document = app.activeDocument;
if (document && folder) {
var documentName = document.name.replace(".ai","");
var fileName = prompt("Prefix", "") || documentName;
saveToRes(100, fileName, "", "ios", false);
saveToRes(200, fileName, "@2x", "ios", false);
saveToRes(100, fileName, "", "drawable-mdpi", true);
saveToRes(150, fileName, "", "drawable-hdpi", true);
saveToRes(200, fileName, "", "drawable-xhdpi", true);
saveToRes(300, fileName, "", "drawable-xxhdpi", true);
saveToRes(400, fileName, "", "drawable-xxxhdpi", true);
}
/**
* Scale and export file suffixed by densitySuffix, in a specific folder named folderName
*/
function saveToRes(scaleTo, preffix, densitySuffix, folderName, lowerCase) {
var i, ab, file, options;
var myFolder = new Folder(folder.absoluteURI + "/" + folderName);
if(!myFolder.exists) myFolder.create();
for (i = document.artboards.length - 1; i >= 0; i--) {
document.artboards.setActiveArtboardIndex(i);
ab = document.artboards[i];
var fileName = preffix;// + ab.name + suffix;
if(lowerCase){
var fileNameLowerCase = "";
for (var j = 0; j < fileName.length; j++) {
if(isUpperCase(fileName.charAt(j))){
if(j > 0){
fileNameLowerCase += "_";
}
fileNameLowerCase += fileName.charAt(j).toLowerCase();
}
else{
fileNameLowerCase += fileName.charAt(j);
}
}
fileName = fileNameLowerCase;
}
file = new File(myFolder.fsName + "/" + fileName + densitySuffix + ".png");
options = new ExportOptionsPNG24();
options.antiAliasing = true;
options.transparency = true;
options.artBoardClipping = true;
options.verticalScale = scaleTo;
options.horizontalScale = scaleTo;
document.exportFile(file, ExportType.PNG24, options);
}
}
function isUpperCase(myString) {
return (myString == myString.toUpperCase());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment