我有这个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
当它被执行时,我认为通过使用keystroke return
模拟按下“返回”键来解除对话框。感谢。
答案 0 :(得分:4)
你的剧本无法奏效。当您告诉应用程序执行某些操作时,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个不同的苹果脚本来做,每个任务一个。另一种方法是使用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
所以你看到有几种方法可以做到这一点。祝你好运。
答案 1 :(得分:3)
“显示对话框”命令包含giving up after [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}
我希望这有帮助! :)