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


Click here to return to the 'Create dynamic Mail signatures via fortune' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create dynamic Mail signatures via fortune
Authored by: af3556 on Sep 02, '04 10:12:29PM
Below is an extended version that a) tests to see if Mail is running before attempting the update, b) reads quotes from a .quotes file in your home directory (fortune can often be inappropriate), and c) prefixes the quote with a constant signature 'template' (i.e. your name + phone). Here's an example .quotes file:

Be alert. The world needs more lerts.
--
It's impossible to foresee the consequences of being clever.
--
Out of my Mind - Back in 5 Minutes
--
One person, one vote (may not apply in certain States).
--
chown -R us:us /yourbase
--
Blessed are we who can laugh at ourselves, for we shall never cease to
be amused.
--
If anyone's not used Mac::Glue before, here's what I did to fetch and install Mac::Glue via the CPAN (http://cpan.org), and configure for Mail:

$ sudo cpan Mac::Glue
$ sudo gluemac /Applications/Mail.app 
You might like to install this script in cron: (save code in a file, e.g. /Users/you/bin/mail_randomsig.pl)

$ chmod +x /Users/you/bin/mail_randomsig.pl
$ ( crontab -l; echo "20,50 * * * * @AppleNotOnBattery /usr/bin/nice /Users/you/bin/mail_randomsig.pl " ) | crontab -

The script:

#!/usr/bin/perl
# - reads a random quote from a quotes file (each quote is one or more lines
#   separated by '--' on a line by itself), and appends to a signature 
#   template, which is then installed as the RandomQuote in Mail.app
# - based on an original idea at macosxhints.com ("Create dynamic Mail 
#   signatures via fortune")
#
# - Mac::Glue incurs a significant startup CPU hit (~40% CPU for a bit over a 
#   second on a 867MHz PowerBook):
#   $ time bin/mail_randomsig.pl 
#   real    0m1.491s
#   user    0m0.410s
#   sys     0m0.130s
# - I considered running as a daemon to mitigate this startup overhead; 
#   though as the perl+Mac::Glue memory footprint is around 12 MB real 
#   I decided to leave as a 'niced' cron job (perl alone is ~1MB real)
# - crontab (at 20 past and 10 to the hour):
#   20,50 * * * * @AppleNotOnBattery /usr/bin/nice /Users/ben/bin/mail_randomsig.pl 
#
# - for resource comparison I wrote an equivalent AppleScript 'stay-open' 
#   application (omitting actually reading the quotes file); the AS app 
#   uses 13.9MB real mem
# - i.e. perl+Mac::Glue's overhead is about the same as AS

use strict;
use Mac::Glue;

use Mac::Apps::Launch qw(IsRunning);

use constant SIGSRC => "$ENV{HOME}/.signature.src";
use constant QUOTES => "$ENV{HOME}/.quotes";

use constant SIGNAME => 'RandomQuote';

# had to run 'sudo gluemac /Applications/Mail.app' to set up the event 'glue'
# (don't know if full path is required)

# this takes about 1.2s on a PowerBook G4/867 (10.3.4)
my $Mail = Mac::Glue->new('Mail');

my $sig;

unless (IsRunning($Mail->{ID}))
{
    exit;
}

foreach my $sig ($Mail->prop('signatures')->get)
{
  if ($sig->prop('name')->get eq SIGNAME)
  {
    $Mail->delete($sig);
    last;
  }
}

# read signature "template", append randomly selected quote
open FH, SIGSRC or die "failed to open [" . SIGSRC . "]: $!\n";
$sig = join '', <FH>;
close FH;

open FH, QUOTES or die "failed to open [" . QUOTES . "]: $!\n";
{
  $/="\n--\n";
  my $l;
  while (<FH>) { rand($.) < 1 && ($l = $_) }
  chomp $l;
  $sig .= $l;
}
close FH;

#warn "[$sig]\n";

$Mail->make(new => signature => with_properties =>
  {
    name => SIGNAME, content => $sig
  }
);



[ Reply to This | # ]