AppleScript Programming/Sample Programs/Converter for temperatures

Simple script that converts temperatures from Celsius to Fahrenheit and back.

Edited by Smiler121: Now it doesn't crash when letters and symbols are accidentally entered

	--Show a dialog asking for input
	set asktemp to display dialog "Enter a temperature and choose the correct scale:" default answer "" buttons {"Fahrenheit", "Celsius", "Kelvin"}
		--Try to set the input as number
		set theOrigTemp to (text returned of asktemp) as number
		set theScale to button returned of asktemp
		exit repeat
		--If it fails
	on error
		display dialog "You must enter a number!" buttons {"OK"} default button 1 with icon caution
	end try
end repeat
set theOrigTemp to setValue(theOrigTemp, theScale)

--Display another dialog, with 2 buttons, one for Celsius, one for Fahrenheit
display dialog ("You've entered " & (theOrigTemp as number) & " degrees " & theScale & ".  Pick a scale to convert to:") buttons {"Fahrenheit", "Celsius", "Kelvin"} default button 2
set theNewScale to button returned of result as string

--convert the temperature
set theConvertedTemp to (round ((setValue(theOrigTemp, theNewScale) as number) * 10)) / 10

--Display a dialog with the answer
display dialog "The temperature in degrees " & theNewScale & " is " & (theConvertedTemp as number) buttons {"OK"}

on setValue(t, s)
	if s is "Fahrenheit" then
		return t as degrees Fahrenheit
	else if s is "Celsius" then
		return t as degrees Celsius
	else if s is "Kelvin" then
		return t as degrees Kelvin
	end if
end setValue