A script to manage the desktop screensaver

Aug 30, '02 05:06:30AM

Contributed by: foon

I loved the Screensaver in the Background hint, but I was tired of typing...

nice -n 20 /System/Library/Frameworks/ScreenSaver.framework/Resources/
ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background -module Random &
...every time I wanted to use it. So I wrote a little shell script to make it easier for me. I know it's not programmatically correct; this is because I'm lazy and this was at first only for my own use. It works, and that's what counts. If someone wants to take the time to correct it and post, feel free. Read the rest of the article for the script...

To install the script:

  1. Copy all of the text below to your clipboard
  2. Open a terminal window, and type sudo pico /usr/bin/saver. You'll probably have to get rid of line breaks put in for web display.
  3. Paste it all in
  4. Type control-x
  5. Type sudo chmod 755 /usr/bin/saver
  6. Type rehash
Now you're good to go. As the code implies, type saver -h to see your options.

[Editor's comment: You may not wish to save this to /usr/bin, where it may get overwritten by future updates from Apple. Instead, try putting it in a "bin" directory in your user's folder, or in /usr/local/bin, and then make sure that directory is in your $PATH variable; see this hint for a discussion on how to set the $PATH variable.]

The script:
#!/bin/sh

module=Random

if [[ $# -ne 0 ]]
then
if [[ $1 = -h ]]
then
echo "Usage:"
echo " saver random module"
echo " saver -h this help screen"
echo " saver -l list available modules"
echo " saver -k kill the running module"
echo " saver -m Modname use module Modname"
exit 0
fi

if [[ $1 = -l ]]
then
user=`users`
ls /Users/$user/Library/Screen\ Savers/ | grep .saver
ls /Library/Screen\ Savers/ | grep .saver
exit 0
fi

if [[ $1 = -m ]]
then
module=$2
fi

if [[ $1 = -k ]]
then
if [[ `ps -c | grep ScreenSaver` ]]
then
for id in `ps -c | grep ScreenSaver`
do
kill $id
break
done
fi
exit 0
fi
fi

if [[ `ps -c | grep ScreenSaver` ]]
then
for id in `ps -c | grep ScreenSaver`
do
kill $id
break
done
fi

nice -n 20 /System/Library/Frameworks/ScreenSaver.framework/Resources/
ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background -module $module &
The last line has been broken into two for easier reading; remove the line break and enter it on one line (no spaces) when pasting the code.

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020830050630964