SketchAPI icon indicating copy to clipboard operation
SketchAPI copied to clipboard

JSON Parse error when accessing some global Settings

Open bomberstudios opened this issue 5 years ago • 2 comments

When trying to access some Sketch’s preferences using the Settings API, we get a JSON Parse error. Running this on the Run Script panel…

const sketch = require('sketch')
console.log(sketch.Settings.globalSettingForKey('colorForMeasurements'))

…gives us:

SyntaxError: JSON Parse error: Unrecognized token '<'
    at parse[native code] (/)
    at onRun (/Users/ale/Library/Application Support/com.bohemiancoding.sketch3/Plugins/Untitled.sketchplugin:3:48)
Script executed in 0.052706s

The Settings implementation assumes all settings can be parsed as JSON, and that's not the case for the ones we store as binary blobs using NSKeyedArchiver (MSColorForSnapsAndGuidesKey, MSColorForMeasurementsKey and MSFlowArrowDrawingColorKey, apparently)

bomberstudios avatar Apr 29 '20 14:04 bomberstudios

Implementation at https://github.com/sketch-hq/SketchAPI/blob/develop/Source/settings/Settings.js#L21-L27

bomberstudios avatar Apr 29 '20 14:04 bomberstudios

I have also come across with this issue, they do not handle SyntaxError error when calling JSON.parse(<smth>)

serjant avatar Jan 11 '21 09:01 serjant