Quickly Generating a Voice Prompt on macOS
[.NET, C#, MacOS, Text To Speech]
Recently, I was executing a series of long-running jobs in a LinqPad script, and wondered if there was a way to be alerted when the work was complete, so that I didn’t have to wait near my computer and could keep myself busy.
My main development environment is a MacBook Pro running macOS Sonoma.
It turns out that this is rather simple to accomplish - all I needed to do was shell to the macOS terminal and execute the say command, and let macOS do the heavy lifting for me.
The code took all of a few seconds to write.
//
// Insert some long-running task here
//
// Shell to the terminal and have your message read
// Some basic parameters
var name = "Conrad";
var timeOfDay = "evening";
// Dummy here to capture how long the task took
var span = TimeSpan.FromMinutes(8);
// Shell to execute the command
Process.Start("say", $"Good {timeOfDay}, {name}. The task took {span.TotalMinutes} minutes to execute");
Worked like a charm.
When the task was completed, I could hear the announcement from across the room.
TLDR
You can shell to the operating system and use the say
command for text to speech.
Happy hacking!