User:ThePolack/RemGrowl.pl

From 43FoldersWiki

< User:ThePolack
Revision as of 23:03, 4 August 2005 by ThePolack (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

I know it ain't pretty. This is one of those "secret scripts" that Merlin and Danny O'Brien keep talking about. It works on my system though and is really only meant to serve as an example for others who want similar functionality. Be warned, the first time it's run after a system restart it is sluggish as hell (I'm probably going to fix that someday with a Cocoa app instead of Perl).

Here is an example screenshot of what the output of RemGrowl looks like. It's different from most remind/Growl combos in that all the reminders are displayed in a single Growl dialog (I use Smoke as the display style). I personally prefer this greatly over having each individual reminder in it's own dialog box.

An uneventful day for me.
An uneventful day for me.
#!/usr/bin/perl 

#################################################
# RemGrowl.pl - script for displaying output of #
#               remind program in Growl alert   #
#################################################
# This perl script uses Mac::Glue instead of    #
# Mac::Growl because the current Growl libs     #
# end up using Glue anyway and it seemed        #
# redundant to me. That may cause problems if   #
# the AppleScript interface to Growl ever       #
# changes, but hey, the script is free so don't #
# complain.                                     #
#################################################

use strict;
use warnings;

use Mac::Glue ':all';

sub RegisterNotifications($$$$;$);
sub PostNotification($$$$$;$$$);

# Define paths to remind application and 
# remind configuration file (I use a different
# configuration for output of this script
# than I do for output on the command line.)
my $remindApp = "/usr/local/bin/remind";
my $reminders = "$ENV{HOME}/.remindersgrowl";

# the following four variables are used for registering
# RemGrowl.pl with Growl. Read the Growl documentation
# for information on the registration process and why
# it's necessary.
my $appName              = "RemGrowl.pl"; # change this if you change 
                                          # the name of this script 
                                          # on your system
my $noteName             = "Reminders";
my $allNotifications     = [$noteName];
my $defaultNotifications = [$noteName];

# the following three variables are used when posting the
# notification to Growl. Alter these as you see fit.
my $stickyBit        = 1;  # notification will be 'sticky' if set to 1
                           # will fade away in time if set to 0
my $priorityLevel    = 0;  # 0 is default priority. valid range is -2 to 2
# the notification icon is displayed in the corner of the growl
# notification. Leave this undefined if you want to use Growl's
# default icon
my $notificationIcon = "file://$ENV{HOME}/Pictures/remindicon";

# First, we create a glue object that will be used
# pass event messages to Growl.
my $glue = Mac::Glue->new('GrowlHelperApp'); 

# use the output of the growl command to create
# the body of our Growl notification. The first line
# of that output, is the current date. Use that line
# as the title of our Growl notification. To change
# the format of this title, use the BANNER command
# in your remind config file.
my @body = `$remindApp -g $reminders`;
my $title = shift @body;

# to avoid an overly large Growl notification, remove the
# line immediately following the title iff it is blank
# (this depends on your BANNER settings)
while ($body[0] =~ /^\s*$/) {
	shift @body;
}

# Register this script as an application with
# Growl using the notification variables defined 
# above
RegisterNotifications($glue, $appName, $allNotifications, $defaultNotifications);

# Use the body and title generated above to post
# a notification to Growl
PostNotification($glue, $appName, $noteName, $title, join("",@body), $stickyBit, $priorityLevel, $notificationIcon);


exit;

# the following subs were lifted from Mac::Growl and altered slightly
sub RegisterNotifications($$$$;$)
{
	my($glue, $appName, $allNotes, $defaultNotes, $iconOfApp) = @_;

	for my $notes ($allNotes, $defaultNotes) {
		$notes = [ map {
			Mac::Glue::param_type(Mac::AppleEvents::typeChar(), $_)
		} @$notes ];
	}

	$glue->register(
		as_application			=> $appName,
		all_notifications		=> $allNotes,
		default_notifications	=> $defaultNotes,
		icon_of_application		=> $iconOfApp,
	);
}

sub PostNotification($$$$$;$$$)
{
	my($glue, $appName, $noteName, $noteTitle, $noteDescription, $sticky, $priority, $image) = @_;
	$sticky = $sticky ? 1 : 0;

	my %params = (
		application_name	=> $appName,
		with_name			=> $noteName,
		title				=> $noteTitle,
		description			=> $noteDescription,
		sticky				=> $sticky
	);

	$params{priority} = $priority if defined $priority;

	$params{icon_of_file} = Mac::Glue::param_type(Mac::AppleEvents::typeChar(), $image) if $image;

	$glue->notify(%params);
}

Personal tools