Automatic login ...

Hi,

I didn’t find anything related to my subject, maybe you can help … I want to put my computer to sleep when there is no activity and awake it when needed only … economical and ecological.

Background applications require computers to be awake. I have found a little utility called Power Manager (very useful in fact …) that helps me with this. It can put a computer to sleep, awake it and much more. But it also has it’s limitations. If the user is password protected it awakes the computer but returns it to sleep almost immediately if no login activity is initiated.

I run an application called Indigo that runs in background on a logged and non-passworded user. But since my normal day-to-day user is passworded, I am looking for a way, when inactive for a while, to change the frontmost user for the non-passworded user. This way, Power Manager would then be able to awake/sleep without the login problem.

Hope this is clear enough … Thanks in advance.

This is a shell script that might do what you want:

#!/bin/bash
idl=$"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`"
idletime="30"
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 "password"
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

It’s from post #83 in this thread:
http://forums.macosxhints.com/showthread.php?t=112&page=5

Hi,

I thank you for your help … but the script does not work in my ScriptEditor. I run OS 10.4.11.

Regards.

That’s because it’s not an Applescript. It’s a shell script that runs an Applescript. Copy/paste it into an empty text file, give the file an extension of “.sh” and then run these commands from the terminal:

chmod +x /path/to/file.sh
sleep 31
/path/to/file.sh

The first command makes the script executable. The second command causes the script to sleep for 31 seconds, after which the third command calls the script. If you haven’t moved the mouse or touched the keyboard in that 31 (change ‘idletime=’ to meet your needs) seconds, the script will switch users.

OK …

Once those commands will be executed from the terminal, will they be permanently active, meaning that whenever the IdleTime is TRUE, the system will switch user to 502 or whichever is specified? Is this correct ?

If yes, can I specify 2400 as the IdleTime ?
How do I interrupt these commands ?

Thanks for your help.

It only works when the script is running. You’d need to use launchd to call it periodically.

It is getting complex … and I am not familliar with this stuff. I would not want it to do things that could dammage my computer. I will give it a try further …

So, I understand that the only way to stop the execution of the script is to restart the computer. It could be automatically activated with what you call «launchd». Could you be more specific to get the script running on startup ? How do I get:

chmod +x /path/to/file.sh
sleep 31
/path/to/file.sh

to be runned automatically on startup ?

We are getting somewhere … Thanks in advance.

This is a complicated task for a newbee in shell scripts like me … :rolleyes::rolleyes::rolleyes:

My objective: I want the computer to automatically switch to the 508 user (it has no password) after there has been no activity (no mouse/keyboard click, no disk access) for 20 minutes. As requested, I created a text file only as follow:

#!/bin/bash
idl=$“ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'
idletime=“1200”
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

This file was named SwitchToUser508.sh and was saved at this location: Macintosh HD/Applications/Utilitaires/AppleScript 2.1.1/Création de scripts/Shell scripts.

I have a «launchd» script that I tried to save/load with Lingon. The script reads as follow:

chmod +x /Macintosh HD/Applications/Utilitaires/AppleScript 2.1.1/Création de scripts/Shell scripts/SwitchToUser508.sh
sleep 1200
/Macintosh HD/Applications/Utilitaires/AppleScript 2.1.1/Création de scripts/Shell scripts/SwitchToUser508.sh

I tried to save and load and it doesn’t seem to load. What am I doing wrong ?

I logged in at work as cwtnospam2 (couldn’t remember my password as my home browser keeps it for me) to answer this, but those posts have disappeared. :frowning:

The first two lines shouldn’t be there, and the last line needs the spaces ‘escaped’ like so:

/Macintosh\ HD/Applications/Utilitaires/AppleScript\ 2.1.1/Création\ de\ scripts/Shell\ scripts/SwitchToUser508.sh
Note the \ character before the spaces in the path.

The following line needs to be done once from the Terminal, as it makes the script executable:
chmod +x /Macintosh\ HD/Applications/Utilitaires/AppleScript 2.1.1/Création de scripts/Shell scripts/SwitchToUser508.sh
Note the \ character before the spaces in the path.

The sleep command causes a shell to pause, which in my example was supposed to demonstrate that the script works. If we use idletime=“30” as in the example, you could call the script from a Terminal window this way:

sleep 31; /Macintosh\ HD/Applications/Utilitaires/AppleScript\ 2.1.1/Création\ de\ scripts/Shell\ scripts/SwitchToUser508.sh

What this would do is wait 31 seconds, then call the script. If you haven’t moved the mouse or hit a key in that time, the script will switch users. There is no need for the sleep command when using Lingon, because you’re going to set Lingon to periodically call the script. If the system has been idle for more than 1200 seconds (in your example) when the script is called, then it will switch users. If it’s been idle for 1200 seconds or less, it won’t switch users, so you could have to wait as long as 2400 seconds for the system to switch users if you have Lingon call the script every 1200 seconds.

I am trying hard … :mad::mad::mad:

I simplified the file path, and placed the shell script in the first root level of my User (called “Nous”). There will be no accentuated characters (like «Ã©, è, etc …») to interfere with Terminal. The path to the shell script is now: Macintosh HD/Users/Nous/SwitchToUser508.sh. This is the content of the .sh file:

This is the content of my SwitchToUser508.sh file:

#!/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

Is it OK the put «“”» as the password since there is no password for the 508 user ?

I then runned the script below in Terminal:

chmod +x /Macintosh\ HD/Users/Nous/SwitchToUser508.sh
sleep 30
/Macintosh\ HD/Users/Nous/SwitchToUser508.sh

I got a «No such file or directory» message:

Nous:~ Nous$ chmod +x /Macintosh\ HD/Users/Nous/SwitchToUser508.sh
chmod: /Macintosh HD/Users/Nous/SwitchToUser508.sh: No such file or directory
Nous:~ Nous$ sleep 30

Since the system is localized in french, I also tested it with «Utilisateurs» instead of «Users», and got the same error message. I also tried to run the content of the SwitchToUser508.sh file directly in Terminal and got this result:

Nous:~ Nous$ #!/bin/bash
-bash: !/bin/bash: event not found
Nous:~ Nous$ idl=$“ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'
Nous:~ Nous$ idletime=“1200”
Nous:~ Nous$ 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

What is the problem ? Thanks again …

Browser: Safari 525.18
Operating System: Mac OS X (10.4)

It is ok to use “” as the password.

Type it this way in the terminal, with items in “()” being things you do, not type:

chmod +x (drag file from Finder window into Terminal window and drop it, then hit return)
(If that doesn’t result in an error, continue to next step)

ls -la ~/Switch* ( Hit return. Does that show permissions on the file containing three x’s? Should be something like: rwxr_xr_x If so, continue)

sleep 30; (Don’t hit return, instead, drag file from Finder window to Terminal window and drop, then in Terminal window, hit return and wait thirty seconds)

Hi cwtnospam,

Excuse being so long to return. I was out of town. I tried your suggestions … This is what I got with your 2 first statements:

Nous:~ Nous$ chmod +x /Users/Nous/SwitchToUser508.sh
Nous:~ Nous$ ls -la ~/Switch*
-rwxr-xr-x 1 Nous Nous 525 Jul 3 11:06 /Users/Nous/SwitchToUser508.sh
Nous:~ Nous$

The third statement did not return anything and did not initiate any user change; This is what I got:

Nous:~ Nous$ sleep 30/Users/Nous/SwitchToUser508.sh
Nous:~ Nous$

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 ?

We are getting closer …

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.

In the «SwitchToUser508.sh» file, the idletime is =“5”. In Terminal I entered:

chmod +x /Users/Nous/SwitchToUser508.sh
sleep 30 /Users/Nous/SwitchToUser508.sh

… 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 ?

Still searching …

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

The script works for me and others. It is from post #83 of this thread: http://forums.macosxhints.com/showthread.php?t=112&page=5

Still not working !! With what application do I have to save the «SwitchToUser508.sh» file ?

If you used Text Edit, be sure to make it plain text. An rtf file won’t work.

Are you getting an error? I just ran it using an invalid user id and got this error: NSReceiverEvaluationScriptError: 4 (1)

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 ?

Can you run t with a valid user # ?

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.