Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Events 是否在输入后自动关闭对话框?_Events_Dialog_Applescript_Handler_Voice Recognition - Fatal编程技术网

Events 是否在输入后自动关闭对话框?

Events 是否在输入后自动关闭对话框?,events,dialog,applescript,handler,voice-recognition,Events,Dialog,Applescript,Handler,Voice Recognition,好的,我正在写一个applescript来为我做一些语音控制动作 我正在使用Dragon听写2.0进行语音控制,主要使用applescript进行编码。除了一个小问题外,我几乎把一切都摆平了。当需要语音命令时,我让applescript显示一个对话框,以便听写文本 例如 这将显示一个带有空文本字段的对话框,以及按钮“取消”和“确定” 我的问题是如何在不说额外短语的情况下按下ok。 目前,我有一个语音命令,听我说“go”,然后运行一个applescript,按下“return”键。这是可行的,但我

好的,我正在写一个applescript来为我做一些语音控制动作

我正在使用Dragon听写2.0进行语音控制,主要使用applescript进行编码。除了一个小问题外,我几乎把一切都摆平了。当需要语音命令时,我让applescript显示一个对话框,以便听写文本

例如

这将显示一个带有空文本字段的对话框,以及按钮“取消”和“确定”

我的问题是如何在不说额外短语的情况下按下ok。 目前,我有一个语音命令,听我说“go”,然后运行一个applescript,按下“return”键。这是可行的,但我不想说“走”

我知道我可以补充

giving up after 10
在一段时间后自动关闭对话框并接受输入,但必须有更好的方法

我做了一些研究,发现我可以有一个“on done editing”事件处理程序来执行返回击键。但我不知道怎么做。如果有人有任何意见或想法,那就太好了。谢谢

完成编辑”是最好的方式,但是您必须使用objective-c或AppleScriptObjC在Xcode中编写对话框。这似乎超出了你的能力

一些你力所能及的事情。。。您可以模拟自己的“完成编辑”方法。当显示对话框的第一个applescript运行时,您可以再次启动applescript。第二个applescript可以定期检查对话框的文本字段。当文本在一定时间内停止更改时,它可以按回车键。这有点复杂,因为您必须找出如何从第二个applescript中确定对话框的目标,但这不会太难。最好为对话框指定一个标题,然后使用系统事件查找具有该标题的窗口

必须在单独的applescript中执行此操作的原因是,当对话框打开时,普通applescript会暂停所有命令。因此,您需要运行一个单独的进程来检查文本字段,第二个脚本是最简单的方法

编辑:以下是如何使用applescript进行编辑。首先创建此脚本,它是您完成编辑后的脚本。当用户停止输入文本时,它将关闭对话框。将其另存为名为onDoneEditing的脚本到桌面

set shortDelay to 0.5
set longDelay to 1

delay longDelay --delay for a moment to let the dialog box from the first script launch

tell application "System Events"
    set theProcess to first process whose frontmost is true
    tell theProcess
        set dialogWindow to first window

        -- this repeat loop looks for when the user starts entering text into the dialog box
        repeat
            -- get the current text field value
            set currentValue to value of first text field of dialogWindow

            -- if the value has changed we know text is being entered
            if currentValue is not "" then exit repeat

            delay shortDelay
        end repeat

        -- this repeat loop looks for when the user stops entering text
        -- we know that when the text is the same after 2 checks
        delay longDelay
        repeat
            set newValue to value of first text field of dialogWindow

            if newValue is currentValue then
                keystroke return
            else
                set currentValue to newValue
            end if

            delay longDelay
        end repeat
    end tell
end tell
现在创建这个脚本。此脚本将使用命令行工具“osascript”启动上述脚本,以便在后台进程中运行。然后为您显示对话框。您可以阅读我在两个脚本中的注释,以了解每个脚本的工作原理

-- launch the onDoneEditing script
-- we get the process id (thePID) of the launched process so we can kill it later
set onDoneEditing to (path to desktop as text) & "onDoneEditing.scpt"
set thePID to do shell script "osascript " & quoted form of POSIX path of onDoneEditing & " > /dev/null 2>&1 & echo $!"

-- diaplay the dialog and make sure it will be frontmost
tell me to activate
display dialog "Speak Command:" default answer ""

-- kill the onDoneEditing to make sure it's not still running
try
    do shell script "kill " & thePID
end try

我有Xcode,只是还没有时间自学objective-c。你知道如何实现目标C吗?如果你能为我提供代码,那将是一个很好的起点。我理解你对applescript所说的,唯一的问题是我不知道如何定期检查对话框的文本字段。另一个问题是一旦显示对话框。当前在后台运行的所有其他脚本或同时停止,直到对话框被关闭。我用applescript解决方案编辑了上面的内容。你不想使用objective-c,因为它对你来说太复杂了。您不仅需要学习objective-c,还需要学习如何使用interface builder创建对话框窗口。没有理由学习所有这些,因为applescript工作得很好。让我知道它是如何工作的!我该如何做类似的事情?当听写打开时,命令窗口被禁用,因此我不能说去获取演讲文本。是因为我没有使用龙指令吗?而且,如果可能的话,如果我能看到这方面的代码,那就太好了。
-- launch the onDoneEditing script
-- we get the process id (thePID) of the launched process so we can kill it later
set onDoneEditing to (path to desktop as text) & "onDoneEditing.scpt"
set thePID to do shell script "osascript " & quoted form of POSIX path of onDoneEditing & " > /dev/null 2>&1 & echo $!"

-- diaplay the dialog and make sure it will be frontmost
tell me to activate
display dialog "Speak Command:" default answer ""

-- kill the onDoneEditing to make sure it's not still running
try
    do shell script "kill " & thePID
end try