2013-03-02 68 views
2

我正在使用我編寫的腳本自動將我的動態IP寫入.txt文件,但我的問題是,單擊退出按鈕時無法關閉對話框。如何處理AppleScript對話框響應?

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 
+0

我仍然困惑爲什麼我的問題被拒絕投票? – 2014-03-12 05:25:24

+1

我還沒有投票贊成,但這裏是我的猜測......從問題詳情中可以看出你的代碼是如何失敗的。特別是,這個短語是荒謬的:「我的問題是如果對話響應要退出,那麼完成最簡單的任務就是關閉applescript。」 – clozach 2016-09-10 00:12:28

+1

@clozach我的天啊。感謝不要低估,欣賞反饋。當我讀到你的評論時,我在這個問題上歇斯底里地大笑起來。它完全沒有意義。我在過去的3年裏走得很遠。我不完全確定我在回想什麼時候,但我盡我所能地猜測至少有一個可理解的問題。 – 2016-09-18 23:13:46

回答

6

我最終什麼事做了

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 

您可以替換任何代碼要運行

+0

不知道爲什麼這是被投票...它的工作...糾正。 – 2013-06-17 04:05:10

+0

我不知道爲什麼你會得到一個downvote,但這是一個upvote以及upvote的問題,我需要的只是從你的答案返回的''按鈕。 – 2016-12-16 16:11:58

3

搞清楚如何利用壓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(它是StandardAdditions字典)的AppleScript字典(文件>打開字典...)。

0

添加爲答案,因爲沒有/被點擊是線「顯示警報‘’」原始問題有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