This hint allows you to randomize your e-mail signatures. I use this with Thunderbird, but this hint should work with any e-mail client that refers to an external file for your signature. I needed a solution that just worked and didn't really require anything more from me. Some of the plug-ins out there require some user action each time you write an e-mail -- which I didn't like. Also, most of my signature remains the same, so I wanted my solution to simply change one part of my signature (a tagline), the rest remains the same.
Basically, my solution is composed of a couple files which can be merged to form a random signature (and any supporting graphic files, etc). These should be placed in your normal directory holding your signature. For this example, let's say this is the directory /Users -> jbloggs -> signature/:
SignatureRandomizer.watchdog
This is a relatively small script that, once it starts, keeps running all the time (or until it is killed). It monitors the signature.html file and, if it has been accessed since it was created, then a new signature file is created by merging a random line from the taglines.txt file into the signature-template.html file. If the signature file has not been used since it was created, then the script does nothing more and goes back to sleep (for five seconds). If this seems wasteful for any reason, I've been running this for sometime now. Since my last reboot, three days ago, the watchdog script itself has consumed only one single second of CPU time.
If you're curious how it works, there is basically a loop which first tells itself to do nothing for five seconds, then, checks the signature.html file for usage. If it has, then it counts how many lines are in the taglines.txt file, then grabs one and stores it in line. Next, it looks for the token [TAGLINE] in the signature-template.html, file and replaces it with the contents of line. The new version of signature-template.html is now stored in signature.html.
What is not clear in the script is that when a new signature.html file is written, the Mac OS filesystem sets the access time and modification time to be the same. If another application (like Thunderbird) reads that signature file later, the access time will be updated and become later than the modification time. Within five seconds, the watchdog script will wake up and detect this, and create a new one.
USERPATH=/Users/jbloggs
TAGLINES=${USERPATH}/signature/taglines.txt
TEMPLATE=${USERPATH}/signature/signature-template.html
SIGNATURE=${USERPATH}/signature/signature.html
HOLDER="\[TAGLINE\]"
# All the magic below.
# Simply check to see if the signature file has been accessed
# since it was created and, if so, modify it
while sleep 5
do
# If the signature file was not accessed since it was modifyied...
if [ ! -N "$SIGNATURE" ]
then
# Modify it
lines=(`wc -l < $TAGLINES`)
let "randomline = $RANDOM % $lines + 1"
line=`sed -n "${randomline}p" < $TAGLINES`
sed "/${HOLDER}/s//${line}/" <${TEMPLATE} >${SIGNATURE}
fi
done
SignatureRandomizer
#!/bin/sh
##
# SignatureRandomizer
##
. /etc/rc.common
MYPATH=/Library/StartupItems/SignatureRandomizer
StartService ()
{
if [ -e ${MYPATH}/SignatureRandomizer.pid ]; then
echo "SignatureRandomizer may already be running. Stopping first (restart)"
StopService
fi
if [ -x ${MYPATH}/SignatureRandomizer.watchdog ]; then
echo "Starting SignatureRandomizer."
${MYPATH}/SignatureRandomizer.watchdog &
echo $! > ${MYPATH}/SignatureRandomizer.pid
fi
}
StopService ()
{
if [ -e ${MYPATH}/SignatureRandomizer.pid ]; then
echo "Stopping SignatureRandomizer."
kill -9 `cat ${MYPATH}/SignatureRandomizer.pid`
rm ${MYPATH}/SignatureRandomizer.pid
else
echo "SignatureRandomizer not running (or at least, there is no PID file)."
fi
}
RestartService ()
{
StopService
StartService
}
RunService "$1"
StartupParameters.plist
//
// SignatureRandomizer
//
{
Description = "Signature Randomizer";
Provides = ("SignatureRandomizer");
Messages =
{
start = "Starting Signature Randomizer";
stop = "Stopping Signature Randomizer";
restart = "Restarting Signature Randomizer";
};
Make sure that you set the owner/group and permissions for SignatureRandomizer, SignatureRandomizer.watchdog and StartupParameters so that they look like this:
>ls -l
-rwxr-xr-x 2 root wheel 872 Jul 25 23:40 SignatureRandomizer
-rwxr-xr-x 2 root wheel 661 Jul 25 23:38 SignatureRandomizer.watchdog
-rw-r--r-- 2 root wheel 296 Jul 25 23:27 StartupParameters.plist
Reboot and it should begin working -- enjoy!
Mac OS X Hints
http://hints.macworld.com/article.php?story=20060729024916512