0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

業務中の自分をちょっと気遣ってくれるPowerShellスクリプト

Last updated at Posted at 2023-10-14

0. 動機

下記のQiita記事で「トースト通知」たるものを知って、これを使って何か作ってみようと思ったこと。

今回はテキストを表示するだけの若干お遊びなスクリプトですが、例えばスクリプトの中に「よく使う文字列をClipboardに書き込む」という機能を加えると、Windowsキー + Vで欲しい文字列がすぐに入力できる、という状況を作ることもできるかと思います。

その他、スクリプト起動したら完了報告にトースト通知が使う、とか便利だと思います。

1. スクリプト

1時間毎に「稼働時間」を表示し「休みましょう!」と呼び掛けてくれる心優しい機能となっています。

#時間に関わる変数の設定
$increTime = 0 #スクリプト起動時を0とカウントした時間
$spentMin = 0 #分数
$spentHour = 0 #時数

#コンソールへの表示(後述の無限ループから抜けるコマンドを表示する目的)
[Console]::Write("Running... press Ctrl + C to stop this program")

#無限ループ
while ($true) {

    #トースト通知生成(詳細は0.動機のところにあるQiita記事参照)
    $bodyText = "勤務開始してから $spentHour" + "時間$spentMin" + "分が経過しました。`n 1時間ごとに5分休憩しましょう!"
    $ToastText01 = [Windows.UI.Notifications.ToastTemplateType, Windows.UI.Notifications, ContentType = WindowsRuntime]::ToastText01
    $TemplateContent = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($ToastText01)
    $TemplateContent.SelectSingleNode('//text[@id="1"]').InnerText = $bodyText
    $AppId = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Show($TemplateContent)

    #変数の更新
    Start-Sleep -Seconds 3600 #1時間毎に表示するので3,600秒。表示間隔を変えるならここを調整。
    $increTime += 3600 #表示間隔を変えるならここも直前の列とあわせて調整。
    $spentHour = [math]::Floor($increTime/3600) #Math.Floorメソッドで小数点以下切り捨て
    $spentMin = [math]::Floor(($increTime-$spentHour*3600)/60)

}


2. 出力

下記のトースト通知が1時間毎に表示されます。(0時間0分の所は徐々に数値が増加していきます)
image.png

同時に立ち上がるコンソールは下記のような文字が表示されます。
image.png

3.おわりに

基本自分用メモですがどこかの誰かの役に立てば幸いです。

所定の文字列をClipboardに登録する操作については下記記事をご確認下さい。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?