DEV Community

Cover image for I Built a Word Clock for My iPhone Using JavaScript — No Swift, No Xcode
Akarshan Gandotra
Akarshan Gandotra

Posted on

I Built a Word Clock for My iPhone Using JavaScript — No Swift, No Xcode

What if your clock told you the time in words instead of numbers?

I recently came across a beautiful minimalist wall clock that illuminates individual words to display the current time. No numbers, no moving hands, just typography and light.

Instead of buying one, I decided to recreate it as an iPhone home screen widget.

The best part? No Swift, no Xcode, no backend. Just JavaScript and an iOS app called Scriptable.

What are we building?

A minimalist Word Clock that:

  • Displays time using illuminated words.
  • Uses a dark, OLED-friendly background.
  • Highlights relevant words dynamically.
  • Represents additional minutes using four small dots.
  • Works completely offline.
  • Requires no external APIs or permissions.

For example, at 2:45, the illuminated words read:

IT IS QUARTER TO THREE

At 2:47, two additional dots illuminate to represent the remaining minutes.

Why Scriptable?

Scriptable is an iOS application that allows you to automate tasks and create custom widgets using JavaScript.

It exposes several native iOS capabilities through JavaScript APIs, including:

  • Calendar and reminders
  • File management
  • HTTP requests
  • Secure credential storage
  • Custom graphics
  • Home screen widgets

For developers who already know JavaScript, it's an interesting way to customize iOS without building a native application.

Step 1: Install Scriptable

Download Scriptable from the App Store.

Open the application, create a new script and name it Word Clock.

Step 2: Design the word matrix

Unlike a traditional digital clock, a Word Clock has a fixed arrangement of words. Only their illumination changes.

Our layout looks like this:

IT     IS     HALF     TEN
QUARTER             TWENTY
FIVE     MINUTES        TO
PAST       ONE       THREE
TWO        FOUR       FIVE
SIX       SEVEN      EIGHT
NINE       TEN      ELEVEN
TWELVE             O’CLOCK

         •  •  •  •
Enter fullscreen mode Exit fullscreen mode

There is one interesting implementation detail here.

Words such as FIVE and TEN appear twice because they can represent either minutes or hours.

Internally, we distinguish them using identifiers such as mFIVE and hFIVE. This prevents both occurrences from illuminating simultaneously.

Step 3: Convert time into words

The clock represents time in five-minute intervals.

We calculate the current interval using:

const now = new Date();
const minute = now.getMinutes();
const m = Math.floor(minute / 5) * 5;
Enter fullscreen mode Exit fullscreen mode

The remaining minutes are represented by four dots.

const remainingMinutes = minute % 5;
Enter fullscreen mode Exit fullscreen mode

There is another important detail: once we cross 30 minutes, the clock references the upcoming hour.

For example:

Time Illuminated words
2:00 IT IS TWO O’CLOCK
2:05 IT IS FIVE MINUTES PAST TWO
2:15 IT IS QUARTER PAST TWO
2:30 IT IS HALF PAST TWO
2:35 IT IS TWENTY FIVE MINUTES TO THREE
2:45 IT IS QUARTER TO THREE
2:55 IT IS FIVE MINUTES TO THREE

We handle the hour transition using modulo arithmetic:

const hourIndex =
  (now.getHours() + (m > 30 ? 1 : 0)) % 12;
Enter fullscreen mode Exit fullscreen mode

This also handles midnight and noon without additional conditions.

Step 4: The complete implementation

Copy the following JavaScript into Scriptable.

// Word Clock for iOS
// No external dependencies or APIs

const now = new Date();
const minute = now.getMinutes();
const m = Math.floor(minute / 5) * 5;

// Theme configuration
const BG = "#080808";
const ACTIVE = "#FFFFFF";
const INACTIVE = "#484848";

const hours = [
  "TWELVE", "ONE", "TWO", "THREE",
  "FOUR", "FIVE", "SIX", "SEVEN",
  "EIGHT", "NINE", "TEN", "ELEVEN"
];

// Determine the active hour
const hourIndex =
  (now.getHours() + (m > 30 ? 1 : 0)) % 12;

// Words that should illuminate
const active = new Set([
  "IT",
  "IS",
  "h" + hours[hourIndex]
]);

