Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Display a random fortune in the login window System
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!]
    •    
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[25,926 views]  

Display a random fortune in the login window | 30 comments | Create New Account
Click here to return to the 'Display a random fortune in the login window' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Display a random fortune in the login window
Authored by: zimmerleut on Nov 18, '04 11:31:13AM
loginfortune.sh as it is here stumbles over double quotes that may be in fortunes. This is how it works for me:

#!/bin/sh
# NOTE: the next two lines must be ONE LINE with a space
# replacing the line break ... broken here for easier reading
/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow
LoginwindowText \""$(/sw/bin/fortune | sed s/\"/\\\"/g)"\"


[ Reply to This | # ]

I'm afraid that doesn't run on my box
Authored by: porkchop_d_clown on Nov 18, '04 12:13:06PM

It generates an error about an unterminated expression in SED.

I think somewhere along the way you lost some back slashes? I used

/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText \""$(/sw/bin/fortune | sed s/\"/\\\"/g)"\"

---
Everyone loves a clown, but no one will lend him money!



[ Reply to This | # ]
I'm afraid that doesn't run on my box
Authored by: zimmerleut on Nov 18, '04 01:15:30PM

yeah, my backslashes got lost while posting... thanks



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: apaterso on Nov 19, '04 05:51:18PM

I must be missing something, because I don't get anything to show up.

I have the file

usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText -string "$(/sw/bin/fortune)"

saved as loginfortune.sh I have chmod 755 it, it lives in the /Libarary/Hooks
directory. But no dice, actally all I get is an empty box where the fortune ought to be. Any neat ideas?

Andy



[ Reply to This | # ]
Some fortunes are long, some are offensive
Authored by: porkchop_d_clown on Nov 18, '04 12:18:28PM

If you're worried about either you might want to do this:

/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText \""$(/sw/bin/fortune -n 80 -s /sw/share/fortunes | sed s/\"/\\\"/g)"\"

This will restrict fortunes to the inoffensive ones that are less than 80 characters long.

---
Everyone loves a clown, but no one will lend him money!



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: leknor on Nov 18, '04 12:23:46PM
If you don't wish to install fink a Mac OS X package of fortune can be found at: http://dsandler.org/soft/macosx/message.php

You'll also need to change the path in the script from /sw/bin/fortune to /usr/bin/fortune .

Also, that version doesn't support the -n switch as the fink version seems to. You can still use -s for short fortunes only. See `man fortune` once you've installed the package for more switches.

[ Reply to This | # ]

Display a random fortune in the login window
Authored by: mattia on Nov 18, '04 01:36:29PM
Notwithstanding the suggested remedy to the double quotes thing, I'm still unable to 'fortune' quotes containing double quotes. Whether I use my fortunes or the system supplied ones, whenever I run the command I get the following error

2004-11-18 08:22:57.703 defaults[1104] Could not parse: "
        The control code for all beginning programmers and those who would
        become computer literate.  Etymologically, the term has come down as
        a contraction of the often-repeated phrase "ascii and you shall
        receive."
                -- Robb Russon".  Try single-quoting it.
I'm not that fluent with shell substitution of single or doiuble quotes, but I've made some unsuccessful attempts to over-quote the expression. Is is just me or has somebody else run into the same problem? Mattia

[ Reply to This | # ]
Display a random fortune in the login window
Authored by: jamiebah on Nov 21, '04 10:51:09AM

This doesn't work if you login as a "standard" user. Try to run the command from terminal. You won't have permissions, now login as an admin user and it will work.

I created a seperate account for admin purposes and my normal account is a "standard" account for security purposes...



[ Reply to This | # ]
Solve quoting problem once and for all
Authored by: greed on Nov 18, '04 03:59:24PM

Aha! We're going off on a tangent with this quoting problem.

The problem is, defaults handles lots of different types, and it is guessing the type from your input. Sufficently complex input (containing ", {, }, [, or ], that I've found so far) give it fits.

So we just tell it the argument is a string and all is well!


/usr/bin/defaults write \
 /Library/Preferences/com.apple.loginwindow \
 LoginwindowText \
 -string "$(/sw/bin/fortune)"

(The trailing backslashes serve to continue the command onto multiple lines.)

And, of course, add the -n 80 -s /sw/share/fortunes flags to taste. I personally hate the Zippy the Pinhead quotes, but like the offensive file.

[ Reply to This | # ]

Solve quoting problem once and for all
Authored by: slowco on Nov 19, '04 10:37:29AM

A good solution, thanks. I forgot about the -string parameter. God job!



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: mproud on Nov 18, '04 04:32:59PM
If you're feeling really adventurous, you can change all sorts of aspects about the login window through Interface Builder.

System/Library/CoreServices/SecurityAgentPlugins/loginwindow.bundle/Contents/Resources/English.lproj/login.nib
(Click me to get there in the Finder)

(As a tip to get you going: any image control placed in the nib file in Interface Builder can use images from the Resources folder of this bundle, just by supplying the name of the image in the control, minus the extension. Any QT format is supported — .tif/.tiff, .png, .jpg, .gif, etc.)

[ Reply to This | # ]

Touchup to make logout hook work with 10.2
Authored by: clueless on Nov 18, '04 04:59:04PM
Cool - I had a bit of trouble getting it to go though. In the end I used 'greed's version in the loginhook script

#!/bin/sh
/usr/bin/defaults write \
 /Library/Preferences/com.apple.loginwindow \
 LoginwindowText \
 -string "$(/sw/bin/fortune)"
and

sudo defaults write com.apple.loginwindow \
        LogoutHook /Library/Hooks/loginfortune.sh
to make the log out hook version work. I don't understand why everybody wants to shorten the long ones - they wrap ok in Jaguar anyway

[ Reply to This | # ]
Display a random fortune in the login window
Authored by: macmaxbh on Nov 18, '04 05:05:01PM

With the loginhook, will there be a new fortune every time you go to the login window or just when you restart (eg--if you log out of a running system, does the fortune change from what you got when you turned the computer)?



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: robg on Nov 18, '04 06:23:16PM

Yes, it seems to change whenever the login screen is presented.

-rob.



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: starwxrwx on Nov 28, '04 03:29:21AM

I use fast-user-switching to return to the login screen (but not log out) when I am away from my desk. The fortune quote stays the same.



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: gw on Dec 08, '04 02:03:52AM

you need to make sure that you add the LogoutHook in order to change the fortune.

the fortune will only change when a user logs out (so just doing a lot of fast user switching will always reveal the same fortune, logging out just one user will force a change)

this is because it's the logout hook that's setting the login window text to the actual string


---
----
"I only conceptualize, everything else is hardware."
Prof. L.B. Wilson



[ Reply to This | # ]
A generic script to change the login banner
Authored by: jecwobble on Nov 18, '04 11:20:27PM
For the fun of it (and the practice), I decided to wrap part of this hint up into a script. I called it loginbanner (only because banner is already taken), and saved it in ~/bin. Pretty straight forward. I haven't tested it extensively, so it may choke in unusual situations.

#! /bin/sh

####### Define constants and Variables #######

ME=`basename $0`       # Capture this script's name
VERS="1.0"             # Current version number
E_NOLENGTH=66          # Exit code for zero length arguments


####### UI Information #######

usage () {
   echo
   echo "  Usage: $ME [options] string"
   echo "  Change the logon window's banner"
   echo "  Options:"
   # Change the following as needed
   # and mirror in the following section
   echo "        -c         Center text"
   echo "        -l         Go to login windoi to view results"
   echo "        -u         Unset login window banner. Other options ignored"
   echo "        -h         For this help list"
   echo
}

####### Parse command line #######

# Get options from $@ (parameters/arguments"
while getopts cluh option
do
   case $option in
   c)   OPT_c=1;;
   l)   OPT_l=1;;
   u)   OPT_u=1;;
   h)   usage
        exit 0;;
   esac
done

# This strips off options leaving remaining parameters
shift $(($OPTIND - 1))

# Check for zero length arguments
# Uncomment to activate
for arg in "$@"
do
   if [ -z "$arg" ]; then
      echo
      echo "$ME error: No empty string arguments allowed" >&2
      usage >&2
      exit $E_NOLENGTH
   fi
done


##############################################################
#                     Main script area                       #
#      If we got this far, all command line parameters       #
#                        must be good                        #
##############################################################

# Unset login window banner
if [ ! -z "$OPT_u" ]; then
   echo "Deleting login window text"
   /usr/bin/defaults delete /Library/Preferences/com.apple.loginwindow LoginwindowText
   exit 0
fi

# Center text
if [ ! -z "$OPT_c" ]; then
   echo "Trying to center"
fi

# Assign text to banner
if [ ! -z "$1" ]; then
   /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText -string "$1"
else
   echo "$ME error: No empty string arguments allowed" >&2
   usage >&2
   exit $E_NOLENGTH
fi

# Preview banner
if [ ! -z "$OPT_l" ]; then
   /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
fi


######## Exit with a successful code ########
exit 0


[ Reply to This | # ]
Ignore the centered text part...
Authored by: jecwobble on Nov 19, '04 08:37:25AM

I had planned to include script for centering the text based on a fixed character width, but abandoned the idea when I realized that the font that appears in the login window is not mono-spaced. If it had been, I was going to search for a script I remembered seeing somewhere that would pad some text with spaces to center it within a given number of columns.



[ Reply to This | # ]
Slightly more refined version
Authored by: jecwobble on Nov 25, '04 09:39:20AM
In case anyone is interested in using this script (turns out I use it everyday since I don't care for the fortunes that the original hint produces), here's a slightly more refined version that removes unused code and adds a little more user friendliness to the UI:
#! /bin/sh

####### Define constants and Variables #######

ME=`basename $0`       # Capture this script's name
VERS="1.0"             # Current version number
E_NOLENGTH=66          # Exit code for zero length arguments


####### UI Information #######

usage () {
   echo
   echo "  Usage: $ME [options] string"
   echo "  Script for manipulating the logon window's banner"
   echo "  Options:"
   # Change the following as needed
   # and mirror in the following section
   echo "        -b         List current banner text"
   echo "        -l         Go to login window to view results"
   echo "        -u         Unset login window banner. Other options ignored"
   echo "        -h         For this help list"
   echo
}

bannerNotSet() {
   echo "  Login window banner is not set."
   echo
   exit 0
}

####### Parse command line #######

# Get options from $@ (parameters/arguments"
while getopts bluh option
do
   case $option in
   b)   OPT_b=1;;
   l)   OPT_l=1;;
   u)   OPT_u=1;;
   h)   usage
        exit 0;;
   esac
done

# This strips off options leaving remaining parameters
shift $(($OPTIND - 1))


##############################################################
#                     Main script area                       #
#      If we got this far, all command line parameters       #
#                        must be good                        #
##############################################################

# Unset login window banner
if [ -n "$OPT_u" ]; then
   /usr/bin/defaults delete /Library/Preferences/com.apple.loginwindow LoginwindowText 2> /dev/null
   echo
   echo "  Deleted login window banner"
   echo
   exit 0
fi

# Assign text to banner
if [ -n "$OPT_b" ]; then
   echo
   /usr/bin/defaults read /Library/Preferences/com.apple.loginwindow LoginwindowText 2> /dev/null || bannerNotSet
   echo
   exit 0
fi

# Assign text to banner
if [ -z "$1" ]; then
   echo
   echo "  $ME error: No empty string arguments allowed" >&2
   usage >&2
   exit $E_NOLENGTH
else
   /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText -string "$1"
   echo
   echo " Login window banner set"
   echo
fi

# Preview banner
if [ -n "$OPT_l" ]; then
   /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
fi


######## Exit with a successful code ########
exit 0


[ Reply to This | # ]
Display a random fortune in the login window
Authored by: las_vegas on Nov 19, '04 12:16:20AM

Great hint! But I don't understand why any scripts need to be modified.

Why not simply place the script into startup items list of each user? Of course, this would only change the fortune for the next login rather than this one, but it would display a new fortune with every login rather than startup.



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: Mato on Nov 19, '04 07:30:22AM
Ok, I might be dumb but I dont understand the "(or something similar) and make it executable with
chmod +x loginfortune.sh
" part. I made the txt file, but now, how do I have to do above command ? Thanks

---
You dont have to swim faster than sharks, just faster than the guy next to you

[ Reply to This | # ]

Display a random fortune in the login window
Authored by: afvdk on Nov 19, '04 09:47:25AM

It's a unix command. So open up a Terminal (found in App lications -> Utilities). Change to the directory where the file is located:

cd ~/Desktop

(if it is on your desktop) and then run the previous command. it will make the script executable.



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: Mato on Nov 19, '04 09:58:20AM
Ok, it was the
cd ~/Desktop
that i was missing. But is it normal now that after doing that, and doing the command to make it executable, I have no confirmation of any sort, and that the file havent changed, I mean it still say "document" when I look in informations ?

---
You dont have to swim faster than sharks, just faster than the guy next to you

[ Reply to This | # ]

Terminal isn't chatty
Authored by: clueless on Nov 19, '04 05:21:00PM

Yes it's normal.

Command line programs like chmod normally only say something if there is an error. No news is good news so to speak.

And making a script executable doesn't assign an icon to it.



[ Reply to This | # ]
Terminal isn't chatty
Authored by: Mato on Nov 19, '04 09:36:27PM

I wasnt assuming an icon, just a new type (dont know if its the right english name, my os is french) in the info, like it was document, I assumed it was perhaps gonna change to a new type like "executable unix file" or something :). Thanks for your answer, I'll see that at next reboot :).

---
You dont have to swim faster than sharks, just faster than the guy next to you



[ Reply to This | # ]
Remove?
Authored by: koen on Nov 21, '04 05:21:15PM

How do I remove the display of the fortunes in the login window?
I guess a variant of this line:

/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow
LoginHook /Library/Hooks/loginfortune.sh

thanks,

- Koen.



[ Reply to This | # ]
Remove?
Authored by: jecwobble on Nov 22, '04 12:55:30PM
To remove the login banner, run this in a terminal:
/usr/bin/defaults delete /Library/Preferences/com.apple.loginwindow LoginwindowText


[ Reply to This | # ]
Display a random fortune in the login window
Authored by: Mato on Dec 02, '04 07:01:01AM

Ok, I'm surelly dumb, but i've done all the steps mentioned here, nothing happens ... is there's a possibility that's because of a french os ? I've installed fink, np. I used textedit to copy past the script. I typed the commands mentioned, created the Hook folder, nothing happens ... I need help :)

---
You dont have to swim faster than sharks, just faster than the guy next to you



[ Reply to This | # ]
Display a random fortune in the login window
Authored by: harywilke on Dec 13, '04 10:36:52AM
I'm having a hard time with this hint. it _seems_ simple enough, but alas i'm frustrated. I can get the fortune to update manualy by running the loginfortune.sh script, but i can't get it update with LoginHook or LogoutHook here is what i've done. I'm setting this up under my administrator account. I created the "loginfortune.sh" script as such:

#!/bin/sh
/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText -string "$(/usr/bin/fortune)"
I placed it in /Library/Hooks folder, did a +x chmod on it. Then wrote to LoginHook with

/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginHook /Library/Hooks/loginfortune.sh
I've checked that all is well with the com.apple.loginwindow.plist by opening it with the Property List Editor. sure enough the fortune string is listed and the LoginHook is set to a string value of "/Library/Hooks/loginfortune.sh" so everything looks ok. but it dosen't work. everytime i login i get the same fortune. I think it's a permissions thing because every time i login and try to examin the com.apple.loginwindow.plist i notice that the permissions get set to Owner:system w/ Read&Write Group:Admin w/ Read Only and Others w/ Read Only If i manually run my loginfortune.sh script the Owner switches to my login name w/Read&Write and the fortune gets updated. how can i fix this so it works?

[ Reply to This | # ]
Display a random fortune in the login window
Authored by: ofey on Aug 17, '05 10:48:46AM

I'm just being very lazy here but anyone can update this hint for it to run on 10.4.2?

---
--
Ti PBook G4 1GHz/SD, iPod G3 20GB, AP Xtreme Base Station.



[ Reply to This | # ]