Using a ScreenSaver over the LoginWindow

From Provider Wiki

Jump to: navigation, search


Contents

Using a ScreenSaver over the LoginWindow

NOTES:

  • This was tested primarily on 10.4.5, but should work for 10.3.x and 10.4.x.
  • This hint also relies on a number of undocumented features, and so should be considered a hack, and thus subject to break under new versions.
  • I am assuming that the reader is comfortable on the command line.
  • We will be periodically be running a script as root, so you need to make sure you secure this file properly. Otherwise this becomes a route for someone to take over your computer.

MacOS X includes a number of beautiful screen-savers, but they only apply when a user is logged in. You can set the screen to blank completely after a set period of time, but for some situations you might want to have the screen-saver activated instead.

This setup will only run the screen saver when the login window is displayed, and the computer has been idle for a certain amount of time.

Installation

You will need to have an administrator password that is capable of using 'sudo'. Due to a bug in 10.4 this means that you have to be a "local admin".

There are three things that need to be changed for this to work:

  • A shell script that does most of the work
  • A crontab entry for root that will cause the script to run
  • A few (optional) preference settings

Installing the shell script

I am going to make two assumptions for this section:

  • You will use the same location for your script as I do, namely: /Library/Scripts/UserScripts/ (you will have to create this folder)
  • You will name the script the same thing I do: loginwindowScreenSaver.bash

Here is the contents of the new file (loginwindowScreenSaver.bash) you should create in the /Library/Scripts/UserScripts/ folder (you will probably have to create this folder as well):

#!/bin/bash

screenSaverEngine='/System/Library/Frameworks/ScreenSaver.framework/Versions/Current/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine'

# first we check to see if we are at the loginwindow
if (( `/bin/ps -awwxco command,user | /usr/bin/grep '^loginwindow ' | /usr/bin/grep -c 'root'` > 0 )); then
	# next we look to see if the screensaver is already running
	if (( `/bin/ps -awwxco command,user | /usr/bin/grep -c '^ScreenSaverEngin '` < 1 )); then
		# and finally, we check to see that we have been idle long enough
		minIdleTime=`/usr/bin/defaults -currentHost read com.apple.screensaver idleTime`
		if [ -z "$minIdleTime" ]; then
			minIdleTime=900
		fi
		idleTime=`/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {print $NF/1000000000; exit}'`
		idleTime=`/bin/echo "scale=0; $idleTime/1" | /usr/bin/bc`
		if (( $idleTime > $minIdleTime )); then
			moduleName=`/usr/bin/defaults -currentHost read com.apple.screensaver moduleName`
			if [ -n "$moduleName" ]; then
				$screenSaverEngine -module $moduleName &
			else
				$screenSaverEngine &
			fi
		fi
	fi
fi

# note that this generates some complaints, but does work

You will also have to make sure that root has the ability to run this file, so with sudo run this:

sudo chown root /Library/Scripts/UserScripts/loginwindowScreenSaver.bash
sudo chmod 755 /Library/Scripts/UserScripts/loginwindowScreenSaver.bash

SECURITY ALERT: This script will be running as root, so you need to make sure that only people you trust can write to this file!

The Crontab Entry

The script that you have installed will need to be run on a regular basis (I have chosen every 5 minutes). The script is very light-weight, so this should not really affect your computer at all. To do this we will add a line to root's crontab file. In 10.4 the crontab file does not exist by default, so you will be editing a blank file.

From the command line type:

sudo crontab -u root -e

This should bring up either blank window with tildes along the left hand side, or possibly other crontab entries, such as the one that Norton Antivirus puts in there. Use the arrow keys to get near the end of the last line of the file, and then push "i" to go into "insert mode" (this is a version of the "vi" editor, and is a bit arcane).

Now that you are in "insert mode" go to the end of the last line and press return a few times to get to a new line. You will then want to enter the following lines:

# Entered by <your name here> on <put the date here>
*/5	*	*	*	*	/Library/Scripts/UserScripts/loginwindowScreenSaver.bash

The first line is just a comment (and you should put your own information there), and is a good idea so that others who might be modifying this later know who has been doing what. The spaces between the *'s should be tabs (but can be single spaces... it just looks better with tabs).

Once this looks right press the 'esc' key to get out of "insert mode" and then press ":qw" and the return key to quit and save changes. Hopefully you will not get any errors.

Running this every 5 minutes means that there is some uncertainty about how long the screen-saver will kick in. Using the defaults that I have here it could be anywhere between 15 and 20 minutes after the computer is idle at the login window.

Setting Your Preferences

Now everything should be up and running, but with default preferences (a 15 minute idle time, and the default screen-saver). If you would like to change this there are two ways of doing so:

You can type:

sudo 'sudo /Applications/System\ Preferences.app/Contents/MacOS/System\ Preferences'

and use the GUI to set the screen-saver preferences. (remember: these settings only apply to the login window)

Or you can set them competely on the command line with these commands:

defaults -currentHost write com.apple.screensaver idleTime -int 900
defaults -currentHost write com.apple.screensaver moduleName 'Nature Patterns'

NOTES:

  • If you use a third-party screen-saver you need to make sure you have installed it for "all users"
  • The "moduleName" is exactly what you see in the System Preferences window
  • "idleTime" is measured in seconds.

Summary

As a review, there are a few things to remember about this hint:

  • This only effects the computer when it is idle (no one touching mouse or keyboard) at the login window. This specifically does not affect the computer when someone is logged in.
  • I have tested this primarily with 10.4.5 (both PPC and Intel), but it should work with 10.3.x and 10.4.x.
  • There is a script that is run by root involved, so you should pay attention to security.
  • Using this hint exactly as written will mean that the screen saver kicks in from 15 to 20 minutes after the computer becomes idle at the login window.

--Larkost 14:39, 28 March 2006 (EST)

Personal tools