Is it possible to get both the text returned and the button returned of the following:
set theInfo to text returned of (display dialog "Hello world" default answer "" buttons {"OK","Do script","Cancel"})
if button returned of theInfo is "Do another" then
do shell script "some shell script"
end if
When I try this, I get an error about can’t get button of “the text I entered”, even though the result log shows
{text returned:the text I entered, button returned:Do scrtipt}
display dialog "Say:" default answer "Test" buttons {"Cancel", "Speak"} default button "Speak"
if button returned of result is "Speak" then
say text returned of result
end if
Now for the explanation.
That depends on what “the following” actually is. Your first line of script has two different events occurring. First, you display a dialog. Then, you set a variable to the text that was returned by the dialog. At this point, the results from the dialog are gone, and you have a variable that contains a string. It is at this point that you are try to get a value (button returned) from a record; That’s a problem, because you don’t have a record, you have a string. Thus, an error is thrown.
Now, the script I posted is very simple. In such a script, we are making use of the predefined variable “result.” Once the user has clicked a button on the dialog, “result” contains a record (e.g. {text returned:“Test”, button returned:“Speak”}). However, using “result” probably won’t work for a more complicated script.
If you need to use the results of the dialog later, put them into a variable:
display dialog "Say:" default answer "Test" buttons {"Cancel", "Speak"} default button "Speak"
set dialogResult to result
if button returned of dialogResult is "Speak" then
say text returned of dialogResult
end if
display dialog "Say:" default answer "Test" buttons {"Cancel", "Speak"} default button "Speak"
copy result to {text returned:theText, button returned:theButton}
if theButton is "Speak" then
say theText
end if
set {text returned:theInfo, button returned:theButton} to display dialog "Hello world" default answer "" buttons {"OK", "Do script", "Cancel"}
if theButton is "Do another" then
do shell script "some shell script"
end if