Just a little temperature converter that works both ways. Lightweight, simple and fast.
set fDeg to false
set cDeg to false
set prompt1 to "Enter Any Number:"
set title1 to "Farenheit/Celsius Converter"
repeat
repeat
set degree to text returned of (display dialog prompt1 default answer "" with title title1 buttons {"Quit", "Convert"} default button 2 cancel button "Quit")
try
set degree to degree as number
end try
if class of degree is not text then exit repeat
display dialog "You must enter a number." buttons {"OK"} default button 1 giving up after 2
end repeat
set cDeg to (degree - 32) * 5 / 9
set cDeg to (round (cDeg * 100)) / 100
set fDeg to 9 / 5 * degree + 32
set fDeg to (round (fDeg * 100)) / 100
set prompt1 to ("Farenheit to Celsius:
" & degree & "° F = " & (cDeg as string) & "° C
Celsius to Farenheit:
" & degree & "° C = " & fDeg & "° F
Enter Any Number:") as string
end repeat
Here’s my version from some years ago:
repeat
tell application "System Events"
activate
set degrees to text returned of (display dialog "Enter a number and append C or F to set the units of the number entered. The result will be in the alternative units." default answer "")
end tell
set ans to true
if "c" is in degrees then
set O to offset of "c" in degrees
set degrees to (text 1 thru (O - 1) of degrees) as real
set conversion to "" & degrees & "°C is " & C_to_F(degrees) & "°F"
else if "f" is in degrees then
set O to offset of "f" in degrees
set degrees to (text 1 thru (O - 1) of degrees) as real
set conversion to "" & degrees & "°F is " & F_to_C(degrees) & "°C"
else
tell application "System Events"
activate
display dialog "Please enter a temperature unit: C or F after the number with or without a space."
end tell
set ans to false
end if
if ans then
tell application "System Events"
activate
if button returned of (display dialog conversion buttons {"Quit", "Again"} default button 1 with title "Temperature Scale Conversion") is "Quit" then exit repeat
end tell
end if
end repeat
to C_to_F(d)
return my oneD(((d * 9 / 5) + 32)) as text
end C_to_F
to F_to_C(d)
return my oneD(5 * (d - 32) / 9) as text
end F_to_C
to oneD(N)
return ((100 * N) div 1) / 100
end oneD
Just for completeness, i suppose i should point out that AppleScript has built-in coercions which can do the calculations for you:
set cDeg to degree as degrees Fahrenheit as degrees Celsius as number
set fDeg to degree as degrees Celsius as degrees Fahrenheit as number
” although if you know the math, that’s actually more efficient!
Conversely, coercing a number to integer is a faster way of rounding it than using the ‘round’ function:
set cDeg to (degree - 32) * 5 / 9
set cDeg to (cDeg * 100) as integer/ 100
set fDeg to 9 / 5 * degree + 32
set fDeg to (fDeg * 100) as integer/ 100
-- Or:
set cDeg to ((degree - 32) * 55.555555555556 as integer) / 100
set fDeg to ((180 * degree + 3200) as integer) / 100
Edit: Script typo corrected. :rolleyes:
Interesting, thanks for the tips and info.
In Adam’s script, is there a reason to call on System Events when using display dialog? I never heard of doing that before. Can anyone explain?
If you activate System Events before posting the dialog it will always appear in front of everything else. Otherwise it can pop up behind another open window.
Wow, that is tremendously helpful, thank you.