Hello all, I am new to sketch plugins. Is there a way to export a whole symbol instance to json string, include its overrides. and then restore the json to SymbolInstance layer?
I have tried this:
To export symbol instance layer to json
const sketch = require('sketch');
const selectedLayers = sketch.getSelectedDocument().selectedLayers;
const layer = selectedLayers.layers[0]; // the symbol instance layer
const layerJson = JSON.stringify(layer); // the exported json string
To restore the json string to symbol instance:
const sketch = require('sketch');
const overrides = [ /** the overrides of json string */ ];
const symbolJson = {
"type": "SymbolInstance",
"id": "268AC45D-90A3-4B46-948F-1337DF8D212E",
"frame": {
"x": 630,
"y": -519,
"width": 304,
"height": 40
},
"name": "01Button按钮/00按钮组合/01默认/01大",
"selected": true,
"hidden": false,
"locked": false,
"exportFormats": [],
"transform": {
"rotation": 0,
"flippedHorizontally": false,
"flippedVertically": false
},
"style": {
"type": "Style",
"id": "FE74B983-5A63-4A96-8D85-AFD91A2E9D45",
"opacity": 1,
"blendingMode": "Normal",
"borderOptions": {
"startArrowhead": "None",
"endArrowhead": "None",
"dashPattern": [],
"lineEnd": "Butt",
"lineJoin": "Miter"
},
"blur": {
"center": {
"x": 0.5,
"y": 0.5
},
"motionAngle": 0,
"radius": 10,
"enabled": false,
"blurType": "Gaussian"
},
"fills": [],
"borders": [],
"shadows": [],
"innerShadows": [],
"styleType": "Layer",
"fontAxes": null
},
"sharedStyleId": null,
};
const page = sketch.getSelectedDocument().selectedPage;
const si = new sketch.SymbolInstance({
...symbolJson,
parent: page
});
si.setOverrideValue(overrides[0], overrides[0].value); // try to set the first override
finally I got this error:
TypeError: undefined is not an object (evaluating 'this._object.overridePoint')
at get (/Applications/Sketch.app/Contents/Resources/SketchAPI.js:1:123717)
at /Applications/Sketch.app/Contents/Resources/core-modules/@skpm/util/index.js:362:23
at forEach[native code] (/)
at formatValue (/Applications/Sketch.app/Contents/Resources/core-modules/@skpm/util/index.js:358:38)
at inspect (/Applications/Sketch.app/Contents/Resources/core-modules/@skpm/util/index.js:205:21)
at /Applications/Sketch.app/Contents/Resources/core-modules/@skpm/util/index.js:139:27
at logEverywhere (/Applications/Sketch.app/Contents/Resources/core-modules/@skpm/console/index.js:20:61)
at log (/Applications/Sketch.app/Contents/Resources/core-modules/@skpm/console/index.js:151:25)
at value (/Applications/Sketch.app/Contents/Resources/SketchAPI.js:1:154483)
at onRun (/Users/liubin/Library/Application Support/com.bohemiancoding.sketch3/Plugins/Untitled.sketchplugin:1515:20)