如何处理AppleScript对话框响应?

时间:2013-03-02 02:56:06

标签: applescript

我正在使用我编写的脚本自动将动态IP写入.txt文件,但我的问题是单击退出按钮时无法关闭对话框。

set yn to (display dialog "Your ip has been written to the server, the application will re-run in 10 minutes if you DO NOT close this window." buttons {"Quit", "Run again"} giving up after 600)
if yn is equal to "Quit" then
quit
end if

4 个答案:

答案 0 :(得分:10)

我最终做的是

display alert "This is an alert" buttons {"No", "Yes"}
if button returned of result = "No" then
     display alert "No was clicked"
else
    if button returned of result = "Yes" then
         display alert "Yes was clicked"
    end if
end if

您可以更换线条"显示警告"没有/是点击了""用你想要运行的任何代码

答案 1 :(得分:4)

了解如何使用按下yn按钮的最简单方法是查看yn

set yn to (display dialog "Your ip has been written to the server, the application will re-run in 10 minutes if you DO NOT close this window." buttons {"Quit", "Run again"} giving up after 600)
return yn

您会看到yn返回{button returned:"Quit", gave up:false}。这表示yn具有您可以在button returned声明中使用的属性if

解决这个问题的另一种方法是查看文档display dialog的AppleScript字典(文件>打开字典...),这是标准添加词典。

答案 2 :(得分:1)

要添加答案,因为原始问题有giving up after,如果对话框确实超过了超时期限,我需要在我的脚本中执行某些操作。这是一个考虑超时的附加选项:

set dialogTitle to "Star Wars Question"
set theDialog to display alert "Do you think Darh Maul should have his own movie?" buttons {"YES", "NO"} default button "YES" giving up after 10
if button returned of theDialog = "" then
    display notification "No decision was made, cancelled dialog" with title dialogTitle
else if button returned of theDialog = "YES" then
    display notification "I concur" with title dialogTitle
else if button returned of theDialog = "NO" then
    display notification "I find your lack of faith disturbing" with title dialogTitle
end if

答案 3 :(得分:0)

display dialog ("An example dialog.") buttons {"Cancel", "OK"}

if button returned of result = "Button" then
 display dialog ("You clicked Button.")
end