Help with SpeechRecognitionServer

I am very new to AppleScript so please bear with me. I am trying to write an applescript that will ask a question using the SpeechRecognitionServer and wait for a response. As an example what I would like it to do is wait for a yes or no answer. Then if the answer is yes or if the script “gives up” then it will do one action but if the answer is no then it will do a different action. I can get the script to work of a straight yes or no response but I can’t get it to default to a “yes” response if it waits for a certain amount of time with no verbal response from a user. I wrote this simplified script to help me solve the problem. Any help us appreciated.

tell application "SpeechRecognitionServer"
	local choices
	set choices to {"Yes", "No"}
	set thePrompt to "Do you want to sleep?"
	try
		set theResponse to listen for choices with prompt thePrompt giving up after 10
		if theResponse is "yes" or gave up is true then
			say "You look tired."
		else if theResponse is "no" then
			say "Way to power through."
		end if
	end try
end tell

As you can tell I was working on this late at night and wanted to go to bed. :smiley:

While gave up works for display dialog, apparently it doesn’t exist for listen for. I’ve tried getting a value for gave up, but I get neither true nor false, it just doesn’t exist.

That being said, here’s a way that works. When the SpeechRecognitionServer times out, it generates an error, which I assume you realized, since you used a try block to intercept it.

Then answer lies in the try…on error block. If you use an on error statement in your try, you can then get the response you want:

tell application "SpeechRecognitionServer"
	local choices
	set choices to {"Yes", "No"}
	set thePrompt to "Do you want to sleep?"
	try
		set theResponse to listen for choices with prompt thePrompt giving up after 10
		if theResponse is "yes" then
			say "You look tired."
		else if theResponse is "no" then
			say "Way to power through."
		end if
	on error
		say "You look tired."
	end try
end tell

Thanks a lot. I modified it slightly because I wanted the script to do the same thing (such as open an app or start a playlist) on error as it did when it received a “yes” response. So basically I reversed my thinking. Instead of forcing the error to a “yes” command I forced the “yes” command to the error.

tell application "SpeechRecognitionServer"
	local choices
	set choices to {"Yes", "No"}
	set thePrompt to "Do you want to sleep?"
	try
		set theResponse to listen for choices with prompt thePrompt giving up after 10
		if theResponse is "yes" then
			go to "on error"
		else if theResponse is "no" then
			say "Way to power through."
		end if
	on error
		say "You look tired."
	end try
end tell

NOW you’re thinking like a scripter! :smiley:

Good job. Just recently, I came across a script where a fellow needed to get a character converted to its unicode hexadecimal representation. Instead of doing the work in a handler, he realized that if he forced an error in handling the character, the error message contained the value he needed, so he just forced an error and grabbed the text of the error message, parsing out the value he needed.

There are all kinds of ways of getting the job done when it comes to a script. As Obi Wan told Luke, “You’ve taken your first step into a larger world…”