Skip to content

Instantly share code, notes, and snippets.

@tlinkner
Last active December 11, 2015 16:28
Show Gist options
  • Save tlinkner/4627802 to your computer and use it in GitHub Desktop.
Save tlinkner/4627802 to your computer and use it in GitHub Desktop.
This script is for Illustrator CS6. It outputs iOS standard and @2x assets from layers. It fits the artboard to the items on the layer, including items without fill and stroke.
// Output iOS Assets.jsx
// 2013 Todd Linkner
// License: none (public domain)
// v1.0
//
// This script is for Illustrator CS6. It outputs iOS standard and @2x
// assets from layers.
//
// Script concept and saveToRes function based on herkulano's
// Illustrator Script for Mobile: https://github.com/herkulano
//
// Install to /Applications/Adobe Illustrator CS6/en_US/Scripts/Mobile
// bring Illustrator into focus
#target illustrator
var folder = Folder.selectDialog();
var document = app.activeDocument;
if (document && folder) {
processLayers();
}
function processLayers() {
var i, n, // Counters
layer, // Source document
newItem, newLayer, newGroup, newDocument; // Destination document
for (i = document.layers.length - 1; i >= 0; i--) {
layer = document.layers[i];
if (!layer.locked) {
newDocument = app.documents.add(DocumentColorSpace.RGB);
newLayer = newDocument.layers.add();
newGroup = newDocument.groupItems.add()
for (var n = layer.pageItems.length-1; n >= 0; n--) {
// Copy the page items on the layer to a new document
newItem = layer.pageItems[n].duplicate(newLayer, ElementPlacement.PLACEATBEGINNING);
newItem.moveToBeginning(newGroup);
}
// Make sure they are on the arboard
newGroup.left = 0;
newGroup.top = 0;
newDocument.selectObjectsOnActiveArtboard();
// Fit the artboard to the layer contents (including un-filled items)
newDocument.fitArtboardToSelectedArt(
activeDocument.artboards.getActiveArtboardIndex()
);
newDocument.artboards[0].name = layer.name;
saveToRes(newDocument,100, "");
saveToRes(newDocument,200, "@2x");
newDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
alert("Done!");
}
function saveToRes(doc, scaleTo, densitySuffix) {
var i, ab,
file, options;
for (i = doc.artboards.length - 1; i >= 0; i--) {
doc.artboards.setActiveArtboardIndex(i);
ab = doc.artboards[i];
file = new File(folder.fsName + "/" + ab.name + densitySuffix + ".png");
options = new ExportOptionsPNG24();
options.antiAliasing = true;
options.transparency = true;
options.artBoardClipping = true;
options.verticalScale = scaleTo;
options.horizontalScale = scaleTo;
doc.exportFile(file, ExportType.PNG24, options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment