Isaiah Turner
Isaiah Turner

Reputation: 2662

How can I handle AppleScript dialog response?

I am using a script that I wrote to automatically write my dynamic IP to a .txt file but my problem is that I cannot get the dialog to close when the quit button is clicked.

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

Upvotes: 2

Views: 19682

Answers (5)

leanne
leanne

Reputation: 8729

First, some notes:

  • Even used without assigning a variable, dialogs return a result "record" object containing two key-value pairs: button returned and gave up.

  • To get the selected button's text (other than Cancel), use button returned of result.

  • When using giving up after n, use either button returned of result (with text value of "" (empty string)) or gave up of result (with boolean value of true or false).

  • Clicking the "Cancel" button causes an error (number -128 or "User canceled."). To handle the Cancel button, use a try block.

  • In my first example, I'm providing a Cancel button in multiple places. This demonstrates that the on error code will work regardless of which dialog in the try block returns the Cancel button. If you don't need a Cancel, then just provide a single button such as buttons "OK".

  • I'm using parens around some text in my examples, but AppleScript does not require these. AppleScript does require quotes around text.

Tested in both Monterey and High Sierra, and using Mar10Josh's answer, with fixes, as the basis for my examples to make it simple and clear...


try
  display dialog ("An example dialog.") buttons {"Cancel", "OK"} giving up after 5
  
  if button returned of result = "OK" then
    display dialog ("You clicked OK.") buttons {"OK", "Cancel"}

  else if gave up of result then
    display dialog ("You let the dialog expire.") buttons {"OK", "Cancel"}

  end if
  
-- handle the Cancel button here
on error "User canceled." -- or you can say: on error number -128

  display dialog ("You clicked Cancel.") buttons "OK"

end try

Same thing, stated slightly differently to show some other stuff (text without parens, giving up after option, generic error handling):

try

    -- uncomment following line to see non-Cancel-related error result ;)
    -- error "foobar!"
    display dialog "An example dialog." buttons {"OK", "Cancel"} giving up after 5
    
    if button returned of result = "OK" then
        display dialog "You clicked OK." buttons "OK"
        
    else if gave up of result then
        display dialog "You let the dialog expire." buttons "OK"
        
    end if
    
-- handle any error that might occur here;
-- errText and errNum are variable names I chose for these values
on error errText number errNum
    
    if errText is "User canceled." then -- or you can say: if errNum is -128
        display dialog "You clicked Cancel." buttons "OK"
        
    else
        display dialog "Error: " & errText & return & return & "Number: " & errNum buttons "OK"
        
    end if
    
end try

Upvotes: 1

Mar10Josh
Mar10Josh

Reputation: 1

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

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

Upvotes: -1

GʀᴜᴍᴘʏCᴀᴛ
GʀᴜᴍᴘʏCᴀᴛ

Reputation: 8918

To add as an answer as the original question had a giving up after and I needed to do something in my script if the dialog did exceed past the timeout period. Here is an additional option that considers the timeout:

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

Upvotes: 2

Isaiah Turner
Isaiah Turner

Reputation: 2662

What I ended up doing was

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

You can replace the lines "display alert "No/Yes was clicked"" with whatever code you want to run

Upvotes: 17

John Sauer
John Sauer

Reputation: 4421

The simplest way to figure out how to make use of yn's button pressed is to look at 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

You'll see that yn returns {button returned:"Quit", gave up:false}. This indicates that yn has a property button returned that you can use in your if statement.

Another way to figure this out is to look through the AppleScript dictionary (File > Open Dictionary...) that documents display dialog, which is the StandardAdditions dictionary.

Upvotes: 6

Related Questions