Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active July 13, 2016 07:46
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 guitarrapc/8426022 to your computer and use it in GitHub Desktop.
Save guitarrapc/8426022 to your computer and use it in GitHub Desktop.
toast sample in PowerShell not yet finished;
# Code conversion from C# to PowerShell
# http://msdn.microsoft.com/en-us/library/windows/apps/hh802768.aspx
# nuget Windows 7 API Code Pack -Shell
# http://nugetmusthaves.com/Package/Windows7APICodePack-Shell
# Install-Package Windows7APICodePack-Shell
Add-Type -Path .\Microsoft.WindowsAPICodePack.dll
Add-Type -Path .\Microsoft.WindowsAPICodePack.Shell.dll
# create toast template TO xml
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
[xml]$toastXml = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText04)).GetXml()
<#
# これでもいいが template type の違いに注意
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$toastTemplate = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01
[xml]$toastXml = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($toastTemplate).GetXml()
#>
# message to show on toast
$stringElements = $toastXml.GetElementsByTagName("text")
for ($i = 0; $i -lt $stringElements.Count; $i++)
{
$stringElements[$i].AppendChild($toastXml.CreateTextNode("Line " + $i)) > $null
}
<#
# こっちもあり
$stringElements = $toastXml.GetElementsByTagName("text") | select -First 1
$stringElements.AppendChild($toastXml.CreateTextNode("Test Toast Notification")) > $null
#>
# no image
$imageElements = $toastXml.GetElementsByTagName("image")
$imageElements[0].src = "file:///" + ""
# convert from System.Xml.XmlDocument to Windows.Data.Xml.Dom.XmlDocument
$windowsXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$windowsXml.LoadXml($toastXml.OuterXml)
$APP_ID = "hoge"
$shortcutPath = [Environment]::GetFolderPath([Environment+SpecialFolder]::ApplicationData) + "\Microsoft\Windows\Start Menu\Programs\hoge.lnk";
# send toast notification
$toast = New-Object Windows.UI.Notifications.ToastNotification ($windowsXml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast)
@guitarrapc
Copy link
Author

TODO

Required to use SHGetPropertyStoreForWindows from IPropertyStore object to set System.AppUserModel.ID
http://msdn.microsoft.com/en-us/library/windows/apps/dd391569.aspx

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