views:

107

answers:

2

I am trying to create a simple program that loops for user speech input using com.apple.speech.recognitionserver. My code thus far is as follows:

set user_response to "start"

repeat while user_response is not equal to "Exit"
tell application id "com.apple.speech.recognitionserver"
    set user_response to listen for {"Time", "Weather", "Exit"} with prompt
            "Good Morning"
end tell


if user_response = "Time" then
    set curr_time to time string of (the current date)
    set curr_day to weekday of (the current date)
    say "It is"
    say curr_time
    say "on"
    say curr_day
    say "day"

else if user_response = "Weather" then
    say "It is hot outside. What do you expect?"
end if
end repeat

say "Have a good day"

If the above is run on my system it says good morning and it then pops up with the speech input system and waits for either Time, Weather, or Exit. They all do what they say they are going to do, but instead of looping if I say Time and Weather and asking again until I say exit the speechserver times out and never pops up again. Is there a way of either keeping that application open until the program ends or is applescript not capable of looping for user speech input?

+1  A: 

If you don't find a way to keep speech recognition open, try adding a delay before you call it again. I recall (long ago) finding that events can be just lost if you try to send an event to an application that's already in the middle of quitting (it doesn't reopen the application).

Kevin Reid
A delay of about 5 seconds between the first repeat and the tell worked PERFECTLY. Thank you.
Waffle
A: 

Before your end Repeat add

tell application "SpeechRecognitionServer" quit end tell

After about 35 seconds it will repeat, it is slow as honey on a cold day but it works. give it a try.

Here is a Simple Example:

repeat tell application "SpeechRecognitionServer" set theResponse to listen for {"yes", "no"} with prompt "open a finder?" set voice to (theResponse as text) end tell if voice contains "yes" then tell application "Finder" activate end tell else say "not understood" end if tell application "SpeechRecognitionServer" quit end tell end repeat

Jonathan