Skip to content

Instantly share code, notes, and snippets.

@TylerLeonhardt
Last active November 9, 2018 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TylerLeonhardt/3f942e236338a946891679898b8f923d to your computer and use it in GitHub Desktop.
Save TylerLeonhardt/3f942e236338a946891679898b8f923d to your computer and use it in GitHub Desktop.
Show notifications on Linux using PowerShell! Thanks to notify-send(1)
function Send-PSNotification {
[cmdletbinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory,ValueFromPipeline,Position=0)]
[object[]]
$Body,
[String]
$Summary = 'PowerShell Notification',
[ValidateSet('low', 'normal', 'critical')]
$Urgency,
[int]
$ExpireTime,
[string[]]
$Icon = "powershell-logo",
[ValidateSet("device","device.added","device.error","device.removed",
"email","email.arrived","email.bounced",
"im","im.error","im.received",
"network","network.connected","network.disconnected","network.error",
"presence","presence.offline","presence.online",
"transfer","transfer.complete","transfer.error")]
[string[]]
$Category
)
begin {
$notifySendArgs = @()
if ($Urgency) {
$notifySendArgs += "--urgency=$Urgency"
}
if ($ExpireTime) {
$notifySendArgs += "--expire-time=$ExpireTime"
}
if ($Icon) {
if ($Icon -eq "powershell-logo" -and !(Test-Path "$HOME/.local/share/icons/powershell-logo.png")) {
if (!(Test-Path "$HOME/.local/share/icons")) {
New-Item -ItemType Directory -Path "$HOME/.local/share/icons"
}
Copy-Item "$PSScriptRoot/../powershell-logo.png" "$HOME/.local/share/icons"
}
$notifySendArgs += "--icon=$($Icon -join ',')"
}
if ($Catagory) {
$notifySendArgs += "--category=$($Catagory -join ',')"
}
$notifySendArgs += $Summary
$notifySendArgs += ""
}
process {
$notifySendArgs[$notifySendArgs.Length - 1] = $Body
If ($PSCmdlet.ShouldProcess("notify-send $($notifySendArgs -join ' ')")) {
& "notify-send" $notifySendArgs
}
}
end {}
}
@steviecoaster
Copy link

Variable typo
catagory should be category

Instead of an arraylist I'd use an
[System.Collections.Generic.List[]] as they don't produce index output on their .Add() methods

@vexx32
Copy link

vexx32 commented Nov 9, 2018

Actually since this code uses += to add to the arraylist, this variable will be converted to a static array after the first operation. You will also need to switch to using the .Add() method.

@vexx32
Copy link

vexx32 commented Nov 9, 2018

Thinking on it a bit, given this is just adding strings, a StringBuilder would probably be more effective anyway. 🤷‍♂️

@KirkMunro
Copy link

The verb should be Send IMHO. Much easier to discover, and much more appropriate for what the command does.

@TylerLeonhardt
Copy link
Author

lets all keep in mind that I hacked this together in like 20min 😄

Thanks all for the feedback!

@TylerLeonhardt
Copy link
Author

@steviecoaster fixed the typo
@vexx32 switched it to a normal array cause I didn't really want to use iex...
@KirkMunro changed the verb 😄

@TylerLeonhardt
Copy link
Author

also on Elementary for some reason you can't specify a path for an icon. It only pulls from the ~/.local/share/icons folder so I added logic to add the PowerShell logo in there. Would love to use a ValidateScript or something to only allow files in that folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment