Created
September 30, 2020 16:39
-
-
Save jmscsns/63d4ede102382b44cbea72285609d814 to your computer and use it in GitHub Desktop.
A short script to create a ‘days left’ widget. It’s really just a rip-off of the sample news widget included with Scriptable. The Cabin Sketch font is from Google Fonts and installed with Fontcase.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables | |
let daysNumber = await loadTimeLeft() | |
let widget = await createWidget(daysNumber) | |
// Check if the script is running in | |
// a widget. If not, show a preview of | |
// the widget to easier debug it. | |
if (!config.runsInWidget) { | |
await widget.presentSmall() | |
} | |
// Tell the system to show the widget. | |
Script.setWidget(widget) | |
Script.complete() | |
async function createWidget(items) { | |
let w = new ListWidget() | |
w.backgroundColor = new Color("#b00a0f") | |
let titleTxt = w.addText(daysNumber.toLocaleString()) | |
titleTxt.font = new Font('Cabin Sketch', 96) | |
titleTxt.centerAlignText() | |
switch (daysNumber) { | |
case 1: | |
var daysText = 'day left.'; | |
break; | |
default: | |
var daysText = 'days left.'; | |
} | |
let subtext = w.addText(daysText) | |
subtext.font = new Font('Cabin Sketch', 24) | |
subtext.centerAlignText() | |
titleTxt.textColor = Color.white() | |
return w | |
} | |
async function loadTimeLeft() { | |
var end = new Date('12/21/2020 5:00 PM'); | |
var now = new Date(); | |
var left = Math.ceil((end - now)/(1000*60*60*24)); | |
return left; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment