2010-03-03 58 views
0

我想在我的AppleScript添加用戶身份驗證來執行一些任務的用戶認證。如果用戶名和密碼是正確的,那麼只有下一個任務應該發生。如何使用applescript做到這一點?添加使用AppleScript

回答

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 

保存爲只讀腳本,它會阻止普通用戶從看劇本的內容,但這不是真的安全可言,如果有人真的想要突破你的保護。

+0

不,我不想檢查這樣的用戶名和密碼。我想根據當前用戶的用戶名和密碼從系統中檢查用戶輸入的用戶名和密碼。 – Shakti 2010-03-06 11:47:18

0

如果您的Mac正在運行Leopard(Mac OS X 10.5+),那麼您可以使用system info命令的long user name屬性,該命令返回當前用戶的全名。說了這麼多,你可以這樣做......

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

如果你想要一個用戶名和密碼輸入,然後使用該命令:

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 
+0

對不起,我不知道如何檢查它對系統,系統密碼是加密的,所以它幾乎是無法得到。 – macscripter 2017-11-15 03:34:59