Change the login background of Mac OS X

(This article needs verification for Mac OS X Lion)

This story has a history. Way back in early 2000, I wrote a small shellscript that would set the background of the login-screen of XDM (It’s a Xwindows/Unix thing). You can find that original here. I get bored with login-screens that look the same everytime you login – or any desktop background or wallpaper but that’s another story. I figured it shouldn’t be too difficult to change that, it’s an image right? Why shouldn’t I be able to change that. It’s on my computer, shouldn’t I be in control of that? And so a cup of coffee and a little Unix shell code did that trick. The old script depends on Windowmaker to set the background image. I’m not quite sure if that is still around.

So some 10 years later, I convinced my wife that we needed a new computer again, and bought this beautiful, hip & trendy iMac. I’m like a kid in a candy store: This baby runs a decent unix shell and … xman! And I must say that I really like OS X, but being greeted with the same star background over-and-over again when you login gets old pretty quick for me.


Hmm. I’ve scratched that itch before, didn’t I? With Unix every once in a while I have this “deja vu”. In this case X11/Xorg was replaced by Apple, which was somewhat of a puzzle. As it turns out, this image is a Jpeg file located in /System/Library/CoreServices as “DefaultDesktop.jpg”. Because I’m not familiar with how (or where) that filename and location is handled, I assumed it easy enough to simply overwrite DefaultDesktop.jpg with one my own backgrounds. So the code is largely unchanged from its original back in 2001:

#!/bin/sh
# $Id: Xsetup_0,v 1.3 2002/11/07 20:55:57 zulu Exp $
#Copyright (C) 2001  B.J. van Buuren
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; version 2 of the License.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

set_next_bg() {
	SCRIPT="set_next_bg"
	IMGDIR="/Users/Shared/Images"
	DISPIMG="${IMGDIR}/.${SCRIPT}"

	if [ ! -d "${IMGDIR}" ]; then
		return 1
	fi

	for img in ${IMGDIR}/*
	do
		file $img | awk -F: '{print $2}' | grep -i 'image data' >/dev/null 2>&1
		if [ "$?" -ne 0 ]; then
			# This is not an image
			continue
		fi
		if [ ! -r "${DISPIMG}" ]; then
			# There's no recorded history, use this image
			unusedimg=yes
			break
		fi
		grep "${img}" "${DISPIMG}" >/dev/null 2>&1
		if [ "$?" -ne 0 ]; then
			# Finally found an image that hasn't been used before
			unusedimg=yes
			break
		fi
	done
	if [ "x$unusedimg" = "x" ]; then
		# Never found a unused image, use the last, but clear the history
		> "${DISPIMG}"
	fi

	echo "${img}" >> "${DISPIMG}"
	cat "${img}" > /System/Library/CoreServices/DefaultDesktop.jpg
	logger -p local2.notice -t CHANGE-LOGIN-BACKGROUND "Changed the background to $img"
	sleep 1
	sync
}

#xhost +localhost
#xscreensaver-command -exit
#xscreensaver -no-splash &

set_next_bg

#xconsole -geometry 480x120-0-0 -daemon -notify -verbose -fn fixed -exitOnFail

So how does this work? First of all, this script assumes that you have a directory filled with backgrounds. You’ll find plenty on the web these days. My wife makes beautiful pictures, which is what I’m using. /Users/Shared/Images would be acceptable, but feel free to change it to whatever you’d like to use. It then selects the first image it encounters from that directory and uses that to overwrite OS Xs’ default. It stores the name of that image in a hidden file in that same directory, and avoids that one until there aren’t any unused images left and starts with a fresh list.

Now, how to have Mac OS X start this automatically? If you think about it there are more ways than just one, although I’m only going to describe the one I’m using. Apple has documented “StartupItems”, and the method below could be improved.

WARNING: THE FOLLOWING SHOULD BE DONE AS SUPERUSER — USE EXTREME CAUTION — A MISTAKE MAY DESTROY YOUR SYSTEM — DO NOT DO THIS IF YOU’RE UNSURE

If you start the terminal.app you can become superuser a.k.a. root by typing ‘sudo su -‘.

Create a directory under /Library/StartupItems named Change-Login-Background. Save the script as Change-Login-Background. NO extension (No .sh or .txt or anything). Place it in /Library/StartupItems/Change-Login-Background and allow it to be executed by setting the appropriate rights. You can use the command ‘chmod 755 Change-Login-Background’ for that. Then add the following to a file named StartupParamters.plist in that same directory:

{
  Description     = "Change Login Background";
  Provides        = ("Change-Login-Background");
  OrderPreference = "None";
}

If you’re not familiar with a Unix commandline, these commands below set everything up. All you have to do is copy the script to /Library/StartupItems (after you run these commands):

sudo su -
mkdir -p /Users/Shared/Images
mkdir -p /Library/StartupItems/Change-Login-Background
cd /Library/StartupItems/Change-Login-Background
touch Change-Login-Background
chmod 755 Change-Login-Background
cat << EOF > StartupParameters.plist
{
  Description     = "Change Login Background";
  Provides        = ("Change-Login-Background");
  OrderPreference = "None";
}
EOF

There! That should do it! Put some Jpeg files in /Users/Shared/Images, reboot and see what happens!

Epilogue

Change-Login-Background as described above is used as a startup-script, which ofcourse it is not. If you want it to be a startup-script there are some rules to follow, which I ommitted here. This causes the background to be overwritten when you shutdown your Mac and although it doesn’t skip the next background, you never get to see it. If you think about users in a multi-user computer environment in this context and when a background should be changed to what and by whom becomes increasingly complex. To my opinion, overwriting a deafult image is a dirty hack anyway, so I didn’t bother to fix this: I have a different image each time I login now! That goal was accomplished with this hack. But feel free to improve it!

Bugs: Yes there is one kind of annoying bug, but that should be expected as this method is “not perfect”. It may happen that a background is only copied half over the previous one, leaving the bottom half grey. If that happens, you’ll find a message saying so in /var/log/system.log, something like “LoginDesktopPicture[273]: Corrupt JPEG data: premature end of data segment”.

By the way: If you want the full power of xman on your Mac unleashed you should type the following:

export MANPATH=/man:/usr/man:/usr/local/man:/opt/man:/share/man:/usr/share/man:/usr/local/share/man:/usr/X11/man:/usr/X11/share/man
xman -notopbox -bothshown

oh… you don’t need rootly powers for xman 😉

This entry was posted in Uncategorized and tagged . Bookmark the permalink.