Skip to content

Instantly share code, notes, and snippets.

@yoav-lavi
Last active October 5, 2023 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoav-lavi/fe57c355d3933b549fa0d4d03f36f5e7 to your computer and use it in GitHub Desktop.
Save yoav-lavi/fe57c355d3933b549fa0d4d03f36f5e7 to your computer and use it in GitHub Desktop.
A key value store for Scriptable
module.exports = storeName => {
const files = FileManager.iCloud();
const documents = files.documentsDirectory();
const stores = `${documents}/stores`;
const store = `${stores}/${storeName}`;
if (!files.isDirectory(stores)) {
files.createDirectory(stores);
}
if (!files.fileExists(store)) {
files.writeString(store, '');
}
const set = (key, value) =>
files.writeExtendedAttribute(
store,
JSON.stringify(value),
key
);
const get = key =>
JSON.parse(
files.readExtendedAttribute(store, key)
);
const remove = key =>
files.removeExtendedAttribute(store, key);
return { set, get, remove };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment