I'm coding an application with a password field:
set password1 to text returned of (display dialog "To continue please enter your special passcode below." buttons {"Cancel", "Continue"} default button 2 default answer "" cancel button 1 with hidden answer)
if the password1 is "passwordhere" then
display dialog "You have entered the password right!
The right password was: passwordhere
The entered password was: " & password1 buttons {"Cancel", "Continue"} default button 2 cancel button 1
else
if the password1 is "" then
set password1 to "Empty Passwordfield"
end if
display dialog "You have entered the password wrong!
The right password was: ********
The entered password was: " & password1 buttons {"Cancel", "Try Again"} default button 2 cancel button 1
end if
end
Now my question is: How do I make a "Try Again" button so it will return to the first display dialog? Is this possible? If not please say that in an answer.
Also a question: Is this also possible for just 2 normal dialogs? So that in the second dialog is a "Back" button? And if you press that button you'll return to the first dialog?
Thanks for reading,
Jort
Consider placing the password prompt/dialog in a applescript handler:
on get_password()
-- get password and return true if good, false if bad
end get_password
This way you can 're-call' the handler from anywhere else in your script as needed.
Here is an example of your code that repeats the prompt for password as your question asked:
local goodpassword, password1, tryagain, proceed
set goodpassword to false -- initial set
set tryagain to true -- initial set
on getpassword()
return text returned of (display dialog "To continue please enter your special passcode below." buttons {"Cancel", "Continue"} default button 2 default answer "" cancel button 1 with hidden answer)
end getpassword
repeat while tryagain = true
set proceed to true
set password1 to getpassword() -- This calls the prompt for the first time
if the password1 is "passwordhere" then
set goodpassword to true
set tryagain to false
try
display dialog "You have entered the password right!
The right password was: passwordhere
The entered password was: " & password1 buttons {"Cancel", "Continue"} default button 2 cancel button 1
on error number -128
set proceed to false
end try
else
set goodpassword to false
if the password1 is "" then
set password1 to "Empty Passwordfield"
end if
try
display dialog "You have entered the password wrong!
The right password was: ********
The entered password was: " & password1 buttons {"Cancel", "Try Again"} default button 2 cancel button 1
on error number -128
set tryagain to false
end try
end if
end repeat
Related
I'm running an applescript program where I am asking for the user to type in their name. If the user clicks "OK", then the name gets stored to the variable I want. However, if the user clicks "Cancel", then the program just quits. How do I set this up to either hide the "Cancel" button, so it's not an option to click, or to set up a loop so that if cancel is clicked, it just continues to ask the user for his/her name until it's entered?
Thanks in advance.
display dialog "Please Enter Your Name." default answer " " with title "Enter Name"
simple solution
display dialog "Please Enter Your Name." default answer " " with title "Enter Name" buttons {"OK"} default button 1
Here's what I got:
display dialog "Please Enter Your Name." default answer "" buttons {"Submit"} with title "Enter Name" default button 1
if the button returned of the result is "Submit" then
set yourVariable to text returned of the result
end if
The way that you hide the cancel button is like this:
display dialog "Please Enter Your Name." default answer " " buttons {"ok"} with title "Enter Name"
set a to the text returned of the result
But if you wanted the repeat then use this (I have made it so they have to enter something too instead of just pressing ok to end it:
repeat
display dialog "Please Enter Your Name." default answer "" buttons {"ok", "cancel"} default button 1 with title "Enter Name"
set a to the text returned of the result
if the button returned of a is "ok" then
if the text returned of a ≠ "" then
exit repeat
end if
end if
end repeat
display dialog "Play game?" buttons {"Yes", "No"} with title "Play?"
if {button returned:"No"}
end if
I pressed compile to pretest and this happened:
Applescript error
Can’t make {button returned:"No"} into type boolean.
Try:
set buttonChoice to (display dialog "Play game?" buttons {"Yes", "No"} with title "Play?")
if button returned of buttonChoice = "No" then
return "no"
end if
I am working on the same app that I mentioned in my first question. I have gotten much farther, but when I try to play "2048" the first time, AppleScript give me the error:
"The variable result is not defined"
I will skip the main bulky body of the app and get to the area with the problem:
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
else
display dialog "Invalid response" with title "Invalid response" buttons {"Go back", "Quit"} default button 1
end if
if the button returned of the result is "Go back" then
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
else
return
end if
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
end if
Just set the result of the display dialog to a variable and use the variable. It's tricky to use "result" as you have in many if statements. One of them is bound to cause a problem because "result" will change at some point.
So do something like this right after your display dialog statement...
set {buttonReturned, textReturned} to {button returned of result, text returned of result}
Then in your if statements use buttonReturned or textReturned.
No matter what my password is, it always says my password isn't right! Help please. Here is the script
set my_password to display dialog ¬
"Please enter your password:" with title ¬
"Password" with icon stop ¬
default answer ¬
"" buttons {"Cancel", "OK"} default button 2 ¬
with hidden answer
if (text returned of my_password) is no then
display dialog "Running the application!" buttons ["OK"] default button 1
else if (text returned of my_password) is not no then
display dialog "Did not enter correct password!" buttons ["OK"] default button 1
end if
You must put the password (no) in quotes.
I also shortened the script. Try it out:
display dialog "Please enter your password:" with title "Password" with icon stop default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
if (text returned of result) is "no" then
display dialog "Running the application!" buttons ["OK"] default button 1
else
display dialog "Did not enter correct password!" buttons ["OK"] default button 1
end if
I am having an error in AppleScript when I use less than or greater than operators consecutively. I probably didn't explain that very well, so I will post the code.
**set good to false**
**repeat until good = true**
set oneTen to the text returned of (display dialog "Pick a number from 1 through 10" default answer "" buttons {"OK"} default button 1) as number
if oneTen is less than 0 then
display dialog "below" buttons {""} default button 1
else if oneTen is greater than 10 then
display dialog "above" buttons {""} default button 1
else
set good to true
end if
**end repeat**
I am trying to take the input from the prompt, and keep the user from entering anything below 0 or above 10. Could you post some code to do this well?
I want something similar to this.
**set oneTen to the text returned of (display dialog "Pick a number from 1 through 10" default answer "" buttons {"OK"} default button 1) as number**
**if oneTen is less than 0 or greater than 10 then**
**-- make them do the prompt again**
**end if**
Try:
repeat
set oneTen to the text returned of (display dialog "Pick a number from 1 through 10" default answer "" buttons {"OK"} default button 1) as number
if oneTen is less than 0 then
display dialog "below" buttons {""} default button 1
else if oneTen is greater than 10 then
display dialog "above" buttons {""} default button 1
else
exit repeat
end if
end repeat