跳至内容

AppleScript 编程/示例程序/温度转换器

来自 Wikibooks,开放世界的开放书籍

一种简单的脚本,它能将温度从 摄氏度 转换为 华氏度,反之亦然。

Smiler121 编辑:现在脚本不会在意外输入字母和符号时崩溃

repeat
	
	--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
		
		--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
下一页: 替换字母 | 上一页: 全屏运行 QuickTime
主页: AppleScript 编程/示例程序
华夏公益教科书