Display a random fortune in the login window

Nov 18, '04 10:04:00AM

Contributed by: slowco

Fortune is one of those old, useless programs whose novelty never quite wore off for me, despite the troubling lack of a switch to disable Star Trek quotes. A fortune on boot is, to me, still an essential feature of any OS.

In the Classic days, there used to be a Mac system extension that would do this, displaying a fortune in a dialog just before the Finder loaded. It's possible to duplicate this functionality on Mac OS X with a bit of AppleScript fiddling or third-party apps -- but who needs another login item slowing things down? A better, cleaner solution -- the one thats seems "right" -- is to use the "hidden" LoginwindowText default documented in an earlier hint to display a fortune in the login window. (robg adds: As seen in my screenshot at right; click it for the full version.)

Assuming fortune is installed via Fink (sudo apt-get install fortune-mod with Fink up and running), copy the following into a text file and save it as loginfortune.sh (or something similar) and make it executable with chmod +x loginfortune.sh:

The following code has been updated based on the comments below. This version should work for all entries in the fortunes file. Please read the comments for methods of controlling the length and 'cleanliness' of the fortunes used.
#!/bin/sh

# NOTE: The following is one line; the backslash should let it copy
#       and paste. If not, just replace the line break with a space.
/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow \
 LoginwindowText -string "$(/sw/bin/fortune)"
Running this script will put a random fortune in your login window. To get it to change at each startup, read the rest of the hint...

To change the fortune at every login, create a folder called Hooks (or whatever you prefer) in the top-level Library folder, and then move loginfortune.sh into it. Next, run this command (again, make it all one line with just a space; replace the relevant bits with the right names on your system):

/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow 
 LoginHook /Library/Hooks/loginfortune.sh
This will now change the fortune each time you see the login window. Personally, the above is what I prefer because, should my computer crash, I am not further enraged by having to read the same fortune again on reboot. However, If you would rather change it on logout, use the LogoutHook default instead of LoginHook.

Alternatively, it's also possible to use cron instead of login/logout hooks to change it daily, weekly or whatever.

[robg adds: A very cool hint, especially for the possibilities it opens up -- you could put whatever you like into your login panel. Date, time, file count in your Documents folder, whatever. One enhancement for the script would be to count the number of characters in the retrieved fortune, and then pad spaces at the start to make the fortune center in the login window. As you can see in the screenshot, it's left-justified. I'm not sure what the total space available is, and I'm not sure I have the shell scripting skills to do this ... but I'm sure someone does!]

Comments (30)


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