Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Make an AppleScript Studio application self-update' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Make an AppleScript Studio application self-update
Authored by: sjmills on Apr 06, '05 12:23:00PM
Comparing version strings won't always work if your version numbers include bug fix numbers (the 3rd number in "a.b.c"). One example of where comparing version strings fails is:
return "1.0.10" < "1.0.2"
 -->true
But in that case, "10" is not a "normal" bug fix number, since it exceeds the 0-9 range. To compare versions correctly, you need to convert them into BCD (binary-coded decimal) or compare the version parts individually, e.g.:

on CompareVersions(v1, v2)
	--v1 and v2 are version strings in either "a.b" or "a.b.c" form.
	--Return true if v1 < v2.
	
	set sd to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"."}
	
	set maj1 to text item 1 of v1 as number
	set min1 to text item 2 of v1 as number
	
	try
		set bug1 to text item 3 of v1 as number
	on error
		set bug1 to 0
	end try
	
	set maj2 to text item 1 of v2 as number
	set min2 to text item 2 of v2 as number
	
	try
		set bug2 to text item 3 of v2 as number
	on error
		set bug2 to 0
	end try
	
	set AppleScript's text item delimiters to sd
	
	return maj1 < maj2 or (maj1 = maj2 and (min1 < min2 or (min1 = min2 and bug1 < bug2)))
end CompareVersions
Of course, this doesn't handle the case where you have letters and build numbers at the end, like 1.0.2b14.

[ Reply to This | # ]
Make an AppleScript Studio application self-update
Authored by: sjmills on Apr 06, '05 12:26:23PM

Actually, "10.0 < 2.0" also fails, and 10.0 is a completely valid version number.



[ Reply to This | # ]
Make an AppleScript Studio application self-update
Authored by: geowar1 on Sep 15, '11 04:00:27PM

Mac OS X 10.4 added "considering numeric strings" so that version strings will compare correctly ("10.6.2" < "10.6.10").

on CompareVersionStrings(v1, v2)
considering numeric strings
set isLess to (v1 < v2)
end considering numeric strings
return isLess
end -- on CompareVersionStrings



[ Reply to This | # ]