set theResponse to display dialog "Name?" default answer nameString with icon note buttons {"Cancel", "Continue"} default button "Continue"
if the button returned of the result is "Cancel" then tell me to "exit"
In this case, the second line has access to the property (?) button returned but if I modify like this
set nameString to text returned of (display dialog "Name?" default answer nameString with icon note buttons {"Cancel", "Continue"} default button "Continue")
if the button returned of the result is "Cancel" then tell me to "exit"
I get an error message (my translation) Can't access button returned by [the string I entered in the dialog].
I guess button returned is a property of result and result is some kind of global variable or object that is created when you display a dialog like this? But for some reason, in this case, result points at the nameString?
Thatâs right. In AppleScript, the result variable contains whatever was returned by the previous instruction â if indeed anything was returned.
In your first script, the result contains the record returned by the display dialog command. This has various properties, including button returned, which youâre thus able to read from the result variable.
In your second script, the result variable contains the result of setting nameString to the recordâs text returned property. That is, itâs the value of one of the recordâs properties, not the record itself.
The received wisdom is not to use the result variable if it can be avoided, but to set an ordinary variable to the whateverâs returned by a command, then to extract whatever you need from this ordinary variable. It avoids situations like this where code is added (possibly at a later date) between where the resultâs obtained and where itâs used.
set nameString to ""
set dialogResult to (display dialog "Name?" default answer nameString with icon note buttons {"Cancel", "Continue"} default button "Continue")
if the button returned of dialogResult is "Cancel" then return
set nameString to text returned of dialogResult
The issue raised by the OP concerns the result variable, and Nigelâs response is framed to address that general issue. On a very-minor point and FWIW, two lines of code could be saved by rewriting the above as shown below. If the user chooses the cancel button then the script stops at that point, and if the user chooses the OK button then the script returns the entered text (if any).
set nameString to ""
set nameString to text returned of (display dialog "Name?" default answer nameString with icon note buttons {"Cancel", "Continue"} default button "Continue")
When one of the buttons is âCancelâ, then AppleScript doesnât return a record when the âCancelâ is chosen. Instead it throws an error that you can catch in a try block.
set nameString to "Joseph"
try
set theResponse to display dialog "Name?" default answer nameString with icon note buttons {"Cancel", "Continue"} default button "Continue"
on error -- user chose "Cancel"
return -- do what you want when user chooses "Cancel"
end try
-- continue with rest of script
get text returned of theResponse
Itâs only âcorrectâ to rely on the order of items in a list coerced from a record â or to advise others to do so â when there are fewer than two items in the list! You may usually be able to get away with it at other times, but itâs not 100% safe. A future change in the operating system might cause the order of a list coerced from display dialogâs result record to change, which would break all existing scripts that rely on the present order.
What you can safely do is to set variables by property label in an analogous way to by list position:
set nameString to "some string"
set {button returned:buttonReturn, text returned:textReturn} to (display dialog "Name?" default answer nameString)
Yes. Itâs OK to coerce a record to list as as long as you donât rely on the order of the result! Donât ask me to think of an example where this would be useful.