sketch-hq/SketchAPI

Make it easier to create a symbol from selected layers

KevinGutowski opened this issue · 0 comments

Currently, it's very painful to create a symbol with the JS api. Its much easier using the internal method:

let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selectedLayersObject = doc.selectedLayers
let selectedLayersArray = selectedLayersObject.layers

let interalLayersArray = selectedLayersArray.map(layer => layer.sketchObject)
// convert your JS layers to internal Sketch Objects

let msLayerArray = MSLayerArray.arrayWithLayers(interalLayersArray)
// MSSymbolCreator actually needs this MSLayerArray object, not just a NSArray of MSLayers for some reason
MSSymbolCreator.createSymbolFromLayers_withName_onSymbolsPage(msLayerArray,"test", true)

MSSymbolCreator handles a bunch of stuff like creating a symbols page if it doesn’t exist. It also sends the Symbol to the symbols page. It also creates a new instance of the symbol in place of the layer. It would be great if this method was exposed to the JS api.


Related posts