AppleScript 编程/示例程序/计算器
外观
< AppleScript 编程 | 示例程序
这个简单的计算器使用了我们在数字和字符串部分、显示对话框部分以及基本命令部分中学到的知识。它还包括“if”和“else”语句以及“tell”块。尝试将它放到脚本编辑器中并运行它。
这是代码
--Brought to you by Smiler121
--Version 1.3
set r to return as text
display dialog "Choose one" buttons {"Add", "Subtract"}
set opr to the button returned of the result
if opr is "Add" then --you clicked add
display dialog "Current equation: ?+?" & r & r & "What is the first number?" default answer "" --displays the current equation and
--asks for first number
set num1 to the text returned of the result
display dialog "Current equation: " & num1 & "+?" & r & r & "What is the second number?" default answer "" --displays the current
--equation and asks for next number
set num2 to the text returned of the result
display dialog "Equation: " & num1 & "+" & num2 & r & r & "Answer: " & num1 + num2 --displays the equation and calculates the answer
else --you clicked subtract
display dialog "Current equation: ?-?" & r & r & "What is the first number?" default answer "" --displays the current equation and
--asks for first number
set num1 to the text returned of the result
display dialog "Current equation: " & num1 & "-?" & r & r & "What is the second number?" default answer "" --displays the current
--equation and asks for next number
set num2 to the text returned of the result
display dialog "Equation: " & num1 & "-" & num2 & r & r & "Answer: " & num1 - num2 --displays the equation and calculates the answer
end if
此计算器脚本阻止除数字以外的所有内容输入。尝试将它放到脚本编辑器中并运行它。
这是代码
--Brought to you by Smiler121
--Version 3.2
tell application "Finder"
set r to return as text
display dialog "Welcome to 'Advanced Calculator' Version 3.2" buttons {"Continue"}
display dialog "Choose one" buttons {"Add", "Subtract", "Divide"} with title "Calculator"
set opr to the button returned of the result
if opr is "Add" then --you clicked add
repeat
set response1 to display dialog "Current equation: ?+?" & r & r & "What is the first number?" default answer "" with title "First number..."
--displays the current equation and asks for first number
try
set num1 to (text returned of response1) as number
exit repeat
on error --if input is not number
set num1 to text returned of response1
display dialog "\"" & num1 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
set num1 to text returned of response1
end try
end repeat
repeat
set response2 to display dialog "Current equation: " & num1 & "+?" & r & r & "What is the second number?" default answer "" with title "Second number..."
--displays the current equation and asks for next number
try
set num2 to (text returned of the response2) as number --checks to see if input is number
exit repeat
on error --if input is not number
set num2 to text returned of response2
display dialog "\"" & num2 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
set num2 to text returned of response2
end try
end repeat
display dialog "Equation: " & num1 & "+" & num2 & r & r & "Answer: " & num1 + num2 with title "The answer is..." --displays the equation
--and calculates the answer
else if opr is "Subtract" then --you clicked subtract
repeat
set response1 to display dialog "Current equation: ?-?" & r & r & "What is the first number?" default answer "" with title "First number..."
--displays the current equation and asks for first number
try
set num1 to (text returned of response1) as number
exit repeat
on error --if input is not number
set num1 to text returned of response1
display dialog "\"" & num1 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
set num1 to text returned of response1
end try
end repeat
repeat
set response2 to display dialog "Current equation: " & num1 & "-?" & r & r & "What is the second number?" default answer "" with title "Second number..."
--displays the current equation and asks for next number
try
set num2 to (text returned of the response2) as number --checks to see if input is number
exit repeat
on error --if input is not number
set num2 to text returned of response2
display dialog "\"" & num2 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
set num2 to text returned of response2
end try
end repeat
display dialog "Equation: " & num1 & "-" & num2 & r & r & "Answer: " & num1 - num2 with title "The answer is..." --displays the equation and calculates the answer
else if opr is "Divide" then --you clicked divide
repeat
set response1 to display dialog "Current equation: ?÷?" & r & r & "What is the first number?" default answer "" with title "First number..."
--displays the current equation and asks for first number
try
set num1 to (text returned of response1) as number --checks to see if input is number
exit repeat
on error --if input is not number
set num1 to text returned of response1
display dialog "\"" & num1 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
set num1 to text returned of response1
end try
end repeat
repeat
set response2 to display dialog "Current equation: " & num1 & "÷?" & r & r & "What is the second number?" default answer "" with title "Second number..."
--displays the current equation and asks for next number
set num2 to text returned of response2
try
set num2 to (text returned of the response2) as number --checks to see if input is number
exit repeat
on error --if input is not number
set num2 to text returned of response2
display dialog "\"" & num2 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
set num2 to text returned of response2
end try
set num2 to text returned of response2
if num2 is "" then --if user didn't input anything
display dialog "Can't divide \"" & num1 & "\" by \"null\". Please try again." buttons {"OK"} default button 1 with title "Try Again" with icon caution
else if num2 is "0" then --if the input is 0
display dialog "Can't divide \"" & num1 & "\" by \"0\". Please try again." buttons {"OK"} default button 1 with title "Try Again" with icon caution
else if num2 is not "0" then --if it's neither
exit repeat
end if
set num2 to text returned of response2
end repeat
display dialog "Equation: " & num1 & "÷" & num2 & r & r & "Answer: " & num1 / num2 with title "The answer is..." --displays the equation and calculates the answer
end if
end tell