Last active
April 25, 2016 19:53
-
-
Save groundh0g/e843282107f52f39a47e to your computer and use it in GitHub Desktop.
Export Sprites Script for Adobe Illustrator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#target Illustrator | |
/* | |
Copyright (c) 2015 Joseph B. Hall [@groundh0g] | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
var DEFAULT_SCALE = 100.0; | |
var DEFAULT_ONLY_EXPORT_SELECTED_ARTBOARD = true; | |
var DEFAULT_INCLUDE_ARTBOARD_NAME = true; | |
var DEFAULT_INCLUDE_DOCUMENT_NAME = false; | |
var DEFAULT_INCLUDE_EMPTY_LAYERS = false; | |
var DEFAULT_INCLUDE_DOUBLE_RESOLUTION = true; | |
var exportSettings = { | |
scale: DEFAULT_SCALE, | |
onlyExportSelectedBoard: DEFAULT_ONLY_EXPORT_SELECTED_ARTBOARD, | |
includeDocumentName: DEFAULT_INCLUDE_DOCUMENT_NAME, | |
includeArtboardName: DEFAULT_INCLUDE_ARTBOARD_NAME, | |
includeEmptyLayers: DEFAULT_INCLUDE_EMPTY_LAYERS, | |
includeDoubleResolution: DEFAULT_INCLUDE_DOUBLE_RESOLUTION | |
}; | |
if(app.documents.length > 0) { | |
if(promptSettings()) { | |
doExport(); | |
} | |
} else { | |
alert('No document loaded. Aborting.'); | |
} | |
function promptSettings() { | |
var popup = new Window("dialog", "Export Sprites"); | |
popup.orientation = "column"; | |
var grpScale = popup.add("group"); | |
grpScale.add("statictext", undefined, "Scale: "); | |
var txtScale = grpScale.add("edittext", undefined, exportSettings.scale); | |
txtScale.characters = 10; | |
txtScale.active = true; | |
var grpBoards = popup.add("group"); | |
grpBoards.orientation = "column"; | |
grpBoards.alignChildren = "left"; | |
var chkOnlyExportSelectedBoard = grpBoards.add("checkbox", undefined, "\u00A0Only export selected artboard."); | |
var chkWithBoardName = grpBoards.add("checkbox", undefined, "\u00A0Include artboard name in filename."); | |
var chkWithDocName = grpBoards.add("checkbox", undefined, "\u00A0Include document name in filename."); | |
var chkEmptyLayers = grpBoards.add("checkbox", undefined, "\u00A0Include empty layers."); | |
var chkDoubleRes = grpBoards.add("checkbox", undefined, "\u00A0Include @2x files."); | |
chkOnlyExportSelectedBoard.value = exportSettings.onlyExportSelectedBoard; | |
chkWithBoardName.value = exportSettings.includeArtboardName; | |
chkWithDocName.value = exportSettings.includeDocumentName; | |
chkEmptyLayers.value = exportSettings.includeEmptyLayers; | |
chkDoubleRes.value = exportSettings.includeDoubleResolution; | |
var grpButtons = popup.add("group"); | |
grpButtons.align = "right"; | |
grpButtons.add("button", undefined, "OK"); | |
grpButtons.add("button", undefined, "Cancel"); | |
if(popup.show() == 1) { | |
exportSettings.scale = parseFloat(txtScale.text) || DEFAULT_SCALE; | |
exportSettings.onlyExportSelectedBoard = chkOnlyExportSelectedBoard.value; | |
exportSettings.includeArtboardName = chkWithBoardName.value; | |
exportSettings.includeDocumentName = chkWithDocName.value; | |
exportSettings.includeEmptyLayers = chkEmptyLayers.value; | |
exportSettings.includeDoubleResolution = chkDoubleRes.value; | |
return true; | |
} | |
return false; | |
} | |
function doExport() { | |
var doc = app.activeDocument; | |
var folder = doc.fullName.parent.selectDlg("Export Sprites ..."); | |
if(folder != null) { | |
var docName = doc.fullName.name.split('.')[0]; | |
var abFirst = 0; | |
var abLast = doc.artboards.length - 1; | |
if(exportSettings.onlyExportSelectedBoard) { | |
abFirst = doc.artboards.getActiveArtboardIndex(); | |
abLast = abFirst; | |
} | |
for(var abIndex = abFirst; abIndex <= abLast; abIndex++) { | |
doc.artboards.setActiveArtboardIndex(abIndex); | |
processArtboard(docName, doc.artboards[abIndex], folder); | |
} | |
} | |
} | |
function processArtboard(docName, artboard, folder) { | |
var bounds = artboard.artboardRect; // l,t,r,b | |
var scale = exportSettings.scale; | |
var includeEmptyLayers = exportSettings.includeEmptyLayers; | |
var options = new ExportOptionsPNG24(); | |
options.antiAliasing = true; | |
options.transparency = true; | |
options.artBoardClipping = true; | |
saveLayerStates(); | |
hideAllLayers(); | |
var layerNameCollisions = { }; | |
var layers = app.activeDocument.layers; | |
for(var i = 0; i < layers.length; i++) { | |
layers[i].visible = true; | |
options.horizontalScale = scale; | |
options.verticalScale = scale; | |
var layerName = layers[i].name; | |
var layerNameSuffix = ""; | |
if(layerNameCollisions[layerName]) { | |
layerNameSuffix = "-" + layerNameCollisions[layerName]; | |
layerNameCollisions[layerName] = layerNameCollisions[layerName] + 1; | |
} else { | |
layerNameCollisions[layerName] = 1; | |
} | |
var filename = layerName + layerNameSuffix; | |
if(exportSettings.includeArtboardName) { | |
filename = artboard.name + "-" + filename; | |
} | |
if(exportSettings.includeDocumentName) { | |
filename = docName + "-" + filename; | |
} | |
var doc = app.activeDocument; | |
doc.selectObjectsOnActiveArtboard(); | |
var artboardHasObjects = doc.selection.length > 0; | |
if(includeEmptyLayers || artboardHasObjects) { | |
var file = new File(folder.fsName + '/' + filename + ".png"); | |
app.activeDocument.exportFile(file, ExportType.PNG24, options); | |
if(exportSettings.includeDoubleResolution) { | |
options.horizontalScale = scale * 2.0; | |
options.verticalScale = scale * 2.0; | |
file = new File(folder.fsName + '/' + filename + "@2x.png"); | |
app.activeDocument.exportFile(file, ExportType.PNG24, options); | |
} | |
} | |
layers[i].visible = false; | |
} | |
restoreLayerStates(); | |
} | |
var layerStates = []; | |
function saveLayerStates() { | |
layerStates = []; | |
var layers = app.activeDocument.layers; | |
for(var i = 0; i < layers.length; i++) { | |
layerStates.push({ | |
visible: layers[i].visible, | |
locked: layers[i].locked | |
}); | |
} | |
} | |
function restoreLayerStates() { | |
var layers = app.activeDocument.layers; | |
for(var i = 0; i < layers.length; i++) { | |
layers[i].visible = layerStates[i].visible; | |
layers[i].locked = layerStates[i].locked; | |
} | |
} | |
function hideAllLayers() { | |
var layers = app.activeDocument.layers; | |
for(var i = 0; i < layers.length; i++) { | |
layers[i].visible = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still doesn't check for empty layers, but I don't need that functionality yet.