AppleScript password checking - macos

I'm trying to get simple user authentication going via AppleScript. The goal is to have the passwords check against each other and then continue on with the rest of the script. Right now the script can recognize mis-matched password if password_initial is correct and password_final is incorrect, but if password_initial is incorrect and password_final is correct there is no logic to check against itself.
set user_name to ""
display dialog "Please enter your user name" default answer "firstname.lastname"
set the user_name to the text returned of result
set password_initial to ""
display dialog "Please enter your password:" default answer "" with hidden answer
set password_initial to the text returned of result
display dialog "Please verify your password below." buttons {"OK"} default answer "" with hidden answer
set password_final to text returned of result
considering case
repeat while password_final is not equal to password_initial
display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with hidden answer
set password_final to text returned of result
end repeat
end considering
--do something
Can anyone point me in the right direction on this? Thanks!

Whoa whoa whoa, handlers and global variables? No need for all that, why not just throw the whole thing into a loop and then break it when we get what we want?
set user_name to text returned of (display dialog "Please enter your user name" default answer "firstname.lastname")
set display_text to "Please enter your password:"
repeat
considering case
set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
if (final_pass = init_pass) then
exit repeat
else
set display_text to "Mismatching passwords, please try again"
end if
end considering
end repeat
#Rest of script here...

The trick with something like this is to use Handlers.
These are bits of code that can be called to run within your script as many times as you want and save on rewriting the same code over again.
They can also help you not use repeat loops like you are doing. Also you should always add the 'cancel' button to your display dialogs. If there is bad logic in a repeat loop or a handler call the user needs a way to exit it.
I have also made some of the display dialog text dynamic. and used some global variables
I have tested this code which has Handles and the calls to them and it works well in my testing. But it is an example and should give you enough to move forward.
set displays to {"Please enter your password:", "Please verify your password below.", "Passwords do not match, please re-enter your password below."}
set user_name to add_user_name() #get user name
set thedisplay to item 1 of displays #set the fisrt dialog for the password display to the first item in displays
global thedisplay, displays, password_initial #global variables
set password_final to setDetails() #Call to start the password dialogs
--your code here ..
#HANDLERS
on setDetails()
set password_initial to add_password() #call to get user password
set password_final to verify_password() #call to get verify password
end setDetails
on add_user_name()
display dialog "Please enter your user name" buttons {"Cancel", "OK"} default answer "firstname.lastname"
set the user_name to the text returned of result
return user_name
end add_user_name
on add_password()
display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer
set password_initial to the text returned of result
return password_initial
end add_password
on verify_password()
set thedisplay to item 2 of displays #set the dialog for the password verify display to the second item in displays
display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer
set password_final to text returned of result
considering case
if password_final is not equal to password_initial then
set thedisplay to item 3 of displays #set the dialog for the password verify display to the third item in displays
my setDetails() # start over again asking for password as it did not does not match dialog displays will also change accordingly
else
set thedisplay to item 2 of displays #set the dialog for the password verify display to the second item in displays
end if
end considering
return password_final
end verify_password

Related

Script editor creating a folder and naming it

So I've been trying to write an AppleScript script that would create a folder with an input of user, but unfortunately I can't get it working. Any help is appreciated.
set theResponse to display dialog "Enter the name of folder" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
display dialog "Name of the folder: " & (text returned of theResponse) & "."
tell application "Finder"
make new folder at desktop with properties {name:"theResponse"}
end tell
Here's a modified version of your code that will work:
set theResponse to text returned of (display dialog "Enter the name of folder" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue")
display dialog "Name of the folder: " & theResponse & "."
tell application "Finder"
make new folder at desktop with properties {name:theResponse}
end tell
In your original code, {name:"theResponse"}, with "theResponse" in quotes, it's literally theResponse not the text returned.
So, In the first line of code, I started off with setting the theResponse variable to the text returned, so it need not be referenced as such later in the script.
Thus (text returned of theResponse) in the second line of code is now set to just theResponse, which contains the value of that variable.
Now when it comes time to make new folder the name property is the value of theResponse, without quotes, and it already contains the text returned from the first line of code.

How do I set a variable to the text returned? - Applescript

How can I set a variable to the text returned?
text returned from:
display dialog "Type something" default answer "" buttons {"Done"} default button 1
Automatic code after that:
copy the result as list to {text_returned, button_pressed}
Variable code:
set thing to text returned of the result
Error Message:
Script Error
Can't get text returned of {"Done","kjhfsddfhksh"}.
I also tried other thing like that^, but they also didn't work.
You seem very close. This works for me:
display dialog "Foo?" default answer "Yes"
set theText to text returned of the result
If you're doing anything between the display dialog and accessing the result then the result may no longer be set.
This is what I got:
display dialog "Password" default answer ""
set w to text returned of the result
if w = "Password" then
display notification w
end if

Applescript: Textedit Password

I'm new to Applescript. I am stumped on this problem of how to read and write a text file and use the contents as a variable. I have done research on this, but nothing works or makes sense. I want to read a text file, which would contain a word or numbers. The word or numbers, let's say 123, would be assigned to a variable called pass. I need an Applescript to ask the user what the password should be, then make a new text file with the password on it. I also need an Applescript to change the password. The following Applescript will be for changing the password.
set theFile to "Users:username:Desktop:pass.txt" as alias
set pass to (read file theFile)
display dialog "Password:" default answer "" with hidden answer
if text returned of result is pass then
display dialog "New Password:" default answer "" with hidden answer
display dialog "Again:" default answer "" with hidden answer
-- code here to change text in pass.txt
display dialog "Password changed."
end if
I just need a starter, or a useful website, or anything that can help me. Thanks!
You can do this with plain Applescript Standard Additions. You find the handlers inside the File Read/Write section.
set theFile to "Users:Username:Desktop:pass.txt" as alias
set pass to (read theFile)
display dialog "Password:" default answer "" with hidden answer
if text returned of result is pass then
set newPass1 to text returned of (display dialog "New Password:" default answer "" with hidden answer)
set newPass2 to text returned of (display dialog "Again:" default answer "" with hidden answer)
-- check the new passwords
if newPass1 = newPass2 and newPass1 ≠ "" then
-- open the file
set pwdFile to open for access theFile with write permission
-- delete the content
set eof of pwdFile to 0
-- write new content
write newPass1 to pwdFile
-- close the file
close access pwdFile
display dialog "Password changed."
end if
end if
BTW: read theFile does not need a file, your alias theFile is enough!
BTW: I hope this script is for learning purposes only. I strongly recommend not to handle your user's passwords this way...!
Enjoy, Michael / Hamburg

On error re-open last dialog

I have a dialog which opens to input and store user input(name).
Upon error (which is is either due to no name entered or the name already exists) i want the dialog to re-open. Finally, if the second attempt fails again, open a dialog which states that and exit.
The problem is, whether the name exists or does not, all 3 dialogs are ALWAYS displayed.
What am i missing?
try
display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
on error errorMessage number errorNumber
end try
try
display dialog "Specify a DIFFERENT folder name:" default answer "John The Dog12"
set newName to (text returned of result)
on error errorMessage number errorNumber
end try
try
display dialog "NAME ALREADY EXISTS! The program will now exit." with icon caution buttons {"EXIT"}
end try
Thanks!
Try this. Note I did not test this but it should work. Just put this code in place of your code where you try to get the folder name.
You can basically create as many dialogTexts/defaultAnswers combinations as you want and it will work. Good luck.
-- get the names of all the folders in the targetFolder
tell application "Finder" to set targetFolderNames to name of folders of folder targetFolder
set dialogTexts to {"Specify a new folder name:", "Specify a DIFFERENT folder name:"}
set defaultAnswers to {"John The Dog", "John The Dog12"}
repeat with i from 1 to count of dialogTexts
display dialog item i of dialogTexts default answer item i of defaultAnswers
set newName to (text returned of result)
if newName is not "" and newName is not in targetFolderNames then exit repeat
if i is (count of dialogTexts) then
display dialog "NAME ALREADY EXISTS! The program will now exit." with icon caution buttons {"EXIT"} default button 1
return input
end if
end repeat
-- do something with newName

applescript icon problem (button_pressed is not definded)

this script works fine until the commands of the buttons, can someone tell my why i doesent work?
it says "button_pressed is not definded"
display dialog "bla" with icon alias ((path to me) & "Contents:Resources:my.icns" as string) buttons {"blu", "bli", "blaa"} default button 3
if the button_pressed is "blu" then
-- action for 1st button goes here
say "blu"
else if the button_pressed is "bli" then
-- action for 2nd button goes here
say "bli"
else
-- action for 3rd button goes here
say "bla"
end if
The appropriate way to do this is to use button returned:
display dialog "bla" with icon alias ((path to me) & "Contents:Resources:my.icns" as string) buttons {"blu", "bli", "blaa"} default button 3
set theResponse to button returned of the result
if theResponse is "blu" then
-- action for 1st button goes here
say "blu"
else ...
The error occurs because the variable button_pressed is NOT defined. All you have to do is add this line of code before the if block and it should work!
set the button_pressed to the button returned of the result
Variables (i.e. button_pressed) must ALWAYS be defined before they can be uesd. For example, this code will not function...
display dialog greeting --> ERROR
...while this one will:
set greeting to "Hello! I am now a defined variable!"
display dialog greeting

Resources