Heydrun

Accessible scripts

This guide is about writing .hex scripts that work for everyone who uses Heydrun: people who look at the screen, people who listen to it with a screen reader, people who can't hear it, and people with the sound off. It covers how to show things, how to get someone's attention, how to draw a screen that reads well and looks clear, and how to test. It ends with complete examples to copy and change.

The language itself is described in Scripting. Everything used here is in it.

The short version

Showing things

WayWhat happensUse it for
echo <buffer> <text>A line appears in the chat and stays there.A record: results, errors, help text. Something the person can come back to.
toast <text>A short notice at the bottom of the screen, gone in a few seconds. TalkBack usually reads it.A quick "done" or "saved" after a command or a button.
view { … }The script's own screen, with text, buttons, cards and pictures.A game, a form, anything with buttons.
text "…" live in a viewShown, and read out by a screen reader whenever it changes, while the view is on screen.What just happened: the score, whose turn it is, a result.

When in doubt, echo. A line in the chat can be found again with a swipe, /last, /find or Ctrl+Shift+1; a toast is gone in a few seconds.

When an echo is read aloud

An echo is a local line, like Heydrun's own command results. An echo into the buffer on screen within a few seconds of a command you typed is read aloud as that command's result, as Heydrun's own are: even with reading aloud off, and during Do not disturb. So a script's answer to its own command, such as /roll, is heard at once.

Any other echo has no sender, so Heydrun treats it as an event line, and reads it aloud only when Read messages aloud and Also read joins, parts, nick changes and other events are both on, as for a join or a topic change. Otherwise a screen reader user finds the line the way they find any other: by swiping to it, or with /last or Ctrl+Shift+1.

So for the answer to something the person just did, such as a command or a button, either:

Pick one. Echoing and toasting the same thing means the person may hear it twice.

Getting attention

Different people notice different things. A flash is no use to someone who can't see the screen; a sound is no use to someone who can't hear it, or has the phone on silent. These commands each reach someone, and together they reach almost everyone.

CommandWhat happensWho it reaches
notify <text>A notification in Heydrun's Scripts category, titled with the buffer the script ran in. Tapping it opens that buffer.Everyone, whether Heydrun is open or not. Android and the screen reader handle it like any other notification.
flashThe edge of the screen flashes once, as it does for a mention.People who can see the screen: deaf and hard-of-hearing people, and anyone in a noisy place or with the sound off.
vibrateOne of Heydrun's vibrations: vibrate mention, vibrate private, or just vibrate for a short knock.Anyone holding the phone, including in a quiet room or a loud one.
sound <name>One of Heydrun's own sounds, such as sound mention or sound join, with its caption when Captions for sounds is on.People who can hear it, and, through the caption, people who can't.
speak <text>Said through the screen reader or Heydrun's voice.People listening. See Speaking.

The names sound knows are: message, action, notice, private, mention, highlight, join, part, quit, nick, topic, typing, invite, kick, dcc, online, offline, connected and disconnected. A name it doesn't know is ignored.

Each one follows the person's own settings:

None of these make an error when they're held back, so a script carries on as usual.

Choosing

Speaking, for what isn't on screen

speak Ten seconds left
speak -i Your turn
stfu

A screen reader already reads what's on screen. So speak isn't for a script's ordinary output; echo, toast or a view does that, for everyone. speak is for the few things a listener needs said that aren't shown, or shouldn't be:

If what you'd speak is also shown on screen, in a toast, a live text or an echo that's read aloud, don't speak it too: the person hears it twice.

A script's speech is only said when all of these are true:

Colour and bold codes are taken out, and only the first 500 characters are said.

When you do speak:

Respecting settings and Do not disturb

Three read-only values say how the person is using their phone. They're read when the script asks, so they're always current.

FieldValue
$screen.reader1 while a screen reader using explore by touch, such as TalkBack, is on; otherwise 0.
$screen.dnd1 while Heydrun's Do not disturb is on; otherwise 0.
$screen.motion0 when the phone's Remove animations is on; otherwise 1.

Use $screen.reader to change how something is worded, not whether it's shown. For example, a script that decorates its output with emoji might leave them out for a screen reader, where each one is read by name:

if ($screen.reader == 1) { echo $chan Score: %score }
else { echo $chan 🏆 Score: %score 🏆 }

Do not disturb

While Heydrun's Do not disturb is on, a script's speak, flash, vibrate and sound are held, as Heydrun's own are. A held sound still shows its caption, with Captions for sounds on. connected and disconnected sound anyway, as Heydrun's own connection alerts do, and private sounds when Let private messages through is on. notify is still posted, without sound, so it's there afterwards.

Heydrun holds those for you. What it can't know about is anything else a script does to get attention, such as sending a message that makes someone else's phone sound. Check $screen.dnd first.

A script can turn Do not disturb on and off itself:

dnd on        ; on until it's turned off
dnd 25        ; on for 25 minutes (1 to 10080, a week)
dnd off

It works as /dnd does, setting away too when Set away while on is on. A value it doesn't understand is ignored. This alias starts a 25 minute focus session:

alias focus {
  dnd 25
  toast Do not disturb for 25 minutes
}

on DND runs when Do not disturb turns on or off, however it was done: the switch, /dnd, a script, or its time running out. $text is on or off, so on DND:off runs only as it ends. The what I missed example uses it.

Motion

Heydrun follows the phone's Remove animations: with it on, cards in a view appear without dropping in, and the flash shows without fading. A game can keep its animations; see Games. A script that moves things itself, such as by redrawing a view on a timer, should check $screen.motion and do less, or nothing, when it's 0.

Changing what's read aloud: the SPEAK event

on SPEAK runs just before Heydrun reads a line from the buffer on screen aloud: a new message when Read messages aloud, or the buffer's own Read aloud choice, reads it; an event, when events are read; the result of a command you typed; a connection alert. A handler can:

The line in the chat is never changed, only what's said. SPEAK changes Heydrun's reading aloud only; when the person swipes to the line with their screen reader, they hear the line as it is.

In a SPEAK handler:

SPEAK doesn't run for mentions announced from other buffers, for /speak, /last or /catchup, or for a script's own speak. Lines from an encrypted channel or query only reach it while Scripts can read encrypted messages is on, in Settings, Privacy.

A SPEAK handler runs for every line read aloud, so keep it quick.

To stop reading a nick or a channel altogether, there's no need for a script: /speechignore nick does it. SPEAK is for the cleverer cases, such as a bot that should only be read when it mentions you. The examples below have three.

Drawing views that work for everyone

A view is read the way it's written: top to bottom, and left to right within a row. A screen reader user meets it one item at a time, without the layout to help, so each item has to make sense on its own. A sighted user sees it all at once, so what matters has to show, not only be said.

Name the screen with a heading

text "Higher or lower" heading bold

The first heading names the screen, and is said when it opens. Headings also let people jump between parts of a long view. Give each part a heading, but not every line. bold and a larger textsize make it look like a heading too.

Give every button a name it can say

A button is read as its label. A label of symbols reads badly, if at all: "▲" may be read as "black up-pointing triangle". Give it alt:

button "▲" hl_guess higher alt "Higher"
button "♠" pick_suit s alt "Spades"

The label still shows on screen; the alt is what's said. Write the name of what the button does, not what it looks like. Quote an alt of more than one word.

Say, and show, what state a button is in

Prefer disabled to leaving a button out. A button that comes and goes moves everything after it, and the person loses their place.

Because a view's body is filled in before it's read, a variable can hold a state:

set %hl_off disabled
...
button "▲" hl_guess higher alt "Higher" %hl_off

While %hl_off is empty, the button is usable; set it to disabled, draw the view again, and it isn't. A variable can hold more than one prop, such as selected bg #1565c0.

Don't use colour alone

Some people can't tell red from green, and a screen reader doesn't say colours at all. Whatever a colour means, say it in words or a symbol as well:

Cards are already done for you: a red card shows its suit symbol as well as its colour, and is read by name.

Keep text readable against its background. White on a dark blue, such as #1565c0, is fine; light grey on white isn't.

Group what belongs together

merge on a container reads everything in it as one item:

row merge gap 6 {
  text "Score"
  text "%score"
}

That's read as "Score, 3", in one swipe, instead of "Score" and then a lone "3". Use it for a label and its value, or a player's name, chips and cards.

alt on a merged container names the whole group instead of reading what's in it. Quote a name of more than one word: column merge alt "Seat 1, 500 chips".

Use live text for what changes

text "%status" live

A live text is shown like any other, and read by a screen reader whenever it changes, without the person having to find it. Use one, near the top, for what just happened. Two live texts that change together are read one after the other, and a live text that changes every second, such as a timer, never lets anything else be heard.

When a tap changes several things, put them in one live sentence: "It's a queen. Right! Score 3." rather than a live card, a live result and a live score.

Pictures, cards and rings

Don't chatter

Games

Three things mark a script as a game:

Whose turn it is matters most in a game. Put it in a live text while the game is on screen. When the person may be elsewhere in Heydrun, flash and vibrate reach those who are looking or holding the phone, and a short speak -i reaches those who are listening, when nothing on screen would say it.

