LaunchBar action to generate spoken audio files

You may know that macOS comes with a rather powerful text-to-speech system. It’s capable of speaking text in different languages, and can be triggered via keyboard shortcut or the “Speech” context menu.

A lesser-known fact is that it has a command line interface.

$ say "hello world"

This CLI offers some useful options, like specifying a voice and saving to file. By pairing this with LaunchBar, we can build actions that take text input and instantly generate a spoken audio file. I’ve been using this to add spoken audio files to my Anki flashcards:

Instant send the text to LaunchBar, invoke the action, and get the generated audio file. Ridiculously fast!

I used to go to Forvo to get audio spoken by actual humans, but there were too many downsides to this. The audio quality, volume, and accents are inconsistent, and it gets pretty tedious when you’re making lots of new flashcards each day. I also find it more effective to have my audio files include articles when learning gendered nouns. And sometimes, you just want to learn a specific phrase that isn’t available on Forvo.

The built-in speech synthesis CLI + LaunchBar combo is the perfect solution for this.

Download on GitHub

Update (Sep 10, 2020) — A more feature-packed polyglot version of this script, as well as my other LaunchBar actions, are now available on GitHub. The rest of this page may be out of date.

How to set up

First, go to System Preferences ▸ Accessibility ▸ Speech, and make sure you’ve installed the voice you want to use.

Next, here’s the AppleScript we’ll be using as the LaunchBar action. The way I have it set up, the text is spoken by the voice chosen in the Accessibility settings, and the audio is saved as an .m4a file in my system’s temporary folder (so as not to clutter my desktop). You should alter this script to meet your exact needs. For example, you might want to add a voice argument to the shell script (e.g. -v alex) to specify a certain voice.

on handle_string(str)
	
	set timeStr to (time of (current date))
	set tempItemsPath to POSIX path of (path to temporary items)
	set filePath to tempItemsPath & timeStr & ".m4a"
	set output to do shell script "say " & quoted form of str & " --data-format=aac -o " & filePath
	
	tell application "LaunchBar"
		set selection to filePath
		activate
	end tell
	
end handle_string

Aug 2, 2020: Updated for macOS Catalina

Place the .scpt file in your LaunchBar actions folder: ~/Library/Application Support/LaunchBar/Actions/

That’s it!