Huddled Masses
You can do more than breathe for free...
Browse: Home / More Growl for Windows from PowerShell

More Growl for Windows from PowerShell

By Joel 'Jaykul' Bennett on 17-Aug-2009

Well, I’m back already with an update for the Growl module I posted yesterday …

This new version is a true PowerShell 2.0 only module, because I found that the Growl callbacks can only be handled in PowerShell 2.0 anyway, so in order to add support for that, I went ahead and upgraded the rest.

The Growl module is now designed to be used BY your PowerShell scripts and modules. The idea is that if you wrote, say, a script/module to check for email and called it PoshMail … you could start up Growl like this:


Import-Module Growl

## At least once (e.g.: on the first Type you register) you should include an AppIcon :)
Register-GrowlType PoshMail NewMail -AppIcon $PoshMailFolder\Email-48.png
## If you want to, you can still override the icon per notice type
Register-GrowlType PoshMail Hotmail -Icon $PoshMailFolder\Hotmail-48.png
Register-GrowlType PoshMail GMail -Icon $PoshMailFolder\GMail-48.png
 

Now, technically that’s all we have to do. At that point, we could pop up Growl notices for either Hotmail or GMail … let’s say our fictitious script (which is running in the background on an event timer) discovers a new message … you could notify the user with a Url callback. Let’s assume that you have a few variables set after checking for email:

  • $Number is the number of email messages
  • $Subjects is an array of email subject lines
  • $Urls is an array of links to the emails


Send-Growl PoshMail GMail "You have $number new messages" ($Summary[0..2] -join "`n") -Url $Urls[0]
# OR ...
Send-Growl PoshMail Hotmail "You have $number new messages" ($Summary[0..2] -join "`n") -Url $Urls[0]
 

Of course, if you wanted to launch your Outlook 2010 preview because you discovered new POP or IMAP mail … or because you want to use Outlook to read your Hotmail/GMail … then a callback URL isn’t going to cut it. In that case, you want to handle the click event yourself:



## We would need it to launch something appropriate on receipt of new POP3 email, for instance.
Register-GrowlCallback {
   PARAM( $response, $context )
   # This is just here for your sake, because I know you want to know what else is in there:
   Write-Host $("Response Type: {0}`nNotification ID: {1}`nCallback Data: {2}`nCallback Data Type: {3}" -f $context.Result, $context.NotificationID, $context.Data, $context.Type) -fore Yellow
   if($context.Result -eq "Click") {
      ## Start the default email client
      Start-Process $(
         $MailTo = (gp Registry::HKEY_CLASSES_ROOT\mailto\shell\open\command)."(default)" -split " "
         for($i=0;$i-lt$mailto.Count;$i++) {
            $email = "$($mailto[0..$i])".Trim('"')
            if(Resolve-Path $email){ return $email }
         }
      )

   }
}
 

Something like that should work regardless of your actual email client, and then you just have to pass a callback value to make sure your function gets called:


# This would trigger the callback REGARDLESS of whether it was clicked.
Send-Growl PoshMail NewMail "You have $number new messages" ($Summary[0..2] -join "`n") -CallbackData "Data" "POP3 Callback"
 

There are a lot of other possibilities here, from alerting when long running commands finish (think PSJobs, or even remote jobs) to … writing popup-based PowerShell instant messengers, or even … using Growl as a ghetto inter-process communication medium which works on multiple PCs. Ok, that’s maybe a bit much, but the point is: sky’s the limit. Have a little fun. Note that the machine SENDING the popups doesn’t necessarily have to have Growl installed — you could just copy the libraries over and then send remote growls….

Similar Posts:

  • Better error messages for PowerShell ValidatePattern
  • PowerShell Scripting Best Practices: Prefix A
  • Working with multiple versions of PowerShell Modules
  • F.A.Q.: How do I Install a PowerShell Module
  • Logging Robocopy errors to the Event Log using PowerShell

Posted in Huddled | Tagged Growl, Growl for Windows, Notifications, Popup, PowerShell, Scripting

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011
  • November 2010
  • August 2010

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.