Getting Value from Button Input

hello all,

Trying to figure out the code for button input.

  1. Need to display a dialog with 3 choices.
  2. Get the seleted value and assign it to a variable.

I’m not sure what I’m missing:


tell application "Finder"
	set v1 to 1
	set v2 to 2
	set v3 to 3
	display dialog ¬
		"Please select Number:" buttons {v1, v2, v3} default button 2
	set dialogVal1 to the result
	
	if dialogVal1 = 1 then
		print "1"
	end if
	if dialogVal1 = 2 then
		print "2"
	end if
	if dialogVal1 = 3 then
		print "3"
	end if
end tell

thanks

Display dialog returns a record. It always contains ˜button returned’; It will also contain ˜text returned’ if your dialog had any text input (from ˜default answer’).

Is this any help?

set v1 to 1
set v2 to 2
set v3 to 3

display dialog "Please select Number:" buttons {v1, v2, v3} default button 2
set dialogVal1 to button returned of result

You also need to be careful with how you’re evaluating the records you get back from the result of the dialog. In your example, you pass integers to the dialog, but the ‘button returned’ value will be of a string type. When you go to test the value returned, you’ll have to either test whether it matches a string value, or coerce the value to an integer before seeing if it matches.

set v1 to 1
set v2 to 2
set v3 to 3

set theSelection to (button returned of (display dialog "Select an option:" buttons {v1, v2, v3}))

if (theSelection is "1") then
	display dialog "Selection: 1"
	
else if ((theSelection as integer) = 2) then
	display dialog "Selection: 2"
	
else if ((theSelection as string) is (v3 as string)) then
	display dialog ("Selection: " & theSelection)

end if

Also, your multiple if statements should be combined into one if-else statement. Since you’re testing the same variable against multiple conditions, it’s best to keep all of the tests in one block.

j

thanks greatly appreicate it. Is there a limitation in how many buttons can be defined? it doesn’t seem that I can define more than 3. Any work around?

The ‘choose from list’ command is probably your best bet, slashdot:

set r to choose from list {1, 2, 3, 4, 5, 6, "a", "b", "c"} with prompt "Choose an option:"
if r is false then error number -128
r's item 1

thanks again kai.