AppleScript的输入后自动关闭对话框? [英] Applescript to automatically close dialog box after input?

查看:2313
本文介绍了AppleScript的输入后自动关闭对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我写一个AppleScript为我做一些语音控制操作。

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

我使用的龙听写2.0的Mac对我的语音控制,主要的AppleScript为我的编码。我所拥有的一切pretty多平方远,除了一次小的问题。当期待一个语音命令,我的AppleScript显示文本的对话将决定成。

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.

如。

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"

我的问题是我怎么能preSS确定,而不必讲一个额外的短语。
目前,我有一个声音命令听我说话走出去,然后运行该presses返回键的AppleScript。这工作,但我不希望有不得不说的走出去。

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".

我知道我可以添加

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!

推荐答案

关于完成编辑是最好的方式,但那么你就必须使用Objective-C的或AppleScriptObjC写你的对话框,在X $ C $℃。这似乎超出了你的能力。

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.

东西可能是你的能力范围内...你可以上进行编辑的方法模拟自己。你可以有第二个启动的AppleScript,当你第一次的AppleScript其中显示的对话框中运行。本次的AppleScript可以定期检查对话框的文本字段。当文本停止变化为一定的时间长度,然后它可能preSS的返回键。这是一个有点复杂,因为你必须弄清楚如何从第二目标的AppleScript您的对话框,但不会太辛苦。这可能是最好的给你的对话框标题,然后利用系统事件找到具有该标题的窗口。

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.

您必须这样做在一个单独的AppleScript的原因是因为你的正常的AppleScript暂停所有命令在对话框打开。所以,你需要一个单独的进程中运行,检查文本字段,第二个脚本将是最简单的方法。

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.

修改的:这里是如何使用的AppleScript做首先创建这个脚本是在你完成编辑的脚本。当用户停止输入文本它将解散你的对话框。将其保存为一个名为onDoneEditing到桌面的脚本。

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

现在创建此脚本。这个脚本会使用命令行工具osascript启动上面的脚本,以便它在后台进程运行。然后,它显示了你的对话框。你可以阅读我的​​意见在这两个脚本了解每个如何工作的。

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

这篇关于AppleScript的输入后自动关闭对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