I’m fairly new to applescript and I’ve run into some trouble. I have a program that will be run as a folder action script to password protect a folder (I know, I know, its not infalliable, it’s purely theroetical) and I can’t seem to run the following code:
set correctPassword to 12345 as string --Sets 12345 to the desired password
set thePassword to display dialog "Enter Password:" default answer " " as string
if thePassword is correctPassword then --Starts the logic sequence for whether the password is correct or not
display dialog "Success!" --displays a success message is the password is correct
else --if the password is not correct
--tell application "Finder" --quits the finder if the password is not correct
--activate -- this and the next two lines are commented out for now for debugging purposes, it will just display "error" instead if the password is incorrect
--quit
--end tell
display dialog "Error."
end if --exits the if statement
No matter what is entered for the password, it runs as if it was incorrect.
I took just the first part for debugging purposes to see if it was working:
set thePassword to display dialog "Enter Password:" default answer " "
display dialog thePassword
After that I get the error message "Can’t make {“text returned} into string”. I understand strings and all just fine, but I can’t seem to figure out what’s wrong with this! Any help is GREATLY appreciated.
display dialog “Enter Password:” default answer “” – don’t put a space within the quotes
into Script Editor and look at the result, you get a result of something like this:
{text returned:“12345”, button returned:“OK”}
Here I entered “12345”. To get a field value something like this:
set correctPassword to “12345”
display dialog “Enter Password:” default answer “” – don’t put a space within the quotes
set the_password to text returned of result
if the_password is correctPassword then
beep 3
else
beep 1
end if
‘result’ is an AppleScript reserved variable. It’s value is the result of the last command.
If you say
set myPassword to display dialog “What is the password” default answer “12345”
you’ll get a dialog box with 12345 in the text entry section and buttons Cancel and OK with the OK highlighted as the default.
If you enter ABCDE and press the OK button (or return or enter), the dialog will return this record to your script as the result and put that in myPassword (which cannot be coerced into text): {text returned:“ABCDE”, button returned:“OK”}
You extract the answer you want this way:
set myPassword to the text returned of display dialog, etc, etc.
If you want to check first whether the cancel button was pressed, you’ll have to use something like this:
set myAnswer to display dialog “my question” etc.
set myPasswd to text returned of myAnswer
set myButton to button returned of myAnswer
if myButton is “OK” then
– do what you want with myPasswd
else
– do something about the cancel choice
end if