Why do I lose access to the result property of a display dialog?

A very basic dialog looks like this:

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
2 Likes

Never do this. Records aren’t ordered. The order of their values when they’re coerced to list isn’t guaranteed, whatever it usually may be.

1 Like

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

Hi @Fredrik71.

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)
1 Like

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. :rofl:

2 Likes