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….