我是Applescript的新手。我对这个如何读写文本文件并将内容用作变量的问题感到困惑。我已对此进行了研究,但没有任何作用或有意义。我想读一个包含一个或多个单词的文本文件。一个或多个数字,比方说123,将被分配给一个名为pass的变量。我需要一个Applescript来询问用户密码应该是什么,然后创建一个带有密码的新文本文件。我还需要一个Applescript来更改密码。以下Applescript将用于更改密码。
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
我只需要一个入门者,一个有用的网站,或任何可以帮助我的东西。谢谢!
答案 0 :(得分:0)
您可以使用普通的Applescript 标准添加来执行此操作。您可以在文件读/写部分找到处理程序。
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
不需要文件,你的别名 theFile 就够了!
BTW:我希望这个脚本仅用于学习目的。我强烈建议不要以这种方式处理用户的密码......!
享受,迈克尔/汉堡