Created
June 19, 2015 19:25
-
-
Save nolastan/fe69695d60cd04395c2e to your computer and use it in GitHub Desktop.
Sketch.app – import styles from airtable
This file contains hidden or 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
var doc = context.document; | |
var buttons = getButtons(); | |
for (var i=0; i < buttons.length; i++) { | |
attrs = buttons[i].fields; | |
var button = createButton(attrs); | |
doc.currentPage().addLayers([button]); | |
} | |
function getButtons() { | |
var request = NSMutableURLRequest.new(); | |
[request setHTTPMethod:@"GET"]; | |
var queryString = "[AIRTABLE URL HERE]" | |
[request setURL:[NSURL URLWithString:queryString]]; | |
var error = NSError.new(); | |
var responseCode = null; | |
var oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:responseCode error:error]; | |
var dataString = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding]; | |
var data = JSON.parse(dataString); | |
return data.records; | |
} | |
function createButton(attrs) { | |
var rectShape = MSRectangleShape.alloc().init(); | |
rectShape.frame = MSRect.rectWithRect(NSMakeRect(0,0,200,40)); | |
var shapeGroup=MSShapeGroup.shapeWithPath(rectShape); | |
var fill = shapeGroup.style().fills().addNewStylePart(); | |
fill.color = MSColor.colorWithSVGString(attrs.backgroundColor); | |
shapeGroup.frame().constrainProportions = true; | |
shapeGroup.setName('container'); | |
// Create style | |
var sharedStyles=doc.documentData().layerStyles(); | |
log(shapeGroup.style()); | |
sharedStyles.addSharedStyleWithName_firstInstance(attrs.name, shapeGroup.style()); | |
// Put into group | |
var group = MSLayerGroup.new(); | |
group.setName(attrs.name); | |
group.addLayers([shapeGroup]); | |
group.resizeRoot(true); | |
return group; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment