Script to submit a web page and grab audio result

The speech synthesis on this AT&T website:
http://www.naturalvoices.att.com/demos/
sound great, almost real, and in several languages.

In this hint from macosxhints.com they talk about using these voices to announce your incoming mail, but they have to manually submit different responses and save the results to the disk as aiff’s to play for different mail senders. Shouldn’t there be a way in Applescript to have it call Safari or another web browser with the correct fields in the demo page filled in, and grab the resulting aiff to play, so you can have it say whatever you wanted each time, like read the sender name and subject?

I know this can be done with the built-in Apple text-to-speech but the AT&T voices sound so damn good, and you can even have them speak with British, German, French, or Spanish accents! Hours of endless fun!

This is a lot easier for systems that expose themselves as a web service. I take it this one doesn’t? If not, this sort of thing is usually done by spoofing the request page to fashion your own requests. You’ll need to curl the page and parse out the current random or session key, then, figure out the post method and fashion a submit of your own with the values you want. For example, you could curl a html form page to disk, insert the values you want into the form element’s value tags, and then open the page in the browser. You have to re-write the method url as absolute path if it’s relative.

Here is how you submit the form using Safari:

tell application "Safari"
	do JavaScript "
	with (ttsdemo){
		language.selectedIndex=1; // US English
		ChooseVoice(); // populate 'Select a Voice'
		VoiceOut.selectedIndex=1; // Mike
		OutFormat[1].checked=true; // aiff
		SampleRateOut[1].checked=true; // 16KHz
		InputText.value='This is a sample';
	}
	urlAdjust(ttsdemo); // adjust hidden fields
	ttsdemo.submit();
	" in document 1
end tell

--> exercice for the reader: grab URL of aiff sample, then:
do shell script "cd ~/Desktop; curl -O 'http://blah/output_1.aiff'"
--> then play it

I’ve tried a “curl” version, but you should download the page, read certain info and generate a magic string to use as argument in a form variable, so I don’t think it’s worth the effort :!:

Hey thanks, JJ

That totally does the trick! I don’t even need the shell curl line to download the file since Safari automatically plays the audio file once it’s downloaded, just what I needed.

The only thing is that I need Safari to have the opening page (http://www.naturalvoices.att.com/demos/) open first or the java script doesn’t work. I can use the “open location” command but that goes automatically to my default web browser which is FireFox, not Safari. Unfortunately Firefox doesn’t have support for the “do JavaScript” command so I can’t use this script with that.

So is there a way to tell specifically Safari to open a URL, or temporarily change the default web browser and then change it back when done?

tell application "Safari"
	make new document with properties {URL:"http://www.google.com"}
end tell

Then, use a simple “delay 5” (or whatever) while the document is loaded (or pick this routine: http://bbs.applescript.net/viewtopic.php?t=5675&highlight=safari )