2010-05-07 56 views
2

我想创建一个简单的程序,使用com.apple.speech.recognitionserver循环用户语音输入。我的代码迄今如下:语音识别服务器不保持打开

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" 

如果上面是我的系统上运行它说早上好,然后将其与语音输入系统弹出并等待任何时间,天气,或退出。他们都按照他们所说的去做,但是如果我说时间和天气,并且再次询问,直到我说退出语音服务器超时并且再也不会弹出,它们就不会循环。有没有办法让应用程序保持打开状态,直到程序结束,或者applescript是否无法循环用户语音输入?

回答

1

如果您没有找到让语音识别保持打开的方法,请在再次调用之前尝试添加延迟。我记得(很久以前)发现,如果您尝试将事件发送给已在退出中的应用程序(它不会重新打开应用程序),则可能会丢失事件。

+1

的大约5秒的第一重复和告诉之间的延迟完美工作。谢谢。 – Waffle 2010-05-09 00:26:57

0

之前,您的最终重复添加

告诉应用程序“SpeechRecognitionServer” 退出 端告诉

大约35秒就会重复之后,它是慢如在寒冷的日子蜂蜜,但它的工作原理。试一试。

下面是一个简单的例子:

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