Skip to content

Instantly share code, notes, and snippets.

@maxfell
Created April 6, 2017 16:40
Show Gist options
  • Select an option

  • Save maxfell/d9dffa127fb8ae7cb940df4c37e15f90 to your computer and use it in GitHub Desktop.

Select an option

Save maxfell/d9dffa127fb8ae7cb940df4c37e15f90 to your computer and use it in GitHub Desktop.
Render selected Sketch slic to NSImage
(function(){
var selection = context.selection.firstObject()
if (selection.class() != "MSSliceLayer")
return
var exportRequest = MSExportRequest.exportRequestsFromExportableLayer_inRect_useIDForName_(
selection,
selection.absoluteInfluenceRect(),
false
).firstObject()
exportRequest.format = "png"
exportRequest.scale = 1.0
var colorSpace = NSColorSpace.sRGBColorSpace()
var exporter = MSExporter.exporterForRequest_colorSpace_(exportRequest, colorSpace)
var imageRep = exporter.bitmapImageRep()
var image = NSImage.alloc().init().autorelease()
image.addRepresentation(imageRep)
COSImageTools.viewNSImage_inWindowNamed_(image, "Share Slice")
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment