API Docs

Speak text aloud with the system's text-to-speech voices, and send announcements to VoiceOver.

hs.speech.speak() queues speech on a shared speaker, so quick messages are spoken one after another rather than over each other. Call hs.speech.stop() to silence it and clear the queue. For independent queues with their own voice and rate, create speakers with hs.speech.create().

Voices, including enhanced and premium ones, are downloaded in System Settings → Accessibility → Spoken Content → System Voice → Manage Voices. Personal Voices, which sound like the user, are listed and can be spoken with once hs.permissions.requestPersonalVoice() has been granted.

Quick start

hs.speech.speak("Hello from Hammerspoon")

// Wait for speech to finish
hs.speech.speak("Battery low").then(finished => console.log("Done: " + finished))

// Interrupt whatever is being said
hs.speech.stop()
hs.speech.speak("Urgent message")

// If VoiceOver is running, have it speak in the user's VoiceOver voice
// (see announce() for the VoiceOver settings this needs)
if (hs.speech.isVoiceOverRunning) hs.speech.announce("Build finished")

Types

This module provides the following types:

Object Types

Plain JavaScript objects that this module's methods and properties take or return:

HSSpeechOptions

How an utterance is spoken. Every field is optional; a speaker's own properties are used for any that are missing

Field Type Description
voice optional string The identifier or name of a voice, e.g. "com.apple.voice.compact.en-US.Samantha" or "Samantha". See hs.speech.voices()
language optional string A BCP-47 language code, e.g. "fr-FR". Speaks with the system's default voice for that language. Ignored if voice is given
rate optional number The speaking rate, from 0.0 (slowest) to 1.0 (fastest). 0.5 is the normal rate
pitch optional number The pitch multiplier, from 0.5 to 2.0. 1.0 is the normal pitch
volume optional number The volume, from 0.0 (silent) to 1.0 (full volume)
preDelay optional number Seconds to wait before speaking this utterance
postDelay optional number Seconds to wait after speaking this utterance, before the next one
prefersAssistiveTechnologySettings optional boolean Whether to speak with the settings of a running assistive technology, such as VoiceOver's voice and rate, instead of the other options. Has no effect when no assistive technology is running

HSSpeechVoice

A text-to-speech voice installed on this Mac

Field Type Description
identifier string The voice's unique identifier, which can be passed as the voice option
name string The voice's display name, e.g. "Samantha"
language string The voice's BCP-47 language code, e.g. "en-US"
quality "default" | "enhanced" | "premium" The voice's quality. Enhanced and premium voices are larger downloads
gender "male" | "female" | "unspecified" The voice's gender
isPersonalVoice boolean Whether this is a Personal Voice the user created. See hs.permissions.requestPersonalVoice()
isNoveltyVoice boolean Whether this is a novelty voice, such as "Bells" or "Zarvox"

Properties

hs.speech.isSpeaking

boolean

Whether the shared speaker is speaking, paused or has speech queued.

Methods

hs.speech.speak(text, options) -> Promise<boolean>

Queues text to be spoken by the shared speaker. Speech queued by earlier calls is spoken first.

hs.speech.speak(text, options) -> Promise<boolean>
Name Type Description
text string The text to speak.
options optional HSSpeechOptions How to speak this text.
Promise<boolean>
Resolves to true when the text has been spoken, or false if it was interrupted by stop(). Rejects if an option is invalid, such as an unknown voice
Queued speech is spoken to the end even if you don't keep the returned Promise
hs.speech.speak("Hello")
hs.speech.speak("Bonjour", { language: "fr-FR", rate: 0.4 })
hs.speech.speak("All done").then(finished => {
    if (finished) console.log("Finished speaking")
})

hs.speech.stop() -> None

Stops the shared speaker immediately, discarding any queued speech. The Promises of the interrupted utterances resolve to false.

hs.speech.stop() -> None
None
hs.speech.stop()
hs.speech.speak("Something more important")

hs.speech.create() -> HSSpeaker

Creates a speaker, with its own queue of speech and its own voice, rate, pitch and volume. Each speaker speaks independently, so several can talk at once.

hs.speech.create() -> HSSpeaker
A new HSSpeaker.
const narrator = hs.speech.create()
narrator.voice = "Samantha"
narrator.rate = 0.55
narrator.speak("Once upon a time")

hs.speech.voices(language) -> HSSpeechVoice[]

The text-to-speech voices installed on this Mac, sorted by language and name. The user's Personal Voices are only included once hs.permissions.requestPersonalVoice() has been granted.

hs.speech.voices(language) -> HSSpeechVoice[]
Name Type Description
language optional string A BCP-47 language code, or a language prefix such as "en", to list only the voices for that language.
The installed voices.
hs.speech.voices("en").forEach(v => console.log(v.name + " (" + v.language + ", " + v.quality + ")"))

hs.speech.defaultVoice(language) -> HSSpeechVoice | null

The system's default voice for a language.

hs.speech.defaultVoice(language) -> HSSpeechVoice | null
Name Type Description
language optional string A BCP-47 language code, e.g. "de-DE". Defaults to the user's current language.
The default voice, or null if no voice is installed for that language.
console.log(hs.speech.defaultVoice().name)

hs.speech.announce(text) -> Promise<boolean>

Asks VoiceOver to speak an announcement, using the user's VoiceOver voice and settings. Does nothing if VoiceOver isn't running.

Announcements are sent with VoiceOver's AppleScript support, which works while another app is in front. Two things must be allowed first:

  • "Allow VoiceOver to be controlled with AppleScript" must be turned on in VoiceOver Utility's General settings.
  • Hammerspoon 2 must be allowed to control VoiceOver. macOS asks the first time an announcement is made; afterwards it's in System Settings → Privacy & Security → Automation.

If either is missing, the Promise resolves to false and a warning is logged to the Console. Announcements are passed to VoiceOver one at a time, in the order they were made.

hs.speech.announce(text) -> Promise<boolean>
Name Type Description
text string The text to announce.
Promise<boolean>
Resolves to true once VoiceOver has accepted the announcement, or false if VoiceOver isn't running or refused it
hs.speech.announce("Battery at 10 percent").then(announced => {
    if (!announced) hs.speech.speak("Battery at 10 percent")
})