Skip to content

Instantly share code, notes, and snippets.

@nomisum
Forked from hhsaez/SaveiOSAndroid.jsx
Last active January 26, 2016 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomisum/e4f172fa07cf767b5177 to your computer and use it in GitHub Desktop.
Save nomisum/e4f172fa07cf767b5177 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)
* Remixer 3: @nomisum (mixing some Matthew Ericson in)
**/
// script takes @2x as basis. edit the 'saveToRes' lines for other scalings
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, svgSaveOpts;
// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with AI files you want to export for iOS + Android. Export will be in /_export of same dir.', '~' );
// If a valid folder is selected
if ( sourceFolder != null )
{
files = new Array();
// Get all files matching the pattern
files = sourceFolder.getFiles("*.ai");
if ( files.length > 0 )
{
// Get the destination to save the files - fixed same dir is faster than choosing here!
destFolder = sourceFolder;
for ( i = 0; i < files.length; i++ )
{
sourceDoc = app.open(files[i]); // returns the document object
var document = app.activeDocument;
if (document && destFolder) {
var documentName = document.name.replace(".ai","");
saveToRes(50, documentName, "", "iOS", true);
saveToRes(100, documentName, "@2x", "iOS", true);
saveToRes(150, documentName, "@3x", "iOS", true);
saveToRes(50, documentName, "", "drawable-mdpi", true);
saveToRes(75, documentName, "", "drawable-hdpi", true);
saveToRes(100, documentName, "", "drawable-xhdpi", true);
saveToRes(150, documentName, "", "drawable-xxhdpi", true);
saveToRes(200, documentName, "", "drawable-xxxhdpi", true);
}
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
}
alert( 'Files are saved as PNG in ' + destFolder );
}
else
{
alert( 'No Files found, so sorry bud :(');
}
}
/**
* 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(destFolder.absoluteURI + "/_export/" + 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;
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