If statement driving me nuts

This should be simple but I can’t get it to work:


property incrUnit : missing value
property lcUnit : missing value

on run
	set optionsList to {"Seconds", "Minutes", "Hours", "Days", "Weeks", "Months"}
	set incrUnit to (choose from list optionsList with prompt "Choose interval...")
	if incrUnit is "Seconds" then
		set lcUnit to "seconds"
	else if incrUnit is "Minutes" then
		set lcUnit to "minutes"
	else if incrUnit is "Hours" then
		set lcUnit to "hours"
	else if incrUnit is "Days" then
		set lcUnit to "days"
	else if incrUnit is "Weeks" then
		set lcUnit to "weeks"
	else if incrUnit is "Months" then
		set lcUnit to "months"
	end if
	
	display dialog lcUnit
end run

The last dialog should return with one of the lowercase options (seconds, minutes, etc), but instead returns a missing value.

If the last line is replaced with “incUnit”, it works just fine:

display dialog incUnit

What am I missing here?

Hi,

choose from list returns a list of items or false if no item has been chosen

try this:

property incrUnit : missing value
property lcUnit : missing value

on run
	set optionsList to {"Seconds", "Minutes", "Hours", "Days", "Weeks", "Months"}
	set incrUnit to (choose from list optionsList with prompt "Choose interval...")
	if incrUnit is false then return
	set incrUnit to item 1 of incrUnit
	if incrUnit is "Seconds" then
		set lcUnit to "seconds"
	else if incrUnit is "Minutes" then
		set lcUnit to "minutes"
	else if incrUnit is "Hours" then
		set lcUnit to "hours"
	else if incrUnit is "Days" then
		set lcUnit to "days"
	else if incrUnit is "Weeks" then
		set lcUnit to "weeks"
	else if incrUnit is "Months" then
		set lcUnit to "months"
	end if
	
	display dialog lcUnit
end run

Edit: you can also the same thing without any if statement (except one ;))

property incrUnit : missing value
property lcUnit : missing value

on run
	set optionsList to {"Seconds", "Minutes", "Hours", "Days", "Weeks", "Months"}
	set incrUnit to (choose from list optionsList with prompt "Choose interval...")
	if incrUnit is false then return
	set incrUnit to characters of item 1 of incrUnit
	set lcUnit to ((ASCII character ((ASCII number of item 1 of incrUnit) + 32)) & rest of incrUnit) as string
	
	display dialog lcUnit
end run

The reason is because the “choose from list” dialog returns the value as a list, so you just need to coerce it to a string before your if statements.


property incrUnit : missing value
property lcUnit : missing value

on run
	set optionsList to {"Seconds", "Minutes", "Hours", "Days", "Weeks", "Months"}
	set incrUnit to (choose from list optionsList with prompt "Choose interval...")
	set incrUnit to incrUnit as string
	if incrUnit is "Seconds" then
		set lcUnit to "seconds"
	else if incrUnit is "Minutes" then
		set lcUnit to "minutes"
	else if incrUnit is "Hours" then
		set lcUnit to "hours"
	else if incrUnit is "Days" then
		set lcUnit to "days"
	else if incrUnit is "Weeks" then
		set lcUnit to "weeks"
	else if incrUnit is "Months" then
		set lcUnit to "months"
	end if
	
	display dialog lcUnit
end run

Hi regulus,

coercing it to a string works only, if multiple selection is not allowed (o.k. in this case is does work :))
and you get an error if the user presses the Cancel button

Good explanation Stefan, I see now that by making it a string by selecting the item number is better. The only bad part is now I have to make a few adjustments in my own scripts to adjress this lesson. :smiley: