-
-
Save thebestsophist/3d2d9250f9475eaba8665d519425cb16 to your computer and use it in GitHub Desktop.
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
/* Surgery countdown calendar | |
Currently set to count down to August 29, 2022. | |
You can change that on line 23. | |
This is designed to be a small MacOS widget, | |
the parameter should be your time zone's offset | |
(https://www.timeanddate.com/time/map/) | |
from UTC, for the US, that is | |
-04:00 for EDT/AST | |
-05:00 for CDT | |
-06:00 for MDT | |
-07:00 for PDT | |
-08:00 for AKDT | |
-10:00 for HDT | |
Modified from: https://github.com/ChriRothe/CountdownWidget/blob/master/Countdown.js | |
/*************************/ | |
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
let dateForCountdown = '' | |
let icon = '' | |
let showDate = true | |
/* This is yer date, it counts down to midnight, | |
but you can change that time as you want. | |
*/ | |
let widgetInputRAW = '2022-08-29T00:00'; | |
/* Timezone offset since javascript defaults to UTC—if | |
you're crossing standard/daylight savings, you'll | |
want to account for that. | |
*/ | |
widgetInputRAW += args.widgetParameter; | |
let widgetInput = null; | |
if (widgetInputRAW !== null) { | |
widgetInput = widgetInputRAW.toString().split(";"); | |
// Will refactor the whole time verification thingy later | |
// I mean, who else is gonna use this, right? | |
/* | |
if (/^\d{2}:\d{2}$/.test(widgetInput[0].trim()) === false) { | |
throw new Error('Invalid time format. Please format: "+HH:MM"') | |
} | |
*/ | |
dateForCountdown = widgetInput[0].trim() | |
icon = widgetInput[1] || '⏳'; | |
if (widgetInput[2] && widgetInput[2].toLowerCase() === 'true') { | |
showDate = true | |
} | |
} else { | |
throw new Error('No time zone set! Please format: "+HH:MM"') | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
const localeText = { | |
default: [' Day', ' Days'], | |
en: [' Day', ' Days'], | |
de: [' Tag', 'Tage'], | |
fr: ['Jour', 'Jours'], | |
es: ['día', 'días'], | |
it: ['giorno', 'giorni'] | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
let backColor; //Widget background color | |
let backColor2; //Widget background color | |
let textColor; //Widget text color | |
if (Device.isUsingDarkAppearance()) { | |
backColor = '111111'; | |
backColor2 = '222222'; | |
textColor = 'EDEDED'; | |
} else { | |
backColor = '423542'; | |
backColor2 = '846985'; | |
textColor = 'EDEDED'; | |
} | |
function getTimeRemaining(endtime){ | |
const total = Date.parse(endtime) - Date.parse(new Date()) ; | |
const days = Math.floor( total/(1000*60*60*24) ); | |
const weeks = Math.floor( days/7 ); | |
return { | |
total, | |
days, | |
weeks | |
}; | |
} | |
let remainingDays = getTimeRemaining(dateForCountdown).days; | |
let remainingWeeks = getTimeRemaining(dateForCountdown).weeks; | |
// Create Widget | |
let widget = new ListWidget(); | |
widget.setPadding(10, 10, 10, 10) | |
const gradient = new LinearGradient() | |
gradient.locations = [0, 1] | |
gradient.colors = [ | |
new Color(backColor), | |
new Color(backColor2) | |
] | |
widget.backgroundGradient = gradient | |
let provider = widget.addText(icon + " until surgery day!") | |
provider.font = Font.mediumSystemFont(12) | |
provider.textColor = new Color(textColor) | |
widget.addSpacer() | |
let textStack = widget.addStack(); | |
textStack.layoutHorizontally() | |
textStack.addSpacer() | |
textStack.centerAlignContent() | |
let daysText = textStack.addText(`${remainingDays}`) | |
daysText.font = Font.regularSystemFont(52) | |
daysText.textColor = new Color(textColor); | |
daysText.minimumScaleFactor = 0.5; | |
//widget.addSpacer() | |
const languageCode = Device.preferredLanguages()[0].match(/^[\a-z]{2}/) | |
const t = (localeText[languageCode]) ? localeText[languageCode] : localeText.default | |
let postfixText; | |
if (remainingDays === 1) { | |
postfixText = textStack.addText(t[0]) | |
} else { | |
postfixText = textStack.addText(t[1]) | |
} | |
postfixText.font = Font.regularSystemFont(15) | |
postfixText.textColor = new Color(textColor); | |
let textStack2 = widget.addStack(); | |
textStack2.layoutHorizontally() | |
textStack2.addSpacer() | |
textStack2.centerAlignContent() | |
let timeText = textStack2.addText('(' + `${remainingWeeks}` + ' weeks)' ) | |
timeText.textColor = new Color(textColor); | |
timeText.minimumScaleFactor = 0.5; | |
widget.addSpacer() | |
//if(showDate) { | |
const dateText = widget.addDate(new Date(dateForCountdown)) | |
dateText.font = Font.lightSystemFont(10) | |
dateText.textColor = new Color(textColor); | |
dateText.centerAlignText() | |
widget.addSpacer(5) | |
//} | |
if(!config.runsInWidget) { | |
await widget.presentSmall() | |
} else { | |
// Tell the system to show the widget. | |
Script.setWidget(widget) | |
Script.complete() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added a week counter, too.