AppleScript - 与对话框窗口交互

发布于 2024-11-18 07:54:54 字数 306 浏览 3 评论 0原文

我有这个AppleScript:

tell application "Finder" to display dialog "derp" -- display a dialog
tell application "System Events" to keystroke return -- dismiss that dialog by simulating the pressing of the "return" key

当它被执行时,我认为通过使用击键返回模拟按下“return”键来关闭对话框。谢谢。

I have this AppleScript:

tell application "Finder" to display dialog "derp" -- display a dialog
tell application "System Events" to keystroke return -- dismiss that dialog by simulating the pressing of the "return" key

and when it is executed, I thought that the dialog would be dismissed by simulating the pressing of the "return" key using keystroke return. Thanks.

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

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

发布评论

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

评论(2

我的痛♀有谁懂 2024-11-25 07:54:54

你的脚本行不通。当您告诉应用程序执行某些操作时,AppleScript 会等待应用程序执行该操作,然后再执行其余代码。因此,该脚本正在等待 Finder 完成其任务,然后再继续处理系统事件代码。因此,本质上,在您的脚本中,系统事件命令直到对话框关闭后才会运行,这意味着您永远不能以这种方式关闭对话框。

但是,您可以告诉 applescript 不要等待来自这样的应用程序的响应...

ignoring application responses
    tell application "Finder"
        activate
        display dialog "blah"
    end tell
end ignoring

delay 0.5
tell application "System Events" to keystroke return

由于 applescript 是单线程的,另一种方法是使用两个单独的进程。第一个用于显示对话框,第二个用于关闭对话框。您可以使用 2 个不同的 applescript 来完成此操作,每个任务一个。另一种方法是使用 shell 创建一个进程,然后将该进程发送到后台,这样 applescript 就不会等待 shell 完成,然后关闭对话框。以下是您可以这样做的方法。

do shell script "osascript -e 'tell application \"Finder\"' -e 'activate' -e 'display dialog \"blah\"' -e 'end tell' > /dev/null 2>&1 &"
delay 0.5
tell application "System Events" to keystroke return

所以你会看到有几种方法可以做到这一点。祝你好运。

Your script won't work. When you tell an application to do something the applescript waits for the application to do it before the rest of the code is performed. As such the script is waiting for the Finder to complete its task before moving on to the system events code. So essentially in your script the system events command isn't run until after the dialog is dismissed which means you can never dismiss a dialog this way.

However, you can tell applescript not to wait for a response from an application like this...

ignoring application responses
    tell application "Finder"
        activate
        display dialog "blah"
    end tell
end ignoring

delay 0.5
tell application "System Events" to keystroke return

Since applescript is single threaded, another way would be to use two separate processes. One to show the dialog and a second to dismiss the dialog. You could do that with 2 different applescripts, one for each task. Another way would be to use the shell to create one process, then you send that process to the background so the applescript doesn't wait for the shell to finish, and then dismiss the dialog. Here's how you could do it that way.

do shell script "osascript -e 'tell application \"Finder\"' -e 'activate' -e 'display dialog \"blah\"' -e 'end tell' > /dev/null 2>&1 &"
delay 0.5
tell application "System Events" to keystroke return

So you see there's several ways to do this. Good luck.

云醉月微眠 2024-11-25 07:54:54

“显示对话框”命令包含一个 giving up after [number] 参数,该参数会在 [number] 秒后自动关闭对话框。一个简单的示例:

tell application "Finder" to display dialog "Quick, press a button!" buttons{"1","2","3"} default button 1 giving up after 5

此代码生成一个包含三个按钮的对话框。只要在指定的时间内(本例中为 5 秒)单击即可。如果你不这样做,命令返回的“对话回复”记录将是这样的:

{button returned:"1", gave up:true}

我希望这会有所帮助! :)

The "display dialog" command includes a giving up after [number] parameter, which dismisses the dialog automatically after [number] seconds. A quick example:

tell application "Finder" to display dialog "Quick, press a button!" buttons{"1","2","3"} default button 1 giving up after 5

This code produces a dialog containing three buttons. You can click any of them as long as you do it within the time specified (in this case, 5 seconds). If you fail to do so, the "dialog reply" record returned by the command will be something like this:

{button returned:"1", gave up:true}

I hope this helps! :)

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