PoshCode Logo PowerShell Code Repository

Send-Growl 1.0 by Joel Bennett 30 months ago (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1274"></script>download | new post

A fix to make this work on PowerShell 1.0

A first release of my module for user-notifications via Growl For Windows … includes only Send-Growl — but has all the wiring up done so you can do custom notices, icons, etc. Initial post here and future posts here

  1. ## This is the first version of a Growl module (just dot-source to use in PowerShell 1.0)
  2. ## Initially it only supports a very simple notice, and I haven't gotten callbacks working yet
  3. ## Coming soon:
  4. ## * Send notices to other PCs directly
  5. ## * Wrap the registration of new messages
  6. ## * Figure out the stupid
  7.  
  8. ## Change these to whatever you like, at least the first one, since it should point at a real ico file
  9. $defaultIcon = "$PSScriptRoot\PowerGrowl.ico"
  10. $appName = "PowerGrowler"
  11.  
  12. [Reflection.Assembly]::LoadFrom("$(Split-Path (gp HKCU:\Software\Growl).'(default)')\Growl.Connector.dll") | Out-Null
  13.  
  14. if(!(Test-Path Variable:Script:PowerGrowler)) {
  15.    $script:GrowlApp  = New-Object "Growl.Connector.Application" $appName
  16.    $script:PowerGrowler = New-Object "Growl.Connector.GrowlConnector"
  17.    $script:PowerGrowler.EncryptionAlgorithm = [Growl.Connector.Cryptography+SymmetricAlgorithmType]::AES
  18.  
  19.    [Growl.Connector.NotificationType[]]$global:PowerGrowlerNotices = @("Default")
  20.    ## You should change this
  21.    $global:PowerGrowlerNotices[0].Icon = $defaultIcon
  22. }
  23.  
  24. ## I was going to store these ON the PowerGrowler object, but ...
  25. ## I wanted to make the notices editable, without making it editable
  26. ## So instead, I add them to it only after you register them....
  27. # Add-Member -InputObject $script:PowerGrowler -MemberType NoteProperty -Name Notices -Value $PowerGrowlerNotices
  28.  
  29. ## Should only (have to) do this once.
  30. $script:PowerGrowler.Register($script:GrowlApp, $global:PowerGrowlerNotices )
  31.  
  32.  
  33. function Send-Growl {
  34. #.Synopsis
  35. #  Send a growl notice
  36. #.Description
  37. #  Send a growl notice with the scpecified values
  38. #.Parameter Caption
  39. #  The short caption to display
  40. #.Parameter Message
  41. #  The message to send (most displays will resize to accomodate)
  42. #.Parameter NoticeType
  43. #  The type of notice to send. This MUST be the name of one of the registered types, and senders should bear in mind that each registered type has user-specified settings, so you should not abuse the types, but create your own for messages that will recur.
  44. #  For example, the user settings allow certain messages to be disabled, set to a different "Display", or to have their Duration and Stickyness changed, as well as have them be Forwarded to another device, have Sounds play, and set different priorities.
  45. #.Parameter Icon
  46. #  Overrides the default icon of the message (accepts .ico, .png, .bmp, .jpg, .gif etc)
  47. #.Parameter Priority
  48. #  Overrides the default priority of the message (use sparingly)
  49. #.Example
  50. #  Send-Growl "Greetings" "Hello World!"
  51. #
  52. #  The Hello World of Growl.
  53. #.Example
  54. #  Send-Growl "You've got Mail!" "Message for you sir!" -icon ~\Icons\mail.png
  55. #
  56. #  Displays a message with a couple of movie quotes and a mail icon.
  57. #
  58. PARAM (
  59. #   [Parameter(Mandatory=$true, Position=0)]
  60.    [string]$Caption=$(Read-Host "A SHORT caption")
  61. ,
  62. #   [Parameter(Mandatory=$true, Position=1)]
  63.    [string]$Message=$(Read-Host "The detailed message")
  64. #,  [string]$CallbackData
  65. #,  [string]$CallbackType
  66. ,
  67. #   [Parameter(Mandatory=$false)][Alias("Type")]  
  68.    [string]$NoticeType=$global:PowerGrowlerNotices[0].Name
  69. ,
  70.    [string]$Icon
  71. ,
  72.    [Growl.Connector.Priority]$Priority = "Normal"
  73. )
  74.  
  75.    $notice = New-Object Growl.Connector.Notification $appName, $NoticeType, (Get-Date).Ticks.ToString(), $caption, $Message
  76.    
  77.    if($Icon) { $notice.Icon = Convert-Path (Resolve-Path $Icon) }
  78.    if($Priority) { $notice.Priority = $Priority }
  79.    
  80.    if($DebugPreference -gt "SilentlyContinue") { Write-Output $notice }
  81.    if( $CallbackData -and $CallbackType ) {
  82.       $context = new-object Growl.Connector.CallbackContext
  83.       $context.Data = $CallbackData
  84.       $context.Type = $CallbackType
  85.       $script:PowerGrowler.Notify($notice, $context)
  86.    } else {
  87.       $script:PowerGrowler.Notify($notice)
  88.    }
  89. }
  90.  
  91. Export-ModuleMember -Function Send-Growl

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me