Adding user Authentication using Applescript
I want to add user authentication in my applescript to perform some task. If the username and password is correct then only 开发者_如何学Cthe next task should happen. How to do this using applescript?
You haven't really said how much security you want.
The most basic of solutions:
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
Save it as a read-only script and it'll prevent casual users from looking at the contents of the script, but this is not really secure at all if someone really wants to break through your protection.
If your Mac is running Leopard (Mac OS X 10.5+), then you could use the long user name
property of the system info
command, which returns the full name of the current user. Having said that, you could do this...
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.
If you have any questions, just ask me :)
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
精彩评论