Dialog with specific set of answers

Hi everyone,

What is wrong with what I am doing below. I want the user to enter a number, and it to belong to a particular set of answers. If it does not, I want the question to repeat itself. The example is below:


set dayList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}

set y to number of items of dayList
set A to 1
set theNumber to 1
display dialog "Please enter the day:" default answer theNumber
set theNumber to text returned of result
repeat until A = (y + 1)
	
	if theNumber is not equal to item A of dayList then
		set A to (A + 1)
	else if theNumber is equal to item A of dayList then
		exit repeat
		
	end if
end repeat

return theNumber




Hi,

an integer 5 is never equal to a letter “5”
Change


set theNumber to (text returned of result) as integer

or use an reduced version of the script


set dayList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}

repeat
	display dialog "Please enter the day:" default answer "1"
	set theNumber to (text returned of result) as integer
	if theNumber is in dayList then exit repeat
end repeat

return theNumber

Hi Stefan,

That’s great!! Thanks!!

Much simpler!!

DDHawk

Applescript lacks for customizing dialogs but choose from list is in my opinion the best solution when a user needs to pick an answer.

set dayList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}

repeat
	set chosenDay to choose from list dayList with prompt "Please enter the day:"
	if chosenDay is not false then
		return item 1 of chosenDay as integer
	end if
end repeat