Vivek Haldar

Text to speech in MacOS X - automatically

MacOS X has a wonderfully crafted new synthetic voice, Alex. It sounds remarkably natural and non-Tron-like. Given a text file foo.txt, this is how you would convert it into speech in a sound file foo.aiff:

say -v alex -o foo.aiff -f foo.txt

I do a good fraction of my “reading” (especially long articles on the web) by converting them into speech like this, and dropping them into my iPod.

You can go a step further to automate this: use folder actions with an AppleScript so that text files dropped into a given folder are automatically converted into speech files. The script goes like this:

(*
add - new item say

This Folder Action handler is triggered whenever items are added to the attached folder. The script will convert the item added into a .aiff sound file, containing the contents of the added item spoken out loud, using the “Alex” voice. *)

property dialog_timeout : 10 – set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items try repeat with current_item in added_items (* Get just the name, without the full path, of the current item *) tell application “Finder” set the current_name to the name of current_item end tell

  (* The sound file is created one level up *)
  set the sound_file to POSIX path of this_folder & "/../" & current_name & ".aiff"

  (* The shell invocation for converting to speech *)
  set the say_script to "cd " & POSIX path of this_folder & "; /usr/bin/say -v alex -o " & sound_file & " -f " & POSIX path of current_item
  do shell script say_script
end repeat

(* One notification when all the conversions are done *)
set the alert_message to "Speaking done!"
display dialog the alert_message buttons {"OK"} default button 1 with icon 1 giving up after dialog_timeout

end try end adding folder items to