Skip to content

Instantly share code, notes, and snippets.

@jontelang
Created September 12, 2016 13:54
Show Gist options
  • Save jontelang/5c3cc35f5da685b6813114c6c94a8151 to your computer and use it in GitHub Desktop.
Save jontelang/5c3cc35f5da685b6813114c6c94a8151 to your computer and use it in GitHub Desktop.
var plistString = '';
var doc;
var folder = '/some/folder/path';
var onRun = function(context) {
doc = context.document;
// Start a new document
plistString += "<?xml version='1.0' encoding='UTF-8'?>";
plistString += "\n<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>";
plistString += "\n<plist version='1.0'>";
plistString += "\n<array>";
artboards = doc.artboards();
for( var i = artboards.count()-1; i >= 0 ; i--) {
var artboard = [artboards objectAtIndex:i];
var layers = artboard.layers();
// Start a new lesson plist
plistString += "\n <dict>";
// Find/add lesson identifier
plistString += "\n <key>lesson_identifier</key>";
plistString += "\n <string>" + artboard.name() + "</string>";
// Find rest of metadata
var metadataGroup = [layers objectAtIndex:layers.count()-1];
var metadataGroupLayers = metadataGroup.layers();
for (var j = 0; j < metadataGroupLayers.count(); j++) {
var metadataLayer = [metadataGroupLayers objectAtIndex:j];
if( metadataLayer.class() == 'MSTextLayer' ){
plistString += "\n <key>" + metadataLayer.name() + "</key>";
plistString += "\n <string>" + metadataLayer.stringValue() + "</string>";
}
};
// Add lesson items
plistString += "\n <key>lesson_items</key>";
plistString += "\n <array>";
// Go through each layer in the artboard
for( var j = layers.count()-1; j >= 0 ; j--) {
var layer = [layers objectAtIndex:j];
saveLayer(layer,artboard.name());
}
plistString += "\n </array>";
// End lesson
plistString += "\n </dict>";
}
// End a new document
plistString += "\n</array>";
plistString += "\n</plist>";
log(plistString);
var s = [[NSString alloc] initWithString:plistString];
[s writeToFile:folder+"lessons.plist" atomically:true encoding:NSUTF8StringEncoding error:null];
};
function saveLayer(layer,artboard_name){
if( layer.name() == "Background" ){
log("Skipping layer named 'Background'");
return;
}
else{
if( layer.class() == 'MSTextLayer' ){
if( layer.name() == 'title' ){
saveTitleLayer(layer);
}else if( layer.name() == 'subtitle' ){
saveSubtitleLayer(layer);
}else{
saveTextLayer(layer);
}
}
else if( layer.class() == 'MSSliceLayer' ){
saveSliceLayer(layer,artboard_name);
}
else if( layer.class() == 'MSLayerGroup'){
// skip
}
}
}
function saveTitleLayer(layer){
plistString += '\n <dict>';
plistString += '\n <key>page_type</key>';
plistString += '\n <string>title</string>';
plistString += '\n <key>page_content</key>';
plistString += '\n <string>'+ layer.stringValue() +'</string>';
plistString += '\n </dict>';
}
function saveSubtitleLayer(layer){
plistString += '\n <dict>';
plistString += '\n <key>page_type</key>';
plistString += '\n <string>title</string>';
plistString += '\n <key>page_style</key>';
plistString += '\n <string>subtitle</string>';
plistString += '\n <key>page_content</key>';
plistString += '\n <string>'+ layer.stringValue() +'</string>';
plistString += '\n </dict>';
}
function saveTextLayer(layer){
plistString += '\n <dict>';
plistString += '\n <key>page_type</key>';
plistString += '\n <string>text</string>';
plistString += '\n <key>page_content</key>';
plistString += '\n <string>'+ layer.stringValue() +'</string>';
plistString += '\n </dict>';
}
function saveSliceLayer(layer,artboard_name){
// Write to file
var file = folder + artboard_name + "_" + [layer name] + "@2x.png";
[doc saveArtboardOrSlice:layer toFile:file];
//log("Exporting slice layer named: " + layer.name() + " to path: " + file);
plistString += '\n <dict>';
plistString += '\n <key>page_type</key>';
plistString += '\n <string>image</string>';
plistString += '\n <key>page_content</key>';
plistString += '\n <string>'+ artboard_name + "_" + layer.name() +'</string>';
plistString += '\n </dict>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment