Automatic login ...

One more thing… :wink:

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

hi cwtnospam,

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 ?

With the statement you told me tu use:

sleep 7; /Users/Nous/SwitchToUser508.sh

I get a «cannot execute binary file» error …

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 ;);):wink: 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:

  1. how can I switch automatically without having to enter a password for a passworded user ?
  2. what are these error message about ?
  3. since I am less familiar with shell scripts, can I manage the user switch with a ON IDLE background AppleScript instead ?

Thanks for your patience …

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!

For the non passworded user, my tests were done with 508 and not 502 … I still get the same error message:

101:162: execution error: System Events got an error: NSReceiverEvaluationScriptError: 4 (1)

Do I have to put the «()» in the AppleScript like this:

set password_ to \"()\"

instead of

set password_ to \"\"

The password_ variable isn’t being used since the line that uses it is commented out:

    --tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to password_

Are you sure you have a user 508? I don’t, and I get that exact error if I try to use the script with that user number.

To see what users you have, run this script:

set x to do shell script "nidump passwd . | grep 'Users'"
display dialog x

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)

You can’t always trust Activity Monitor. :frowning:

Runt this script:

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.

Got to go. I’ll check in later. :wink:

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)

Try one of your other user numbers. You can get them from the short script above.

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)

In that case, try it this way:

 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:

set password_ to ""

Hi,

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

It is already enabled …

Doh! It’s been so long since I turned that on that I never would have thought about it.</smacks head>

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

That would make my script:

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"