我想在我的AppleScript中添加用户身份验证来执行某项任务。如果用户名和密码正确,则只应执行下一个任务。如何使用applescript做到这一点?
答案 0 :(得分:0)
你还没有真正说出你想要多少安全性。
最基本的解决方案:
display dialog "Enter username:" default answer ""
set enteredUsername to text returned of result
display dialog "Enter password:" default answer ""
set enteredPassword to text returned of result
if enteredUsername is "CORRECTUSERNAME"
if enteredPassword is "CORRECTPASSWORD"
-- do something
end if
end if
将其保存为只读脚本,它会阻止临时用户查看脚本的内容,但如果有人真的想要突破您的保护,这根本不是真的安全。
答案 1 :(得分:0)
如果你的Mac运行Leopard(Mac OS X 10.5+),那么你可以使用long user name
命令的system info
属性,该命令返回当前用户的全名。话虽如此,你可以这样做......
set the current_user to (get the long user name of (system info)) as string
set the input_password to the text returned of (display dialog "Please enter the password for " & the current_user & ":" default answer "" buttons{"OK"} default button 1 with hidden answer) --The user cannot bypass this dialog.
--The 'hidden answer' property, when true, changes every character you type in the dialog box into ◘'s.
try
--This next line will verify that the password entered matches the current user's password.
do shell script "history -c" user name the current_user password the input_password with administrator privileges
on error --incorrect password
display dialog "Sorry. You entered the wrong password." buttons{"Cancel"}
end try
--Put whatever you want to do after authentication here.
如果您有任何疑问,请问我:)
答案 2 :(得分:-2)
If you are wanting a username and password input then use this:
set a to the text returned of (display dialog "Please enter your username" default answer "")
set b to the text returned of (display dialog "Please enter your password" default answer "")
if a="username" then --set the username to your username
if b="password" then --set the password to your password
--what ever is after your password protection
end if
end if