!!! デスクトップ通知(トースト通知) {{category Windows PowerShell,nolink}} PowerShell で、Windows のデスクトップ通知・トースト通知をする。 WindowsRuntime を使用するため、Windows PowerShell が対象。 !!!トースト通知 !!基本 $headline = "headline text" $message = "message text" $xml = @" $($headline) $($message) "@ $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つ目は普通(太字でない)となる。 headline text message text second message text !改行 改行する場合は「`n」で改行する(改行された文字列はそのまま改行されて表示される)。 second`nmessage`ntext !表示行数 表示される最大行数を、text 要素の hint-maxLines 属性で指定する。 実際に有効なのは、1つ目で、1行に制限するくらい。 headline text !クレジット(帰属) 常に通知の下部に表示されるテキストで、text 要素の placement 属性で attribution を指定する。 Anniversary Update で導入され、サポートされない古いバージョンでは、通常のテキストとして扱われる(テキスト要素が最大3つに達していない場合)。 via Windows PowerShell !!ロゴ メッセージの左側に表示されるアプリロゴを、image 要素の placement 属性で appLogoOverride を指定する(この配置の指定値は Win8 の実装に由来するらしい)。 サイズは、100% スケーリングで 48 x 48 ピクセル。 message text !円形にクリップする アプリロゴを円形にクリップする場合は、hint-crop 属性で circle を指定する。 !!イメージ トーストに表示する画像を、image 要素で、src 属性でファイルパスを指定する。複数可能だがその分長くなる。 message text !ヒーローイメージ トーストの上に画像が表示されるヒーローイメージを、image 要素の placement 属性で hero を指定する。 サイズは、100% スケーリングで 364 x 180 ピクセル。 !!通知音 トーストを表示した際の通知音を audio 要素で指定(変更)できる。 利用可能な通知音は 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秒間表示される。 !!!アクション !!トーストをクリック 表示されたトーストをクリックしたときにアクションする(例:Google のサイトを開く)。 toast 要素で、activationType 属性に protocol 、launch 属性で開く URL やファイルパスを指定する(関連付けで開く)。 Click to open Google !!アクションボタン トースト下部にボタンを表示して、ボタンが押された時にアクションをする(例:Google のサイトを開く)。 actions 要素内に、action 要素でボタンを作成する。content 属性がキャプション、imageUri 属性でボタンに画像を表示できる。 activationType 属性に protocol を指定して、arguments 属性で開く URL やファイルパスを指定する(関連付けで開く)。 !!!プログレスバー !!プログレスバー付のトースト 表示した後、更新メソッドでステータスを更新する。 プログレスバーの値やステータスは、更新されるように別途データを渡す。 更新対象は、(同じアプリの) Tag と Group が同一のものが対象になる。 [String] $xml = @" PowerShell toast progress bar.. "@ [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 ** https://github.com/GitHub30/toast-notification-examples * 【PowerShell】デスクトップ通知のスニペット【トースト通知】 #初心者 - Qiita ** https://qiita.com/relu/items/b7121487a1d5756dfcf9 * トースト スキーマ - Windows UWP applications | Microsoft Learn ** https://learn.microsoft.com/ja-jp/uwp/schemas/tiles/toastschema/schema-root