Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const url = "https://xkcd.com/info.0.json"
const req = new Request(url)
const { img: imgUrl } = await req.loadJSON()
const imgReq = await new Request(imgUrl)
const img = await imgReq.loadImage()
// for development, displays the widget if run
const debug = false;
if (config.runsInWidget) {
// create and show widget
let widget = createWidget(img)
Script.setWidget(widget)
Script.complete()
} else if (debug) {
let widget = createWidget(img);
await widget.presentMedium();
} else {
Safari.open(imgUrl)
}
function createWidget(img) {
let widget = new ListWidget();
widget.url = imgUrl;
let image = widget.addImage(img);
image.centerAlignImage();
const [width, height] = getDimensions(config.widgetFamily);
image.imageSize = new Size(width, height);
return widget;
}
function getDimensions(size) {
switch (size) {
case "small":
return [150, 150];
case "medium":
return [300, 150];
case "large":
return [300, 300];
default:
return [150, 150];
break;
}
}
@bendiscover

This comment has been minimized.

Copy link

@bendiscover bendiscover commented Mar 15, 2021

I get an error. "widget.centerAlignContent()" is undefined/not a function

@rudotriton

This comment has been minimized.

Copy link
Owner Author

@rudotriton rudotriton commented Mar 16, 2021

@bendiscover Honestly, I forgot I had this here, but I updated it, so now it looks like my current widget script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment