<Creating a Countdown Widget for Your iPad or iPhone in 10 Minutes>
Written on
Have you ever found yourself surprised by an upcoming event? Like realizing your birthday is tomorrow and you’ve already made dinner plans?
While we have calendars, diary entries, and alarm notifications, these require us to stay vigilant and not overlook reminders.
A home screen widget that counts down the days to your next big event can be incredibly helpful. It serves as a constant visual reminder each time you unlock your iPhone or iPad.
You could purchase applications that offer such widgets, but writing a simple script in JavaScript can achieve this easily. Although Apple promotes Shortcuts for scripting, there's a free app that allows JavaScript scripting: Scriptable.
To get started, download the app, copy the JavaScript code provided here, and paste it into the application.
You will then have your very own countdown widget ready to be added to your Home Screen. Let’s dive in.
What You Will Need
You will need either an iPad or an iPhone, and using a keyboard is advisable, although it’s not strictly necessary if you only plan to copy the code.
Next, download the free Scriptable app from the App Store. I do not have any affiliation with the app's developer.
What is Scriptable?
Scriptable is a powerful automation tool that allows you to create scripts that interact with iOS’s native features.
Widget Specifications
At a high level, the script will take a list of dates, calculate how many days remain until each date, and display this information on the home screen widget. Each date will also have an associated title to explain its significance.
Strategy Overview
- Read an array of event records, each containing a date and a title.
- Create a widget.
- For each event, calculate the number of days until it occurs and display the title.
- Use the absolute value to manage past dates, relying on the title for clarification.
- Update the widget with the results.
Writing the Code
Begin by launching the Scriptable app. Create a new script and assign it a name that you prefer, such as “Date Countdown.” This name will be important when setting up the widget.
Copy and paste the code below, which follows the video tutorial on how to create the script. This process is much simpler than navigating Apple Shortcuts!
Here’s the code to use:
// Based on sample script, Apple Event Countdown
// W Murphy 18-10-23
// The dates you want to track are in this array.
// Each array record holds the date and a title for the date.
const theDates = [
{ aDate: "Dec 25, 2023 0:00:00", title: " days until Xmas"},
{ aDate: "Jul 20, 1969 0:00:00", title: " days since moon landing"},
{ aDate: "Oct 23, 2023 0:00:00", title: " days until my birthday" },
{ aDate: "Jan 1, 2024 0:00:00", title: " days left in this year" }
]
// Sort the dates into ascending order
theDates.sort((a, b) => new Date(a.aDate) - new Date(b.aDate));
// Launch the widget
if (config.runsInWidget) {
let widget = createWidget(theDates)
Script.setWidget(widget)
Script.complete()
}
// function to create widget: this does most of the work.
function createWidget(listOfDates) {
let widget = new ListWidget()
// Loop through the array of date records and put them in the widget.
for(let i = 0; i < listOfDates.length; i++) {
addTextToThis(widget,
theDaysUntil(listOfDates[i].aDate).toLocaleString("en-US") + listOfDates[i].title)}
//make a gradient for the widget background.
let startColor = new Color("#3050cc")
let endColor = new Color("#050530")
let gradient = new LinearGradient()
gradient.colors = [startColor, endColor]
gradient.locations = [0.0, 1]
widget.backgroundGradient = gradient
widget.backgroundColor = new Color("#ff5401")
return widget
}
// helper function to calculate days until date.
// Uses absolute value, and the title indicates whether it's in the past.
function theDaysUntil(aDate) {
var countDownDate = new Date(aDate).getTime();
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.ceil(distance / (1000 * 60 * 60 * 24));
return Math.abs(days)
}
// Format the message for the date
function addTextToThis(widget, line1) {
let title = widget.addText(line1)
title.font = Font.semiboldSystemFont(20);
title.centerAlignText()
title.textColor = Color.white()
}
Using the Widget
- Long press on your home screen.
- Tap the plus sign located in the top left corner.
- Locate Scriptable and select the larger widget option.
The Scriptable widget won't know which script to execute initially, so perform a long tap on it to configure. Scroll down until you find your script.
Once you select the right script, set it to run every time the widget is tapped.
When you return to your home screen, you should see your countdown widget displayed.
Enjoy your new countdown feature!