Skip to content

Instantly share code, notes, and snippets.

@stonetip
Last active October 22, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stonetip/cb9691aaf632654863248bf8d20227df to your computer and use it in GitHub Desktop.
Save stonetip/cb9691aaf632654863248bf8d20227df to your computer and use it in GitHub Desktop.
Adobe Illustrator script to generate PNG icons for apps.
/***
Adobe Illustrator script to generate PNG icons for apps.
Current for Xcode 9 and iOS 11 as far as icon sizes go, but set icon sizes in the pixelSizes array to get what you neeed.
Will generate images for each storyboard in a document, in the format {docName}{artboardName}_{pixelSize}, e.g.
"myDocAB1_120.png"
Tested with Adobe Illustrator CC 22.0.0 and Xcode 9
***/
function saveFile(destFolder, fileName, pixelSize) {
file = new File(destFolder.fsName + "/" + fileName + ".png");
// Scale the image based on the pixel size (assuming square, but maybe not, so pick larger value of width and height)
const scale = 100 * Math.max(pixelSize / document.width, pixelSize / document.height);
options = new ExportOptionsPNG24();
options.antiAliasing = true;
options.transparency = true;
options.artBoardClipping = true;
options.includeProfile = false;
options.verticalScale = scale;
options.horizontalScale = scale;
document.exportFile(file, ExportType.PNG24, options);
}
function createIcons(pixelSizes, srcFileName, folderName) {
const destinationFolder = new Folder(destFolder.absoluteURI + "/" + folderName);
if (!destinationFolder.exists) destinationFolder.create();
// Process each artboard
for (abIndex = 0; abIndex < document.artboards.length; abIndex++) {
document.artboards.setActiveArtboardIndex(abIndex);
var ab = document.artboards[abIndex];
// Define a file name for each size and create the icon file
for (sizeIndex = 0; sizeIndex < pixelSizes.length; sizeIndex++) {
var fileName = srcFileName + ab.name + "_" + pixelSizes[sizeIndex];
saveFile(destinationFolder, fileName, pixelSizes[sizeIndex]);
}
}
}
// Select the destination folder and switch to active document
var destFolder = Folder.selectDialog();
var document = app.activeDocument;
if (document && destFolder) {
// Remove the doc extension
var documentName = document.name.substring(0, document.name.lastIndexOf("."));
// Define sorted sizes for icons in pixels/points
var pixelSizes = [40, 60, 58, 87, 20, 29, 80, 76, 120, 152, 167, 180, 1024].sort(function (a, b) { return a - b; });
createIcons(pixelSizes, documentName, "appIconSet_" + documentName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment