Random Trello Card

Thanks to everyone in this thread for sharing your scripts! I just wrote my first script by stealing snippets of code from you guys😆

The script fetches a random Trello card from my “Ongoing” list to remind me of the books, games etc. that I haven’t finished yet.

let url = 'https://api.trello.com/1/lists/5db1e25fe891bd2449e1de99/cards'
let req = new Request(url)
let json = await req.loadJSON()
let index = Math.floor(Math.random() * json.length)
let randCard = json[index]
let r = new Request('https://api.trello.com/1/cards/' + randCard.id + '/attachments/' + randCard.idAttachmentCover)
let imgJSON = await r.loadJSON()
r = new Request(imgJSON.url)
let img = await r.loadImage()
let widget = createWidget(randCard.name, img)
Script.setWidget(widget)
Script.complete()
function createWidget(title, img) {
  let w = new ListWidget()
  w.backgroundColor = new Color("#1A1A1A")
  let image = w.addImage(img);
  image.imageSize = new Size(150,125)
  image.cornerRadius = 4
  image.centerAlignImage()
  let titleTxt = w.addText(title)
  titleTxt.applyHeadlineTextStyling()
  titleTxt.textColor = Color.white()
  titleTxt.textSize = 9
  titleTxt.centerAlignText()
  w.setPadding(4, 0, 0, 0)
  w.url = randCard.url
  return w
}

I do have a question though: is it possible to make this script run periodically so that the widget auto-updates? I don’t need frequent updates, once a day should do fine. I realized I could create a Shortcuts Automation that runs without confirmation, but are there better methods?

1 Like

iOS updates the widget (and runs your script) automatically every 4-5 minutes. If you want to run it less frequently you could save a timestamp in iCloud Drive and check against it everytime.

2 Likes

Woah I somehow didn’t notice that, good to know that I don’t need to worry about running the script at all!

I too love Trello! Nice work!

1 Like