Testing

Turn your script on in Settings, IRC, Scripts, and reload it after each change.

With TalkBack

  1. Open it and listen. The first thing said should be the screen's name, from its first heading.
  2. Swipe right through everything, from the top. Is the order the order things happen in? Does each item make sense on its own? Is any button read as a symbol, or as just "button"?
  3. Move by heading. In recent versions of TalkBack, swipe up then down to choose Headings, then swipe down to move. Each part should have one.
  4. Double-tap each button, and listen. You should hear what happened, from a live text or a toast, once. Note where focus is afterwards.
  5. Check states. A chosen button should say selected; one that can't be used, disabled. Double-tap a disabled one: nothing should happen.
  6. Explore by touch, dragging a finger round the screen, to find anything a swipe skipped, or anything read twice.
  7. Try the settings that change speech: Speak with set to Screen reader and to Text-to-speech, Scripts can speak off, and events read aloud on and off. With events read aloud off, can you still find every echo?

With the sound off

  1. Turn the sound off, or the phone to silent, and use the script by eye. Is everything that matters shown? Does anything that alerts by sound also flash, vibrate or notify?
  2. Turn on Captions for sounds, and check each sound shows what it's for.
  3. Look at the colours. Does anything rely on colour alone? Is all the text easy to read?

With the person's settings

  1. Turn Do not disturb on. The script should stay quiet: no speech, flashes, buzzes or sounds, and notifications without sound.
  2. Turn on Remove animations, in Android's accessibility settings. Only a game view should still move.
  3. Try it fast. Tap a button many times quickly. Past five in five seconds, speech, flashes, vibrations, sounds and notifications are dropped; make sure nothing important was in them.

If something isn't working, log <text> writes to the script log, and an error there is often why a view or a line is missing.

Examples

Each example is a complete script. Paste one into a new script on the Scripts screen, save, and turn it on. Globals start with a short prefix, such as %hl_, because all scripts share them.

Dice roller

/roll rolls a six-sided die; /roll 2d6 rolls two; /roll d20 one twenty-sided die. The roll goes to the channel as an action, so everyone sees it, you included.

; dice.hex: /roll, /roll 2d6 or /roll d20
alias roll {
  set -l %count 1
  set -l %sides 6
  if ($1 iswm *d*) {
    set -l %parts $split($lower($1), d)
    set -l %count $get(%parts, 0)
    set -l %sides $get(%parts, 1)
  }
  if ($len(%count) == 0) { set -l %count 1 }
  set -l %count $clamp($int(%count), 1, 10)
  set -l %sides $clamp($int(%sides), 2, 100)
  set -l %rolls $list()
  set -l %i 0
  while (%i < %count) {
    push %rolls $rand(1, %sides)
    inc %i
  }
  set -l %total $sum(%rolls)
  me rolls %count d%sides: $join(%rolls, ", "). Total %total.
}

Reminder

/remind 10 the tea is ready puts a line in the chat now, and a reminder in the chat and the notification shade in ten minutes.

; remind.hex: /remind <minutes> <what>
alias remind {
  if ($0 < 2) { echo $chan Usage: /remind <minutes> <what> | return }
  set -l %mins $int($1)
  if (%mins < 1) { echo $chan Give the minutes as a number, such as /remind 10 tea | return }
  timer $calc(%mins * 60000) remind_due $2-
  echo $chan I'll remind you in %mins minutes: $2-
}

on SIGNAL:remind_due {
  echo $chan Reminder: $1-
  notify Reminder: $1-
  flash
}

During Do not disturb, the notification comes without sound and the flash is held, so the reminder waits for you quietly. A reminder is lost if scripts are reloaded or Android closes Heydrun.

Turn cue

For a game bot that announces turns by colour, such as "Red to play", rather than by nick. /colour red tells the script which colour you are. When it's your turn, the screen flashes, the phone vibrates, and "Your turn" is said.

; turncue.hex: /colour <your colour>, then a cue when it's your turn.
alias colour {
  set %tc_colour $1
  toast You're playing %tc_colour
}

on TEXT:*to play* {
  if ($len(%tc_colour) == 0) { return }
  if (!($text iswm %tc_colour to play*)) { return }
  flash
  vibrate mention
  speak -i Your turn
}

Because the bot's line doesn't contain your nick, Heydrun doesn't treat it as a mention, so none of this doubles up with its own alerts. None of it happens while Heydrun is closed, or during Do not disturb.

What I missed

While Do not disturb is on, keeps every mention and private message. When it ends, lists them in the chat.

