Standard Additions: Make Audio Books Yourself (simple way)

I designed following script to be able to create audio books myself.

This time it works as is, but I will improve it further.

For example, I want to automatically detect language of .txt file, then filter installed voices list to appropriate voices list. Also, I read my text files as UTF8, because I save my .txt files as UTF8, always. So, users who have other .txt files text encoding, should change encoding manually to appropriate text encoding. This other task to automate further. :expressionless:

NOTE: as I checked, converting 1 MB .txt file to 2.5 GB audio book takes about 12 minutes. :slight_smile:


-- make new "Text-to-Speech AIFF Files" on the desktop (if need)
tell application "Finder" to if not (folder "Text-to-Speech AIFF Files" of desktop exists) then ¬
	make new folder at desktop with properties {name:"Text-to-Speech AIFF Files"}

-- get list of installed voices on my Mac
set voiceChoices to paragraphs of (do shell script "say -v? | awk -F\"\\ \\ \" '{print $1}'")

-- read contents of .txt file
set textFile to choose file of type "txt"
set baseName to getBaseName(POSIX path of textFile)
set textSpeak to read file (textFile as text) as «class utf8»

-- choose some voice from installed voices list
set chosenVoice to (choose from list voiceChoices)
if chosenVoice is false then return
set chosenVoice to item 1 of chosenVoice

-- set the name of generated Audio .aiff book
set chosenName to text returned of (display dialog "What do you want to name your AIFF file?" default answer baseName & " (" & chosenVoice & ") .aiff")

-- generate Audio .aiff book
say textSpeak using chosenVoice saving to ¬
	((path to desktop) as Unicode text) & "Text-to-Speech AIFF Files:" & chosenName

on getBaseName(posixPath)
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	if (posixPath ends with "/") then
		set baseName to text item -2 of posixPath
	else
		set baseName to text item -1 of posixPath
	end if
	if (baseName contains ".") then
		set AppleScript's text item delimiters to "."
		set baseName to text 1 thru text item -2 of baseName
	end if
	set AppleScript's text item delimiters to ATID
	return baseName
end getBaseName
1 Like