Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Store the passwords in the Keychain' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Store the passwords in the Keychain
Authored by: wackazong on Apr 08, '08 02:35:20PM

Said, done.

Here you go (just the script)

<code>
--get the name of the user to switch to from the file name
set N to name of (info for (path to me))
set AppleScript's text item delimiters to (".")
set N to first text item of N
set AppleScript's text item delimiters to ""

--get the password out of the keychain
tell application "Keychain Scripting"
set myKeyChain to keychain "login.keychain"
unlock myKeyChain
set theKeyList to every key of myKeyChain
repeat with x from 1 to (length of theKeyList)
set theKey to item x of theKeyList
if the name of theKey is N and the description of theKey is "User Login" then
set thePassword to the password of theKey --grab the password
set theUserID to the account of theKey --grab the userid
exit repeat
end if
end repeat
end tell

--get the uid
set N to do shell script "/usr/bin/id -u " & N

--do the switch
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & N
tell application "System Events"
--use universal access to enter the text and to click the button
tell process "SecurityAgent" to set value of text field 1 of group 1 of window 1 to thePassword
click button 2 of window 1 of application process "SecurityAgent"
end tell
</code>

1) Enable access for assistive devices
2) Store a key in the login keychain for each user you want to switch to. Choose "New Password", enter the username as the account, and the password as the password. Save, open again and change the description to "User Login" (you only need to do this once.
3) Enjoy secure Fast User Switching



[ Reply to This | # ]
Store the passwords in the Keychain
Authored by: wackazong on Apr 09, '08 06:48:52AM

I have now developed a variant where you can name the script either "Switch to username" or "Log out to username". If you choose "Log out to", then the current user is logged out (gracefully) after the switch (does not work of course if programs cancel the logout".

One question: To log out, I use a do shell script command to open another applescript which logs the user out after about 10 seconds (after the switch has occured). There must be a more elegant solution to this, at the moment I have to keep a separate aplication called "Log Out", which does the logout. Any ideas to contain this into one Script file?

The problem is that if you use e.g. osascript (even with &) then the logout is executed before the script can continue to the user switch section.

The Script

on run
try

--get the name of the user to switch to from the file name
set N to name of (info for (path to me))
set AppleScript's text item delimiters to (".")
set N to first text item of N
set AppleScript's text item delimiters to ""
--get the username, depending on whether the prefix is "Log out to " or "Switch to "
if (characters 1 thru 3 of N) as text is "Log" then
set N to ((characters 12 thru (length of N)) of N) as string
set doLogOut to true
else
set N to ((characters 11 thru (length of N)) of N) as string
set doLogOut to false
end if

--get the password out of the keychain
set thePassword to ""
tell application "Keychain Scripting"
set myKeyChain to keychain "login.keychain"
set theKeyList to every key of myKeyChain
repeat with x from 1 to (length of theKeyList)
set theKey to item x of theKeyList
if the name of theKey is N and the description of theKey is "User Login" then
set thePassword to the password of theKey --grab the password
exit repeat
end if
end repeat
end tell

if thePassword is not "" then

DoSwitch(N, thePassword, doLogOut)

else

--get the password
repeat
display dialog "Enter the password for the targeted account:" default answer "" with icon note with title "User Password" with hidden answer
set the password_01 to the text returned of the result
display dialog "Enter the password for the targeted account again:" default answer "" with icon note with title "User Password" with hidden answer
set the password_02 to the text returned of the result
if password_01 is not password_02 then
display dialog "The two passwords do not match. Try again." with icon stop with title "User Password"
else
exit repeat
end if
end repeat

--make a new key
tell application "Keychain Scripting"
set myKeyChain to keychain "login.keychain"
make new generic key at myKeyChain with properties {name:N, description:"User Login", account:N, password:password_01}
end tell

DoSwitch(N, thePassword, doLogOut)

end if

on error error_message number error_number
display dialog error_message with icon 2 buttons {"Cancel"} default button 1
end try
end run

on DoSwitch(N, P, doLogOut)

--get the uid
set N to do shell script "/usr/bin/id -u " & N

--do the switch
if doLogOut then
do shell script "open '/Applications/Utilities/Switchers/Log Out.app'"
end if
tell application "System Events"
do shell script "/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & N
delay 0.2
repeat until exists process "SecurityAgent"
delay 0.2
end repeat
--use universal access to enter the text and to click the button
tell front window of process "SecurityAgent"
set value of text field 1 of group 1 to P
click button 2
end tell
end tell

end DoSwitch

The logout script

delay 10
tell application "System Events"
keystroke "q" using {command down, shift down, option down}
end tell



[ Reply to This | # ]