Dialog Textfield in sevice returns button text instead text entered?

I have a strange thing that I don´t get.

I have the followig code in a Service:


display dialog "New Entry"default answer "" buttons {"Cancel", "Add"} default button 2
copy the result as list to {text_returned, button_pressed}
if button_pressed is equal to "Cancel" then
	error -128
else
	set csv_data to text_returned
	-- more code

end if


The code works fine in ScriptEditor. Now I try to use it in a Service and something strange happens.
The button gets recognized properly, since its doing the loop correct.
BUT
Whatever I enter in the textfield csv_data will be “Add” (the button text !??)
Why is it doing this in the evice when it is working fine in Scripteditor? What am I missing?

Hi,

the strange thing is that display dialog returns a record rather than a list

Replace the second line with

set {text returned:text_returned, button returned:button_pressed} to result

You’re coercing a record to an list, which results in an list with an unknown order. Best way to do it is like getting the values by their keys and not by it’s index.


set dialogResult to display dialog "New Entry" default answer "" buttons {"Cancel", "Add"} default button 2

tell dialogResult to set {button_pressed, text_returned} to {button returned, text returned}

if button_pressed is equal to "Cancel" then
	error -128
else
	set csv_data to text_returned
	-- more code
	
end if 

Thanks guys totally overlooked the record part. The way you scripted it works fine. Both two nice and convinient ways to do it from now on.

Should read a little more of the documentation. :wink: