AppleScript Programming/Sample Programs/Calculator
Simple Calculator
editThis simple calculator uses things we learned from the numbers and strings section, display dialog section, and the basic commands section. It also includes the "if" and "else" statements, as well as the tell block. Try putting this into Script Editor and running it.
Here's the code:
--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
Advanced Calculator
editThis calculator script prevents everything except numbers to be entered. Try putting this into Script Editor and running it.
Here's the code:
--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