Skip to content

Instantly share code, notes, and snippets.

@10c8
Last active January 26, 2024 04:14
Show Gist options
  • Save 10c8/142bb997d17ae096f9c177ea4d446e4f to your computer and use it in GitHub Desktop.
Save 10c8/142bb997d17ae096f9c177ea4d446e4f to your computer and use it in GitHub Desktop.
scriptable-topbar
const GIST_ID = "10c8/142bb997d17ae096f9c177ea4d446e4f";
const ACTIONS = [["Test!", test]];
async function main() {
const API_URL = "https://xkcd.com/info.0.json";
const data = await new Request(API_URL).loadJSON();
const img = await new Request(data.img).loadImage();
const widget = new ListWidget();
widget.backgroundColor = Color.white();
const w_img = widget.addImage(img);
w_img.applyFittingContentMode();
w_img.centerAlignImage();
Script.setWidget(widget);
}
async function test() {
await alert("Hello, world!");
}
/**
* Loader
*/
const fm_local = FileManager.local();
const files = fm_local.isFileStoredIniCloud(module.filename)
? FileManager.iCloud()
: fm_local;
await alert("Options", [
...ACTIONS,
["Update", update_code],
[
"Exit",
() => {
Script.complete();
},
],
]);
await main();
Script.complete();
/* Helpers */
async function alert(message, options) {
const a = new Alert();
a.message = message;
if (!options || options.length === 0) {
a.addCancelAction("OK");
await a.presentAlert();
} else {
const actions = options.map((o) => o[0]);
for (const action of actions) {
a.addAction(action);
}
const choice = await a.presentAlert();
options[choice][1]();
}
}
async function update_code() {
try {
const req = new Request(
`https://gist.githubusercontent.com/${GIST_ID}/raw`
);
const res = await req.loadString();
files.writeString(module.filename, res);
await alert(
"The script has been updated. Close it for changes to take effect."
);
} catch (err) {
if (err.message) {
await alert(err.message);
} else {
await alert("Failed to update script.");
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment