Skip to content

Instantly share code, notes, and snippets.

@jacopocolo
Last active February 21, 2024 06:35
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jacopocolo/0e28c4af2e28c9de058480da0c5d4582 to your computer and use it in GitHub Desktop.
Save jacopocolo/0e28c4af2e28c9de058480da0c5d4582 to your computer and use it in GitHub Desktop.
Scriptable widget that load a random image from an are.na channel
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: black; icon-glyph: user-md;
// just replace the url below with the url if the channel you want to use
// image channels only, doesnt supoort text or anything else
const channel = 'https://www.are.na/una/color-tx32pz_qsu0'
const url = 'https://api.are.na/v2/channels/'+channel.substring(channel.lastIndexOf('/') + 1)+'/contents';
const req = new Request(url)
const res = await req.loadJSON()
const randomBlock = await res.contents[Math.floor(Math.random() * res.contents.length)];
const id = randomBlock.id
const i = await new Request(randomBlock.image.square.url);
const img = await i.loadImage();
if (config.runsInWidget) {
// create and show widget
let widget = createWidget("Are.na random image", img)
Script.setWidget(widget)
Script.complete()
} else {
Safari.open("https://are.na/block/"+id)
}
function createWidget(title, img) {
let w = new ListWidget()
w.backgroundColor = new Color("#1A1A1A")
w.centerAlignContent()
let image = w.addImage(img);
image.centerAlignImage();
image.imageSize = new Size(150,150)
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment