AppleScript Programming/Temp converter

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

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

--Show a dialog asking for input
display dialog "Enter a temperature:" default answer ""

--Make sure it's a number, if it's not, the script will error
set thetemp to text returned of result as number

--Display another dialog, with 2 buttons, one for Celsius, one for Fahrenheit
display dialog "Pick a scale to convert to:" buttons {"F", "C"} default button 2
set thebutton to button returned of result as string

--Use an if statement to decide which computation to perform
if thebutton = "F" then
	set thetemp to thetemp * 1.8 + 32
else if thebutton = "C" then
	set thetemp to ((thetemp - 32) / 9) * 5
end if

--Display a dialog with the answer
display dialog thetemp buttons {"OK"}