// Determine the minute phrase
if (m === 0) {
  active.add("O’CLOCK");
} else {
  const n = m > 30 ? 60 - m : m;

  const minuteWords = {
    5: ["mFIVE"],
    10: ["mTEN"],
    15: ["QUARTER"],
    20: ["TWENTY"],
    25: ["TWENTY", "mFIVE"],
    30: ["HALF"]
  };

  for (const word of minuteWords[n]) {
    active.add(word);
  }

  if (![15, 30].includes(n)) {
    active.add("MINUTES");
  }

  active.add(m > 30 ? "TO" : "PAST");
}

// Fixed word matrix
const rows = [
  ["IT", "IS", "HALF", "mTEN"],
  ["QUARTER", "TWENTY"],
  ["mFIVE", "MINUTES", "TO"],
  ["PAST", "hONE", "hTHREE"],
  ["hTWO", "hFOUR", "hFIVE"],
  ["hSIX", "hSEVEN", "hEIGHT"],
  ["hNINE", "hTEN", "hELEVEN"],
  ["hTWELVE", "O’CLOCK"]
];

// Initialize the widget
const w = new ListWidget();

w.backgroundColor = new Color(BG);
w.setPadding(12, 12, 12, 12);

// Create a drawing canvas
const ctx = new DrawContext();

ctx.size = new Size(320, 320);
ctx.opaque = false;
ctx.respectScreenScale = true;

// Render the word matrix
for (let r = 0; r < rows.length; r++) {
  const words = rows[r];

  const labels = words.map(
    x => x.replace(/^[hm]/, "")
  );

  const weights = labels.map(
    x => x.length + 0.5
  );

  const total = weights.reduce(
    (a, b) => a + b, 0
  );

  let x = 5;

  for (let i = 0; i < words.length; i++) {
    const width = 310 * weights[i] / total;
    const on = active.has(words[i]);

    ctx.setFont(
      on
        ? Font.semiboldSystemFont(24)
        : Font.lightSystemFont(24)
    );

    ctx.setTextColor(
      new Color(on ? ACTIVE : INACTIVE)
    );

    ctx.setTextAlignedCenter();

    ctx.drawTextInRect(
      labels[i],
      new Rect(x, 9 + r * 36, width, 33)
    );

    x += width;
  }
}

// Draw the additional minute indicators
for (let i = 0; i < 4; i++) {
  ctx.setFillColor(
    new Color(
      i < minute % 5 ? ACTIVE : "#303030"
    )
  );

  ctx.fillEllipse(
    new Rect(137 + i * 14, 305, 5, 5)
  );
}

// Render the canvas inside the widget
const img = w.addImage(ctx.getImage());

img.centerAlignImage();
img.applyFittingContentMode();

// Request the next refresh
w.refreshAfterDate = new Date(
  now.getFullYear(),
  now.getMonth(),
  now.getDate(),
  now.getHours(),
  minute + 1,
  0
);

// Register the widget
Script.setWidget(w);

// Preview inside Scriptable
if (!config.runsInWidget) {
  await w.presentLarge();
}

Script.complete();
Enter fullscreen mode Exit fullscreen mode

Step 5: Understanding the rendering engine

Instead of constructing the clock using multiple widget stacks, I used Scriptable's DrawContext API.

This gives us greater control over:

  • Typography
  • Word spacing
  • Alignment
  • Canvas dimensions
  • Minute indicators
  • Active and inactive colours

Every word is rendered onto a 320 × 320 canvas.

The width allocated to each word is proportional to its character count. This creates a more balanced layout without manually calculating coordinates for every word.

Active words use a semibold font and bright white colour. Inactive words use a lighter font and muted grey.

The result resembles a physical illuminated Word Clock.

Step 6: Add the widget to your home screen

  1. Run the script inside Scriptable to preview the clock.
  2. Long-press your iPhone home screen.
  3. Select Edit and then Add Widget.
  4. Search for Scriptable.
  5. Choose the large widget size.
  6. Long-press the newly added widget.
  7. Select Edit Widget.
  8. Choose the Word Clock script.

That's it. You now have a custom JavaScript-powered Word Clock on your iPhone.

What's next?

I'm exploring the idea of turning my iPhone home screen into a personal productivity dashboard using Scriptable.

All powered by small, focused JavaScript programs.

If you've built something interesting using Scriptable, I'd love to hear about it in the comments.

Happy hacking!

References

Top comments (0)