Skip to content

Instantly share code, notes, and snippets.

@steckel
Last active August 29, 2015 14:16
Show Gist options
  • Save steckel/99c9fd268f22500e3867 to your computer and use it in GitHub Desktop.
Save steckel/99c9fd268f22500e3867 to your computer and use it in GitHub Desktop.
Adobe Illustator -> PDF -> XCode 6+ workflow
/**
* Poor man's Slicy for Adobe Illustator -> PDF -> XCode 6+ workflow
*
* This niave script scans the current document for any GroupItem or
* CompoundPathItem who's name matches "@2x" in anyway. Once found, it
* will attempt to export them as PDF to `~/Desktop`.
*
* verboseMode {Bool=false} - When enabled, the script will alert information
* about each layer it attempts to scan.
*
* http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/illustrator/sdk/CC2014/Illustrator%20Scripting%20Reference%20-%20JavaScript.pdf
*/
var verboseMode = false;
function descriptionForLayer(layer) {
var layerName = (typeof layer.name !== "undefined" || layer.name !== null) ?
layer.name : "unnamed layer";
var numberOfLayers = (typeof layer.layers !== "undefined" || layer.layers !== null) ?
layer.layers.length : 0;
var numberOfGroupItems = (typeof layer.groupItems !== "undefined" || layer.groupItems !== null) ?
layer.groupItems.length : 0;
var numberOfCompoundPaths = (typeof layer.compoundPathItems !== "undefined" || layer.compoundPathItems !== null) ?
layer.compoundPathItems.length : 0;
return "Scanning layer " + layerName +
" with " + numberOfLayers + " layers" +
" , " + numberOfGroupItems + " groupItems ";
" and " + numberOfCompoundPaths + " compoundPathItems.";
}
function findElementsToExport(parent) {
if (verboseMode) alert(descriptionForLayer(parent));
var elements = [];
var layers = parent.layers;
for (var i = 0; i < parent.layers.length; i++) {
var layer = layers[i];
var compoundPathItems = layer.compoundPathItems;
for (var _i = 0; _i < compoundPathItems; _i++) {
var compoundPathItem = compoundPathItems[_i];
if (compoundPathItem.name.match("@2x") !== null) elements.push(compoundPathItem);
}
var groupItems = layer.groupItems;
for (var __i = 0; __i < groupItems.length; __i++) {
var groupItem = groupItems[__i];
if (groupItem.name.match("@2x") !== null) elements.push(groupItem);
}
elements.concat(findElementsToExport(layer));
}
return elements;
}
function newDocumentWithFrom(item) {
var newDoc = app.documents.add(DocumentColorSpace.RGB);
var newItem = item.duplicate(newDoc, ElementPlacement.PLACEATEND);
var artboard = newDoc.artboards[0];
artboard.artboardRect = newItem.visibleBounds;
return newDoc;
}
function saveDocumentToPDF(doc, dest) {
var saveName = new File(dest);
saveOpts = new PDFSaveOptions();
saveOpts.compatibility = PDFCompatibility.ACROBAT5;
saveOpts.generateThumbnails = true;
saveOpts.preserveEditability = true;
doc.saveAs(saveName, saveOpts);
}
function main() {
var elementsToExport = findElementsToExport(app.activeDocument);
if (elementsToExport.length === 0) return alert("Did not find any layers to export");
for (var i = 0; i < elementsToExport.length; i++) {
var element = elementsToExport[i];
var filename = element.name;
var newDoc = newDocumentWithFrom(element);
saveDocumentToPDF(newDoc, "~/Desktop/" + filename + ".pdf");
newDoc.close();
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment