bizarre conditioinal bug? [Comparing strings to numbers]

i seem to have run across a very odd bug in applescript’s handling of conditional statements.

for example


	set numberofsongs to text returned of (display dialog "Stop after how many songs?" default answer "100")
	if (numberofsongs > 50) then display dialog "that's just too many..."

you would think this statement would display the dialog for any number entered that was greater than 50, after all that’s what the condition is.
but no, it seems that instead it only does it if the number is greater than 50 and the first number is greater than 4, so it acts as if 499 < 50, very strange…

anyone got any clues as to what is going on here?

Hi nat,

this is neither odd nor a bug :wink:

You are going to compare a string (the text returned) with an integer.
AppleScript takes the class of the first item (Unicode text) and coerces the second silently (if possible)

this does the right thing:

set numberofsongs to text returned of (display dialog "Stop after how many songs?" default answer "100")
if (numberofsongs as integer > 50) then display dialog "that's just too many..."

ah huh, thank you

Stefan’s solution is probably the most sensible. But another method that follows from his explanation is to put the integer first:

set numberofsongs to text returned of (display dialog "Stop after how many songs?" default answer "100")
if (50 < numberofsongs) then display dialog "that's just too many..."