SketchAPI
SketchAPI copied to clipboard
JS API export doesn’t behave as native sketch export
If you have overlapping artboards the JS export will behave differently than the native sketch export. Example code:
let sketch = require('sketch')
let Artboard = sketch.Artboard
let Rectangle = sketch.Rectangle
let document = sketch.getSelectedDocument()
let page = document.pages[0]
page.layers = []
let bgArtboard = new Artboard({
frame: new Rectangle(0,0,100,100),
parent: page,
background: { color: "#D0021B", enabled: true, includedInExport: true }
})
let fgArtboard = new Artboard({
frame: new Rectangle(25,25,50,50),
parent: page,
background: { color: "#0000FF", enabled: true, includedInExport: true }
})
const options = { formats: 'png', output: false }
const buffer = sketch.export(bgArtboard, options).toNSData()
var pasteboard = NSPasteboard.generalPasteboard();
pasteboard.clearContents();
pasteboard.setData_forType(buffer, NSPasteboardTypePNG);
Is this comparing the result between exporting from the (internal/cocoascript) JS API via the pasteboard vs. exporting natively/normally via the File > Export menu?
Yep! Exactly! The JS export seems to grab artboards that are above it whereas the File>Export doesn’t.
Hey guys, is there any update on this issue?
Quick and dirty solution is to just remove the parent of the object that you are wanting to export, export it, and then add the parent back. Just have to be careful with ordering of the layer when adding it back.