Hack 15. Alert Your Mac


Schedule GUI alerts from the command line.

Growl (http://www.growl.info/) is a small utility for Mac OS X that allows any application to send notifications to the user. The notifications pop up as a small box in a corner of the screen, overlayed on the current active window (as shown in Figure 2-1).

Figure 2-1. A simple Growl notification


The Hack

You can send Growl notifications from Perl, thanks to Chris Nandor's Mac::Growl. The first thing you have to do is tell Growl that your script wants to send notifications. The following code registers a script named growlalert and tells Growl that it sends alert notifications:

use Mac::Growl; Mac::Growl::RegisterNotifications(     'growlalert', # application name     [ 'alert' ],  # notifications this app sends     [ 'alert' ],  # enable these notifications );

Growl displays a notification to let you know the script has registered successfully (Figure 2-2). You need only register an application once on each machine that uses it.

Figure 2-2. A newly registered application


When you want to send a notification, call PostNotification( ), passing the name of the script, the kind of notification to send, a title, and a description:

Mac::Growl::PostNotification(     'growlalert', # application name     'alert',      # type of notification     "This is a title",     "This is a description.", );

This will pop up a notification window (Figure 2-3) and fade it out again after a few seconds.

Figure 2-3. Notification with title and description


Running the Hack

You might want a small script that sends you an alert after a time delay. The following command-line utility takes a time period to delay and a message to display in the alert. It calculates the time when the alert should appear, then forks to return control of the terminal window to the user. The forked child sleeps the requested amount of time, and then posts a Growl notification with the message as the title and no description.

my %seconds_per = (     's'   => 1,     'm'   => 60,     'h'   => 60*60, ); my ( $period, @message ) = @ARGV; my ( $number, $unit )    = ( $period =~ m/^([\\.\\d]+)(.*)$/ )       or die "usage: ga number[smh] message\\n"; $unit ||= 's'; my $growl_time = $number * $seconds_per{$unit}; my $pid        = fork; die "fork failed ($!)\\n" unless defined $pid; unless ( $pid ) {     require Mac::Growl;     sleep $growl_time;     Mac::Growl::PostNotification(         'growlalert', # application name         'alert',      # type of notification         "@message",   # title         "",           # no description         1,            # notification is sticky     ); }

The additional argument passed to PostNotification tells Growl that the notification should stay on the screen until the user clicks it, instead of fading after a few seconds.

Some common uses of this program are:

$ ga 5m coffee $ ga 2.5h 'oxford soon - get off the train'             

Hacking the Hack

For Unix systems running the X Window system, use the xmessage command instead of Mac::Growl:

unless ( $pid ) {     sleep $growl_time;     system( 'xmessage', @message ); }

You can get creative there, popping up the window near the cursor or automatically fading out after a specified period of time.

On Windows systems running the messenger service, the proper invocation is something like:

unless ( $pid ) {     sleep $growl_time;     system( qw( cmd net send localhost ), @message ); }



Perl Hacks
Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving
ISBN: 0596526741
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net