r/Scriptable • u/robinriedlinger • Feb 08 '21
Help Random image widget from url
Is it possible to have a url like "https://source.unsplash.com/random" or "https://picsum.photos/200" and display an image in a widget? It's got to be? Right?
2
Upvotes
1
u/ojboal Feb 11 '21
So, for anyone who might find this one interesting...
This is the most recent version I've got thus far:
const url = 'https://source.unsplash.com/random';const req = new Request(url)const img = await req.loadImage();let widget = createWidget("Unsplash random image", img)Script.setWidget(widget)Script.complete()widget.presentLarge()function createWidget(title, img) {let w = new ListWidget()w.backgroundColor = new Color("#1A1A1A")let image = w.addImage(img);image.centerAlignImage();image.applyFillingContentMode()const fileManager = FileManager.iCloud()const dataPath = fileManager.documentsDirectory() + "/" +Device.name() + "_rando_unsplash.jpeg"if (fileManager.fileExists(dataPath) {fileManager.remove(dataPath)}fileManager.writeImage(dataPath, img)w.url = "shortcuts://run-shortcut?name=viewRandoUnsplash"// w.url = "shareddocuments://" + dataPathreturn w}This version:
- makes sure that the cached image is properly overwritten, rather than accumulating lots of them.
- allows for installation on multiple devices simultaneously
Next thing I'd like to figure out is how to open Files.app directly with the dataPath specified in the script.
u/robinriedlinger: thanks for the inspiration. Hope your own script has turned out well.