Plugin dev: Storing arbitrary metadata on a Sketch object

I have a situation where I am generating artboards for developer handoff and want to be able to effectively remove them for regeneration. My first thought was to store a property at the top level of the Artboard object so that I can filter for them later but it seems they gets sanitized out.

Is there a place to store arbitrary properties on sketch objects? If not, I would suggest it be possible. perhaps a meta object at the top of the object?

Yep, there’s an API for that – take a look at the Settings section of the official Sketch API Reference. Here’s an example for setting and retrieving a key-value pair on a specific layer:

var Settings = require('sketch/settings')

Settings.setLayerSettingForKey(layer, 'my-key', 0.1)
var setting = Settings.layerSettingForKey(layer, 'my-key')

Those key-values pairs are persisted in the document file itself and they won’t get erased until you explicitly remove them via setting a null value for a key.

There’re also different kind of settings available other than just layer-local: per-plugin, global, per-document and per-session.

1 Like

Thanks for this Dmitry! If nothing else I can stash the artboard ids on the document.

Is there a way to set settings on an Artboard object? Not seeing it documented but I feel like i’ve guessed on a few things that weren’t documented but did exist.

1 Like

Absolutely: an artboard is ultimately just a special type of a layer, so any API that operates on a Layer would also work for an Artboard.