Speech recognition server does not stay open

I am trying to create a simple program that loops for user speech input using com.apple.speech.recognitionserver. So far, my code looks like this:

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 action is done on my system it says good morning and then the speech input system appears and waits for either Time, Weather, or Exit. They all do what they say they are going to do, but instead of getting stuck in a loop if I say Time and Weather and ask again until I say we’re out of the speech server and never show up again. Is there a way to either keep this application open until the program ends or be used to record the user's speech?

+2


a source to share


2 answers


If you haven't found a way to keep speech recognition open, try adding a delay before you trigger it again. I remember (a long time ago) discovering that events can just be lost if you try to send an event to an application that is already in the middle of exiting (it doesn't reopen the application).



+1


a source


To the end Repeat adding

tell the "SpeechRecognitionServer" application to quit end tell

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



Here's 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

      

0


a source







All Articles