AppleScript - 与对话框窗口交互
我有这个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的脚本行不通。当您告诉应用程序执行某些操作时,AppleScript 会等待应用程序执行该操作,然后再执行其余代码。因此,该脚本正在等待 Finder 完成其任务,然后再继续处理系统事件代码。因此,本质上,在您的脚本中,系统事件命令直到对话框关闭后才会运行,这意味着您永远不能以这种方式关闭对话框。
但是,您可以告诉 applescript 不要等待来自这样的应用程序的响应...
由于 applescript 是单线程的,另一种方法是使用两个单独的进程。第一个用于显示对话框,第二个用于关闭对话框。您可以使用 2 个不同的 applescript 来完成此操作,每个任务一个。另一种方法是使用 shell 创建一个进程,然后将该进程发送到后台,这样 applescript 就不会等待 shell 完成,然后关闭对话框。以下是您可以这样做的方法。
所以你会看到有几种方法可以做到这一点。祝你好运。
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...
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.
So you see there's several ways to do this. Good luck.
“显示对话框”命令包含一个
giving up after [number]
参数,该参数会在 [number] 秒后自动关闭对话框。一个简单的示例:此代码生成一个包含三个按钮的对话框。只要在指定的时间内(本例中为 5 秒)单击即可。如果你不这样做,命令返回的“对话回复”记录将是这样的:
我希望这会有所帮助! :)
The "display dialog" command includes a
giving up after [number]
parameter, which dismisses the dialog automatically after [number] seconds. A quick example: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:
I hope this helps! :)