Skip to content

Instantly share code, notes, and snippets.

@nolastan
Created June 19, 2015 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolastan/fe69695d60cd04395c2e to your computer and use it in GitHub Desktop.
Save nolastan/fe69695d60cd04395c2e to your computer and use it in GitHub Desktop.
Sketch.app – import styles from airtable
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