Applescript输入后自动关闭对话框?

发布于 2024-11-26 09:52:04 字数 598 浏览 1 评论 0原文

好的,所以我正在编写一个苹果脚本来为我执行一些语音控制操作。

我使用 Dragon Dictate 2.0 for mac 进行语音控制,主要使用 applescript 进行编码。除了一次小问题外,我已经把所有事情都安排好了。当等待语音命令时,我让 applescript 显示一个对话框,用于听写文本。

例如。

set cmd1 to the text returned of (display dialog "Speak Command:" default answer "")

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

我的问题是如何按下“确定”而无需说出其他短语。 目前,我有一个语音命令,它会听我说“开始”,然后运行一个按“返回”键的苹果脚本。这可行,但我不想必须说“走”。

我知道我可以添加

giving up after 10

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

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

Ok, so I am writing an applescript to do some voice control actions for me.

I am using Dragon Dictate 2.0 for mac for my voice control and mainly applescript for my coding. I have everything pretty much squared away except for once small issue. When expecting a voice command, I have applescript display a dialog for the text to be dictated into.

eg.

set cmd1 to the text returned of (display dialog "Speak Command:" default answer "")

This displays a dialog box with an empty text field, and the buttons "cancel" and "ok"

My problem is how I can press ok without having to speak an additional phrase.
Currently I have a voice command that listens for me to speak "go" and then runs an applescript that presses the "return" key. This works, but I don't want to have to have to say "go".

I know I can add

giving up after 10

to automatically close the dialog box and accept the input after a certain time period, but there has to be a better way.

I have done some research and found that I could have an event handler for "on done editing" to execute a return keystroke. But I have no idea how to do this. If anyone has any input or ideas that would be great. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话已封尘 2024-12-03 09:52:04

“完成编辑”是最好的方法,但是您必须使用 Objective-C 或 AppleScriptObjC 在 Xcode 中编写对话框。看来这超出了你的能力范围。

一些可能在你能力范围内的东西......你可以模拟你自己的“完成编辑”方法。当显示对话框的第一个 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

The "on done editing" is the best way but then you would have to write your dialog box in Xcode using objective-c or AppleScriptObjC. It seems that is beyond your abilities.

Something that might be within your abilities... you could simulate your own "on done editing" method. You could have a second applescript launch when your first applescript which shows the dialog box is run. This second applescript could check the text field of the dialog box periodically. When the text stops changing for a certain length of time then it could press the return key. It's a little complicated because you have to figure out how to target your dialog box from the second applescript, but it wouldn't be too hard. It might be best to give your dialog box a title, and then use system events to find the window with that title.

The reason you have to do this in a separate applescript is because your normal applescript pauses all commands while the dialog box is open. So you need a separate process running to check the text field and a second script would be the easiest way.

EDIT: Here's how to do it with applescript. First create this script which is your on done editing script. It will dismiss your dialog when the user stops entering text. Save it as a script called onDoneEditing to your desktop.

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

Now create this script. This script will launch the above script using the command line tool "osascript" so that it runs in a background process. Then it shows the dialog box for you. You can read my comments in both scripts to understand how each works.

-- 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文