Applescript: get answer and buttons of display dialog

applescriptdialogdisplay

How do I get both the answer and the button pressed of a display dialog on applescript?

The following code will get me the text, but not the button:

set myquestion to the text returned of (display dialog "hallo" ¬
          buttons {"h", "a"} default button "a" default answer "")

But how do I get the button which was pressed? I know I can use the following:

set mybutton to button returned of (display dialog "hallo" ¬
          buttons {"h", "a"} default button "a" default answer "")

But how to I get both?

Best Answer

The result returned of a display dialog command is a record, which is a collection of labeled properties, and as such you can code it so the variable contains the whole record and thus afterwards assign a variable for each labeled property within the record as in the following example:

set theResultReturned to (display dialog "hallo" buttons {"h", "a"} default button "a" default answer "")
set theTextReturned to the text returned of theResultReturned
set theButtonReturned to the button returned of theResultReturned
  • Note the use of the in to the ... can be omitted if you want to be less verbose. Also the variable names can be less verbose as well and were written in this manner for clarity.

You can then act upon the assigned variables as needed/wanted.

Unless for reasons of how/why you're coding it necessitates it, you also can code it in a manner in which separate variables for each labeled property do not have to be explicitly set. For an example logic flow without explicit setting of labeled properties, have a look at the Examples section of the display dialog command in the AppleScript Language Guide.