Yep, this is a pretty common question here on the forum so I thought I would answer it in more detail.
Ideally, we would have access to Sketch's internal symbol image cache. I don't really recommend messing with that unless you know how to mess with the Sketch headers/making a native plugin.
I'd recommend first trying with the JS api and seeing if that is enough for your needs. Here is some example code to get ya going.
let sketch = require('sketch')
let document = sketch.getSelectedDocument()
let selection = document.selectedLayers.layers[0]
let data = sketch.export(selection, { format: 'jpg', output: false })
let nsdata = data.toNSData()
let nsimage = NSImage.alloc().initWithData(nsdata)
let nsimageview = NSImageView.imageViewWithImage(nsimage)
nsimageview.frame = CGRectMake(0,0,nsimage.size().width,nsimage.size().height)
let alert = NSAlert.alloc().init()
alert.addButtonWithTitle("Done")
alert.setMessageText("Preview Export")
alert.accessoryView= nsimageview
alert.runModal()
Couple things to note:
let data = sketch.export(selection, { format: 'jpg', output: false })
let nsdata = data.toNSData()
You can use the export
part of the JS api to get a Buffer
. This buffer object has a handy toNSData()
method that I always seem to forget because its not documented in the official sketch documentation but rather here 🤦♂️
Don't forget to make an NSImageView to display your NSImage. You'll need to ensure that the frame is of the correct size.
Should get something like this
