Skip to content

Instantly share code, notes, and snippets.

@joshfreemanIO
Last active November 23, 2021 06:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfreemanIO/9e13a6bc34257a910ba0 to your computer and use it in GitHub Desktop.
Save joshfreemanIO/9e13a6bc34257a910ba0 to your computer and use it in GitHub Desktop.
Given an Adobe Illustrator project, generate the images and Contents.json file for an iOS Application Icon
// Download http://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.js
// and save as json3.js in the same directory as this script
//
// Move the new images and Contents.json into
// Assets.xcassets/AppIcon.appiconset/
#include "json3.js"
var manifest = [
{
size: 29,
scale: 2,
idiom: "iphone"
},
{
size: 29,
scale: 3,
idiom: "iphone"
},
{
size: 40,
scale: 2,
idiom: "iphone"
},
{
size: 40,
scale: 3,
idiom: "iphone"
},
{
size: 60,
scale: 2,
idiom: "iphone"
},
{
size: 60,
scale: 3,
idiom: "iphone"
},
{
size: 29,
scale: 1,
idiom: "ipad"
},
{
size: 29,
scale: 2,
idiom: "ipad"
},
{
size: 40,
scale: 1,
idiom: "ipad"
},
{
size: 40,
scale: 2,
idiom: "ipad"
},
{
size: 76,
scale: 1,
idiom: "ipad"
},
{
size: 76,
scale: 2,
idiom: "ipad"
},
{
size: 83.5,
scale: 2,
idiom: "ipad"
},
];
createDirectory();
exportIcons();
writeContentsJSON();
alert("Your icons and Content.json has been saved to " + iconDirectory());
function exportIcons() {
for (var i = manifest.length - 1; i >= 0; i--) {
exportFileToPNG24(manifest[i].size, manifest[i].scale, manifest[i].idiom)
}
}
function exportFileToPNG24(iconSize, scale, idiom) {
var activeArtboard = app.activeDocument.artboards[app.activeDocument.artboards.getActiveArtboardIndex()];
var width = activeArtboard.artboardRect[2] - activeArtboard.artboardRect[0];
var computedScale = iconSize / width * 100 * scale;
var exportOptions = new ExportOptionsPNG24();
exportOptions.verticalScale = computedScale;
exportOptions.horizontalScale = computedScale;
var file = new File(iconDirectory() + "/Icon-" + iconSize + "@" + scale + "x.png");
app.activeDocument.exportFile(file, ExportType.PNG24, exportOptions);
}
function writeContentsJSON() {
var object = {
images: [],
info: {
version: 1,
author: "GrokInteractive"
}
}
for (var i = manifest.length - 1; i >= 0; i--) {
object.images.push({
"idiom" : manifest[i].idiom,
"size" : manifest[i].size + "x" + manifest[i].size,
"scale" : manifest[i].scale + "x",
"filename" : "Icon-" + manifest[i].size + "@" + manifest[i].scale + "x.png"
});
}
var file = new File(iconDirectory() + "/Contents.json")
file.open("e");
file.write(JSON.stringify(object, null, 2));
file.close();
}
function createDirectory() {
var newFolder = new Folder(iconDirectory())
newFolder.create()
}
function iconDirectory() {
return app.activeDocument.path + '/' + app.activeDocument.name.replace(/\..*$/, '')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment