"choose from list" dialog

I’m writing a script which requires users to choose a year from a list. The list contains all years from 1900 to the current year. This results in a very large “choose from list” dialog. Is there any way to control the size of this dialog window so that fewer of the list items are visible?

You could play 20 questions first by presenting 1900 to 1925, 1925 to 1950, 1950 to 1975… you get the picture, and then present a shortened list in that range only. For brevity, however, you can’t beat just asking for the birth year and then checking that the result is a number in the range given.

Thanks for the idea. I’ve decided instead to shorten the list to include all years from 20 years ago to the current date, and added an item at the beginning of the list called “Earlier than (current year - 20)…” which, when selected allows the user to manually enter the number in a text field. It’s not perfect, but it suits my needs…

Hi,

I don’t think you can resize the window. Here’s a workaround:


set l to {}
repeat with i from 1900 to (year of (current date))
	set end of l to i
end repeat
set the_count to 0
set r to false
repeat until r
	set i to the_count * 10
	set j to i + 10
	try
		set sub_l to items (i + 1) thru j of l
		set the_count to the_count + 1
	on error
		if (i is (count l)) then set the_count to 0
		set sub_l to items (i + 1) thru -1 of l
		set the_count to 0
	end try
	choose from list sub_l cancel button name "Next list" with empty selection allowed
	set r to result
end repeat
if r is {} then
	display dialog "You didn't choose a year."
else
	display dialog "You chose - " & (r as string)
end if

gl,

In general, it’s probably still a good idea to include a cancel option for a dialog. Since choose list offers only two buttons, any additional options could be added to the list itself (as I believe metrodus has decided to do). Besides a “Before.” option, it might also be helpful to include an “After.” variant.

If, as Adam speculated, the chosen year is the user’s birth year, then the range might initially cover adult users from (say) 20 to 39 years old - perhaps something like this:

to |choose year| for e from l to h
	if e < h then
		set a to "After " & e & "."
	else
		set {e, a} to {h, {}}
	end if
	tell e - 19 to if l < it then
		set {b, r} to {it, {"Before " & it & "."}}
	else
		set {b, r} to {l, {}}
	end if
	repeat with i from b to e
		set r's end to i
	end repeat
	tell (choose from list r & a with prompt "Please choose a year:")
		if it is false then error number -128
		tell item 1 to if class is integer then
			it
		else if it starts with "Before" then
			my (|choose year| for b - 1 from l to h)
		else
			my (|choose year| for e + 20 from l to h)
		end if
	end tell
end |choose year|

tell (current date)'s year to set chosenYear to my (|choose year| for it - 20 from 1900 to it)

(* do something with chosenYear *)

Come, come, Kai - 39 misses me by nearly 30 years (and I’m not 9)!

Adam

Well, the idea was merely to start with a more likely initial range, Adam - rather than with ages 0 - 19 (at the risk of also offending our teenage members). :wink:

If it makes you feel any better, I also excluded myself with that suggestion. However, I’ll leave you to figure out whether I fall into the younger or the um… more mature category. :stuck_out_tongue:

All this discussion about ages and my little app has nothing to do with birthdays whatsoever! :stuck_out_tongue:

Point taken, metrodus.

All the same, such speculation is almost inevitable when folks are trying to come up with the best possible solution. Not that it was significant in this case, but perhaps I could take this as a general opportunity to urge anyone posting questions to include as much background info as possible on what they’re trying to do - and why. Not only can this add to the interest attracted by a question, but may sometimes prompt an even better alternative approach… :smiley: