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