If you want to leave the password blank you can comment out the password portion of the Applescript with – like so:
#!/bin/bash
idl=$“ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'”
echo $idl
idletime=“5”
if [ $idl -gt $idletime ]; then
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID 502
/usr/bin/osascript <<EOT
delay 5
set password_ to “()”
tell application “System Events”
delay 1 --tell process “SecurityAgent” to set value of text field 1 of group 1 of window 1 to password_ click button “Log In” of window 1 of application process “SecurityAgent”
end tell
EOT
fi
Can you run successfully the script on your computer ? I tried it with a functional passworded user and it did not work either … Maybe somebody else out there that can use this shell script from within Terminal could help ?
Yes, it works for me. So does the Applescript below. If it works for you, all you need to do is edit SwitchToUser.sh to contain the user id and password that you want. Then you only need to call the shell script ~/SwitchToUser.sh either from a do shell script or from Lingon (launchd).
set x to quoted form of "#!/bin/bash
idl=$\"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`\"
echo $idl
idletime=\"5\"
if [ $idl -gt $idletime ]; then
/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID 502
/usr/bin/osascript <<EOT
delay 5
set password_ to \"\"
tell application \"System Events\"
delay 1
--tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to password_
click button \"Log In\" of window 1 of application process \"SecurityAgent\"
end tell
EOT
fi"
do shell script "echo " & x & " > ~/SwitchToUser.sh"
do shell script "chmod +x ~/SwitchToUser.sh"
do shell script "sleep 7; ~/SwitchToUser.sh"
Note that this script does everything for you, including test the SwitchToUser script, so after you click run don’t touch the keyboard or mouse for at least 7 seconds.
We are getting definitively somewhere. This is a shell script beginner 101 course ;);) Just to be sure … I have tested also a passworded user. I understand that :
¢ any reference to the user number and user password have to be modified in the AppleScript and in the «SwitchToUser508.sh» file
¢ I have no other changes to do to my «SwitchToUser508.sh» file
¢ in your AppleScript I have to modify your «~/SwitchToUser.sh» with my file path that is «/Users/Nous/SwitchToUser508.sh».
¢ I have to paste the AppleScript in my Script Editor and run it
That is what I did. When testing a passworded user, OS X prompted me to enter the password. Then when I switched back to the Script Editor there was this error message:
109:170: execution error: System Events got an error: NSReceiverEvaluationScriptError: 4 (1)
When testing the non passworded user the automatic switch finally worked but the Script Editor had also an error message:
101:162: execution error: System Events got an error: NSReceiverEvaluationScriptError: 4 (1)
Still a few questions:
how can I switch automatically without having to enter a password for a passworded user ?
what are these error message about ?
since I am less familiar with shell scripts, can I manage the user switch with a ON IDLE background AppleScript instead ?
Apparently you don’t have a user 502, so try this version that uses 508:
set x to quoted form of "#!/bin/bash
idl=$\"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`\"
echo $idl
idletime=\"5\"
if [ $idl -gt $idletime ]; then
/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID 508
/usr/bin/osascript <<EOT
delay 5
set password_ to \"\"
tell application \"System Events\"
delay 1
--tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to password_
click button \"Log In\" of window 1 of application process \"SecurityAgent\"
end tell
EOT
fi"
do shell script "echo " & x & " > ~/SwitchToUser.sh"
do shell script "chmod +x ~/SwitchToUser.sh"
do shell script "sleep 7; ~/SwitchToUser.sh"
You can’t switch to a user who has a password without using that password. That’s part of what they’re for!
I got the user number from the info window on a process belonging to that user in Activity Monitor. So yes, to my knowledge that 508 user exists. Since the switch to 508 worksnow from the ScriptEditor, the user number should be good …
My test were done with the «tell process» statement active (in both the AppleScript and the .sh file). It was not converted in a note with «–».
What is the point of all the code referring to the password if I have to enter it manually ? I understand tough that it is not security proof … :|:|:|
I desactivated both «tell process» statements. The switch worked again but I got a different error message:
165:237: execution error: System Events got an error: NSReceiverEvaluationScriptError: 4 (1)
set x to do shell script "nidump passwd . | grep 'Users'"
display dialog x
You should get something like:
UserOne::501:20::0:0:xxxxxxxxxxxxxxxxxx
UserTwo::502:20::0:0:xxxxxxxxxxxxxxxxxx
UserThree:********:504:504::0:0:xxxxxxxxxxxxxxxxxx
The user number is the first one to the right of the user name.
Use that number in place of 508 in this script without changing anything else:
set x to quoted form of "#!/bin/bash
idl=$\"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`\"
echo $idl
idletime=\"5\"
if [ $idl -gt $idletime ]; then
/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID 508
/usr/bin/osascript <<EOT
delay 5
set password_ to \"\"
tell application \"System Events\"
delay 1
--tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to password_
click button \"Log In\" of window 1 of application process \"SecurityAgent\"
end tell
EOT
fi"
do shell script "echo " & x & " > ~/SwitchToUser.sh"
do shell script "chmod +x ~/SwitchToUser.sh"
do shell script "sleep 7; ~/SwitchToUser.sh"
The script should get to the login screen and the login should fail if a password is needed.
After checking, the user number I am trying to switch to is really 508. I runned your AppleScript, but to have it running, I had to modify the path to the .sh file as previously:
do shell script “echo " & x & " > /Users/Nous/SwitchToUser508.sh”
do shell script “chmod +x /Users/Nous/SwitchToUser508.sh”
do shell script “sleep 7; /Users/Nous/SwitchToUser508.sh”
Is this OK ? Got the same result and the same error messsage:
165:237: execution error: System Events got an error: NSReceiverEvaluationScriptError: 4 (1)
No. If you change the path, it trys to run your script file, which is the one that isn’t working. (It could work, but that just adds another variable that we don’t need.) Run it exactly the way I have it and report any errors. You can also check in your home folder to verify that it created a file called:
SwitchToUser.sh
Also, and this is very important, did you run this script to verify the user?
set x to do shell script "nidump passwd . | grep 'Users'"
display dialog x
Yes … and the user number I am trying to switch to is really 508 … I runned your AppleScript as requested. It did create a file in my home folder and I got the same error message:
181:253: execution error: System Events got an error: NSReceiverEvaluationScriptError: 4 (1)
I tried it on a passworded user (I only have one non-passworded user) and the switch occured asking me for the password. But still it gave the same error message:
181:253: execution error: System Events got an error: NSReceiverEvaluationScriptError: 4 (1)
set x to quoted form of "#!/bin/bash
idl=$\"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`\"
echo $idl
idletime=\"5\"
if [ $idl -gt $idletime ]; then
/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID OTHERUSERID
/usr/bin/osascript <<EOT
delay 5
set password_ to \"OTHERUSERPASSWORD\"
tell application \"System Events\"
delay 1
tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to password_
click button \"Log In\" of window 1 of application process \"SecurityAgent\"
end tell
EOT
fi"
do shell script "echo " & x & " > ~/SwitchToUser.sh"
do shell script "chmod +x ~/SwitchToUser.sh"
do shell script "sleep 7; ~/SwitchToUser.sh"
If that works, try it with userid 508 and the password set to:
the error says quite clearly that “System Events” got an error.
Therefore the error occurs in this part
.
tell application \"System Events\"
delay 1
--tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to password_
click button \"Log In\" of window 1 of application process \"SecurityAgent\"
end tell
.
maybe just “Enable access for assistive devices” is not checked in SysPrefs > Universal Access
I use this script to log in,
it works very reliable, because it waits controlled until the process exists
property pass_word : "¢¢¢¢"
tell application "System Events"
repeat until exists process "SecurityAgent"
delay 0.5
end repeat
tell window 1 of process "SecurityAgent"
set value of text field 1 of group 1 to pass_word
click button 2
end tell
end tell
set x to quoted form of "#!/bin/bash
idl=$\"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`\"
echo $idl
idletime=\"5\"
if [ $idl -gt $idletime ]; then
/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID OTHERUSERID
/usr/bin/osascript <<EOT
delay 5
set password_ to \"OTHERUSERPASSWORD\"
tell application \"System Events\"
repeat until exists process "SecurityAgent"
delay 0.5
end repeat
tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to password_
click button \"Log In\" of window 1 of application process \"SecurityAgent\"
end tell
EOT
fi"
do shell script "echo " & x & " > ~/SwitchToUser.sh"
do shell script "chmod +x ~/SwitchToUser.sh"
do shell script "sleep 7; ~/SwitchToUser.sh"