INDEX
デスクトップ通知(トースト通知)
PowerShell で、Windows のデスクトップ通知・トースト通知をする。
WindowsRuntime を使用するため、Windows PowerShell が対象。
トースト通知
基本
$headline = "headline text"
$message = "message text"
$xml = @"
<toast>
<visual>
<binding template="ToastGeneric">
<text>$($headline)</text>
<text>$($message)</text>
</binding>
</visual>
</toast>
"@
$doc = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]::New()
$doc.loadXml($xml)
$appid = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::CreateToastNotifier($appid).Show($doc)
テキスト
トーストに表示するテキストを、text 要素を最大3つのテキストが指定でき、合わせて6行分が表示される。1つ指定した場合は、太字で最大6行。2つまたは3つを指定した場合は、1つ目が太字で最大2行、2つ目と3つ目は普通(太字でない)となる。
<binding template="ToastGeneric">
<text>headline text</text>
<text>message text</text>
<text>second message text</text>
</binding>
改行
改行する場合は「`n」で改行する(改行された文字列はそのまま改行されて表示される)。
<text>second`nmessage`ntext</text>
表示行数
表示される最大行数を、text 要素の hint-maxLines 属性で指定する。実際に有効なのは、1つ目で、1行に制限するくらい。
<text hint-maxLines="1">headline text</text>
クレジット(帰属)
常に通知の下部に表示されるテキストで、text 要素の placement 属性で attribution を指定する。Anniversary Update で導入され、サポートされない古いバージョンでは、通常のテキストとして扱われる(テキスト要素が最大3つに達していない場合)。
<text placement="attribution">via Windows PowerShell</text>
ロゴ
メッセージの左側に表示されるアプリロゴを、image 要素の placement 属性で appLogoOverride を指定する(この配置の指定値は Win8 の実装に由来するらしい)。サイズは、100% スケーリングで 48 x 48 ピクセル。
<binding template="ToastGeneric">
<text>message text</text>
<image placement="appLogoOverride" hint-crop="circle" src="C:\Windows\IdentityCRL\WLive48x48.png" />
</binding>
円形にクリップする
アプリロゴを円形にクリップする場合は、hint-crop 属性で circle を指定する。
<image placement='appLogoOverride' hint-crop='circle' src='$($logo_path)' />
イメージ
トーストに表示する画像を、image 要素で、src 属性でファイルパスを指定する。複数可能だがその分長くなる。
<binding template="ToastGeneric">
<text>message text</text>
<image src="C:\Windows\Web\Screen\img100.jpg" />
</binding>
ヒーローイメージ
トーストの上に画像が表示されるヒーローイメージを、image 要素の placement 属性で hero を指定する。サイズは、100% スケーリングで 364 x 180 ピクセル。
<image placement="hero" src="C:\Windows\Web\Screen\img100.jpg" />
通知音
トーストを表示した際の通知音を audio 要素で指定(変更)できる。
<toast> <visual>〜</visual> <audio src="ms-winsoundevent:Notification.Default" loop="false" silent="false" /> </toast>
利用可能な通知音は https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio#attributes
ループ
loop 属性で true を指定する。トーストを消すまで通知音が鳴り続ける。
サイレント
silent 属性で true を指定する。トースト通知が表示される際に通知音がならない。
表示時間
長く表示される通知
トーストが表示される時間を、toast 要素の duration 属性で long または short を指定する。指定なし または short で約5秒、long で約25秒間表示される。
<toast duration="long">
アクション
トーストをクリック
表示されたトーストをクリックしたときにアクションする(例:Google のサイトを開く)。toast 要素で、activationType 属性に protocol 、launch 属性で開く URL やファイルパスを指定する(関連付けで開く)。
<toast activationType="protocol" launch="https://www.google.com/">
<visual>
<binding template="ToastGeneric">
<text>Click to open Google</text>
</binding>
</visual>
</toast>
アクションボタン
トースト下部にボタンを表示して、ボタンが押された時にアクションをする(例:Google のサイトを開く)。actions 要素内に、action 要素でボタンを作成する。content 属性がキャプション、imageUri 属性でボタンに画像を表示できる。activationType 属性に protocol を指定して、arguments 属性で開く URL やファイルパスを指定する(関連付けで開く)。
<toast>
<visual>〜</visual>
<actions>
<action content="Open Google" activationType="protocol" arguments="https://www.google.com/" />
</actions>
</toast>
<action content="設定を開く" activationType="protocol" arguments="ms-settings:" imageUri="C:\Windows\System32\@EnrollmentToastIcon.png" /> <action content="メモ帳を開く" activationType="protocol" arguments="C:\Windows\notepad.exe" />
プログレスバー
プログレスバー付のトースト
表示した後、更新メソッドでステータスを更新する。プログレスバーの値やステータスは、更新されるように別途データを渡す。更新対象は、(同じアプリの) Tag と Group が同一のものが対象になる。
[String] $xml = @"
<?xml version="1.0" encoding="utf-8"?>
<toast>
<visual>
<binding template="ToastGeneric">
<text>PowerShell toast progress bar..</text>
<progress
title="{progressTitle}"
value="{progressValue}"
valueStringOverride="{progressValueString}"
status="{progressStatus}"/>
</binding>
</visual>
</toast>
"@
[String] $toast_tag = 'my_tag'
[String] $toast_group = 'my_group'
# 開始(表示)
[Object] $doc = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]::New()
$doc.loadXml($xml)
[Object] $toast = [Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime]::New($doc)
$toast.Tag = $toast_tag
$toast.Group = $toast_group
[Object] $dic = [System.Collections.Generic.Dictionary[String, String]]::New()
$dic.Add('progressTitle', 'progress title')
$dic.Add('progressValue', 0)
$dic.Add('progressValueString', '0/15 files')
$dic.Add('progressStatus', 'Running...')
$toast.Data = [Windows.UI.Notifications.NotificationData]::New($dic)
$toast.Data.SequenceNumber = 1
[String] $AppId = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::CreateToastNotifier($AppId).Show($toast)
# 更新
for ($index = 1; $index -le 15; $index++) {
Start-Sleep 1
[Object] $dic = [System.Collections.Generic.Dictionary[String, String]]::New()
$dic.Add('progressValue', $index / 15)
$dic.Add('progressValueString', "$index/15 files")
$toast = [Windows.UI.Notifications.NotificationData]::New($dic)
$toast.SequenceNumber = $index + 1
$result = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Update($toast, $toast_tag, $toast_group)
}
# 完了
[Object] $dic = [System.Collections.Generic.Dictionary[String, String]]::New()
$dic.Add('progressValue', 1)
$dic.Add('progressStatus', 'Completed!')
$toast = [Windows.UI.Notifications.NotificationData]::New($dic)
$toast.SequenceNumber = 16
$result = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Update($toast, $toast_tag, $toast_group)
リマインダー
アラーム
着信
参考
- Toast notification examples for PowerShell
- 【PowerShell】デスクトップ通知のスニペット【トースト通知】 #初心者 - Qiita
- 私PowerShellだけどあなたにトーストを届けたい(プログレスバー付) #Windows - Qiita
- トースト スキーマ - Windows UWP applications | Microsoft Learn
最終更新時間:2025年12月14日 14時00分37秒 指摘や意見などあればSandBoxのBBSへ。