; missed.hex: list the mentions and private messages from Do not disturb.
on LOAD { set %ms_list $list() }

on HIGHLIGHT {
  if ($screen.dnd != 1) { return }
  push %ms_list $time $chan <$nick> $text
}

on DND:off {
  if ($len(%ms_list) == 0) { return }
  toast $len(%ms_list) messages while Do not disturb was on
  foreach %m %ms_list { echo -a %m }
  set %ms_list $list()
}

The messages are still in their own buffers as well; this just gathers them in one place. The list is lost if scripts are reloaded.

Away notes during Do not disturb

While Do not disturb is on, tells anyone who sends you a private message that you'll read it later, once an hour per person at most.

; dndnote.hex: during Do not disturb, say you'll answer later.
on LOAD { set %dn_told $map() }

on TEXT {
  if ($isprivate != true) { return }
  if ($isme == true) { return }
  if ($screen.dnd != 1) { return }
  set -l %who $lower($nick)
  set -l %last $get(%dn_told, %who)
  if ($len(%last) > 0) {
    if ($calc($ctime - %last) < 3600) { return }
  }
  setat %dn_told %who $ctime
  notice $nick I'm not reading IRC just now. I'll see your message later.
}

People let through Do not disturb get the note too. To leave them out, check their nick before sending.

Shorter joins and quits

When events are read aloud, says "alice joined #rust" instead of "alice, tilde alice at some long host, has joined #rust".

; shorter.hex: joins, parts and quits in fewer words, when they're read aloud.
on SPEAK {
  if ($len($nick) > 0) { return }
  set -l %t $re_replace($text, " \(\S+\) has", " has")
  set -l %t $replace(%t, has joined, joined)
  set -l %t $replace(%t, has left, left)
  set -l %t $replace(%t, has quit, quit)
  rewrite %t
}

Brief wording and Hide hostnames in join/part/quit messages also take out the user@host, without a script; this one shortens the rest.

A quiet bot

Doesn't read a busy bot aloud, unless it mentions you.

; quietbot.hex: don't read FactBot aloud, unless it mentions you.
on SPEAK {
  if ($lower($nick) != factbot) { return }
  if ($me isin $text) { return }
  halt
}

To silence the bot completely, /speechignore FactBot does it without a script.

; links.hex: say "link" instead of reading out a web address.
on SPEAK:*http* {
  rewrite $re_replace($text, "https?://\S+", "link")
}

Higher or lower

A small card game with a view that reads well and shows clearly. Open it from the script launchers, under Higher or lower.

; @game
; higher.hex: guess whether the next card is higher or lower. Aces are low.

on LOAD {
  set %hl_faces $list(A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K)
  set %hl_names $list(an ace, a 2, a 3, a 4, a 5, a 6, a 7, an 8, a 9, a 10, a jack, a queen, a king)
  set %hl_suits $list(s, h, d, c)
  sidebar add higher hl_new Higher or lower
}

alias hl_new {
  set %hl_score 0
  set %hl_off
  set %hl_hi
  set %hl_lo
  hl_deal
  set %hl_msg You have $get(%hl_names, %hl_card). Higher or lower?
  hl_draw
}

alias hl_deal {
  set %hl_card $rand(13)
  set %hl_face $get(%hl_faces, %hl_card)$pick(%hl_suits)
}

alias hl_guess {
  if (%hl_off == disabled) { return }
  set -l %was %hl_card
  hl_deal
  set -l %name $get(%hl_names, %hl_card)
  if ($1 == higher) {
    set %hl_hi selected bg #1565c0
    set %hl_lo
  }
  else {
    set %hl_lo selected bg #1565c0
    set %hl_hi
  }
  if (%hl_card == %was) {
    set %hl_msg It's %name, the same. You said $1. That one's free.
  }
  elseif ((%hl_card > %was && $1 == higher) || (%hl_card < %was && $1 == lower)) {
    inc %hl_score
    set %hl_msg It's %name. You said $1: right! Score %hl_score.
  }
  else {
    set %hl_off disabled
    set %hl_msg It's %name. You said $1: wrong. You scored %hl_score.
  }
  hl_draw
}

alias hl_draw {
  view {
    column game gap 12 pad 16 {
      text "Higher or lower" heading bold textsize 20
      card %hl_face width 72
      text "%hl_msg" live
      row merge gap 6 {
        text "Score"
        text "%hl_score"
      }
      row gap 8 {
        button "▲" hl_guess higher alt "Higher" %hl_hi %hl_off
        button "▼" hl_guess lower alt "Lower" %hl_lo %hl_off
      }
      button "New game" hl_new
    }
  }
}

The script:

The view: