Dropping the file in Terminal showed that the file path was not good. It was shorten to «/Users/Nous/SwitchToUser508.sh». I also tried it in Ligon, and this time the shell script loaded, probably because the file path was good.
Could there be a simple modification to do to the «SwitchToUser508.sh» file ?
If you have idletime=“5” in the script, and in a Terminal window you typed:
sleep 30; /path/to/script
then it should have tried to switch users.
If you still have idletime=“1200” in the script, then the response was correct. Because 30 is less than 1200, the script will simply quit when called. It will not execute any of the commands between “if [ $idl -gt $idletime ]; then” and “fi”
Edit:
IMPORTANT! If you do have idletime=“5” in the script, be sure to not move the mouse or touch the keyboard after hitting return, as that will reset idl to 0 each time you do and the clock will start counting again from there. Pressing a key or moving the mouse in the last 5 seconds of the sleep command will cause the script to quit.
… I did not move anything and there was no user switch. Again, the SwitchToUser508.sh file is:
#!/bin/bash
idl=$“ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'”
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
Could there be an error in the in the script of the «SwitchToUser508.sh» file ?
Can you explain what each statement in Terminal means ?
chmod +x /path/to/file makes the file executable. This only needs to be done once. You don’t need to do it again.
Since idletime is set to 5, you don’t have to sleep 30, but you need a semicolon after the sleep command. The command you want to use is:
sleep 7; /Users/Nous/SwitchToUser508.sh
In TextEdit I cannot save it in «plain text». I do not have that choice in the Save As menu. The only choices I have are: DOC, HTML, RTF and XML. The file I use is a MS Word «text only» file.
Running the script in Terminal does not end with an error message, but there is no user switch. The link you suggest in MacOsHints also seems to have problems to configure the automatic user switch.
Is all this setup compatible with Tiger or only Leopard ?
There’s the problem! In Text Edit, select “Make Plain Text” from the “Format” menu. Then save the file, rename it with a “.sh” extension, and do the chmod +x on it. You should be able to run it now.
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