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

Randomize Thunderbird (or other mail client) signatures UNIX
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/:
  • signature-template.html - my normal signature (and all of its support files), but with a minor change: I've replaced the tagline with a marker [TAGLINE].
  • taglines.txt - this is a list of all my taglines that I want to randomly choose from for each e-mail. There is one tagline per line, and any characters that might be special to the shell must be escaped properly.
Other files required to make up this solution are the following, which are to be placed in the directory /Library -> StartupItems -> SignatureRandomizer, which is owned by root, has its group set to wheel and has permissions 0755 (drwxr-xr-x):
  • SignatureRandomizer.watchdog - this is the magical bit. This shell script monitors my signature.html file to see if it has been used, and if it has, it creates another one by randomly selecting a line from my taglines.txt file and inserts it into the signature-template.html file.
  • SignatureRandomizer - this script controls the watchdog-script.
  • StartupParameters.plist - this is required to automatically start the service when my Mac boots up.
Read on for the details on each piece of code.

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

The watchdog script above works and works well, but I don't really want to run it manually every time I plan to send emails. So, I decided to run this as a Mac OS X StartupItem. This script and the StartupParameter.plist file make this happen. It allows both the normal Mac bootup process and any authorized user to start up the watchdog-script when necessary, or shut it down, or restart it. It can do this because it maintains the watchdog-script's PID (Process ID) in a file called SignatureRandomizer.pid in the StartupItems -> SignatureRandomizer directory. This is created and deleted automatically by this script.
#!/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

This special file is what is required to get the startup process to know how to interact with the SignatureRandomzier scripts above. This is short and simple. In fact, it's probably more than we even need.
//
// 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!
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,757 views]  

Randomize Thunderbird (or other mail client) signatures | 4 comments | Create New Account
Click here to return to the 'Randomize Thunderbird (or other mail client) signatures' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
why a StartupItem ?
Authored by: hayne on Aug 03, '06 08:31:49AM

It isn't clear why this needs to use a (system-wide) StartupItem.
What can't the randomizer watchdog script be a login item (in the Accounts preferences)?
That would be much better as it would not have the security issues of StartupItems (running as root) and would only affect the one user account.



[ Reply to This | # ]
Randomize Thunderbird (or other mail client) signatures
Authored by: mkoistinen on Aug 03, '06 09:13:20AM
You're quite correct to question it. I've put it in StartupItems because I wanted it to be "invisible" to me once I set it up, and it indeed is. I had also considered putting it into a LoginItem, but once I researched it at Apple.com, it appeared that there was a lot of transition in that area, which left me a bit uncomfortable. If you'd like to make it an item to start automatically (Account Pref Pane), I think it would be trivial to use an AppleScript to invoke it with do shell script "SignatureRandomizer start". In this case you could start the feature at startup without the concerns about root and you could store these scripts virtually anyway /Users/[username]/bin would be a good place. I think also that with some simple modification, it could be a multi-user capability with the same scripts. Since I'm only one user on a laptop, I haven't had the need to venture there (yet). Good luck!

[ Reply to This | # ]
Randomize Thunderbird (or other mail client) signatures
Authored by: david-bo on Aug 05, '06 02:39:51AM
Shouldn't launchd be useful for both the startup task and the refresh-when-file-has-been-touched?

Something like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">;
<plist version="1.0">
<dict>
<key>Label</key>
<string>test</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/sig.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/private/tmp/aaa.xml</string>
</array>
</dict>
</plist>

---
http://www.google.com/search?as_q=%22Authored+by%3A+david-bo%22&num=10&hl=en&ie=ISO-8859-1&btnG=

[ Reply to This | # ]

Randomize Thunderbird (or other mail client) signatures
Authored by: david-bo on Aug 05, '06 02:42:07AM

Btw, great idea!

---
http://www.google.com/search?as_q=%22Authored+by%3A+david-bo%22&num=10&hl=en&ie=ISO-8859-1&btnG=



[ Reply to This | # ]