Perhaps this
let sketch = require('sketch')
let Settings = sketch.Settings
let document = sketch.getSelectedDocument()
let UI = sketch.UI
let key = 'mytestkey2'
let value = 'hello'
Settings.setDocumentSettingForKey(document,key,value)
let counter = 0
saveAfterKey(key,300,30)
function saveAfterKey(key,throttle,numberOfAttempts) {
if (counter > numberOfAttempts) {
return UI.message("Unable to save")
}
if (Settings.documentSettingForKey(document, key)) {
document.save()
UI.message("Document Saved: " + counter)
} else {
counter = counter + 1
setTimeout(()=>{saveAfterKey(key,throttle)},throttle)
}
}
If you are reading/writing large amounts of data you might want to consider moving that out to a separate file instead of on the document.