set enter_name to text returned of (display dialog “Enter name” default answer “” buttons {“Back”, “Add”} default button 2)
set button_returned to button returned of enter_name
Which I thought should work.
It compiles OK but when I run it I get the following error:
Can’t get button returned of “”.
And the event log reads (with the bit in bold picked out in purple):
display dialog "Enter name" default answer "" buttons {"Back", "Add"} default button 2
{text returned:"", button returned:"Back"}
"Can't get [b]button returned of \"\"[/b]."
display dialog returns a record {button returned and text returned}.
You try to get button returned of text returned instead of the record result of display dialog.
This works:
set enter_name to (display dialog "Enter name" default answer "" buttons {"Back", "Add"} default button 2)
set button_returned to button returned of enter_name
enter_name is the variable name associated with the text returned of the dialog, but it’s the dialog itself that has a button returned as well. If you want both the text and the button, then:
set D to (display dialog "Enter name" default answer "" buttons {"Back", "Add"} default button 2)
set Ans to text returned of D
set Btn to button returned of D
Or in more compressed form:
set {text returned:Ans, button returned:Btn} to (display dialog "Enter name" default answer "" buttons {"Back", "Add"} default button 2)