5
0

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.

wsl2のGNU emacsからWindowsのデスクトップ通知を行う

Posted at

環境

この記事の内容は、
・Windows10
・wsl2 Ubuntu 20.04LTS
・GNU emacs 29 (wsl2でソースからビルド)
・X-server環境でGUI emacs起動
で確認しています。
が、wsl2で動かすemacsなら同じように動作できると思います。

前提

普段、タスク・スケジュール管理に、emacs の org-mode , org-agenda を使っています。

org-mode, org-agenda については、
https://maskaw.hatenablog.com/entry/2018/09/21/205910
などご参考に。

org-mode,org-agendaのすごいところ

org-modeで作業環境母艦としてのemacsから離れることなく、テキストでTODOを確認、編集できます。
org-agendaで、仕事、プライベート、一時メモなど複数のorgファイルからまとめてスケジュールを確認することが出来ます。
現時点(2022/06)、世界最高のオーガナイザーと思います。

今回やりたいこと

org-modeでTODOの日時を入れて、時間が来たらWindowsのデスクトップに自動通知

です。
Linux/MacOSではさほど悩まずにgrowlやterminal_notifierで実現できるのでしょうが、wsl2 なので実現した例を見つけることが出来ませんでした。

[準備1] wsl2からデスクトップ通知するpowershellスクリプトを作成する

を、引数2つ、titleとmessageを受け取れるように少し改造して保存します。

[準備2] emacsからpowershellスクリプトを呼び出すシェルを作成する

wsl2側からwindowの実行ファイルを起動できるので、PowerShellをオプションつけて、先程のデスクトップ通知するスクリプトを起動させます。

terminal_notifier.sh
#!/usr/bin/sh
token=$1
shift
name=$*
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Sta -NoProfile -WindowStyle Hidden -ExecutionPolicy RemoteSigned -File d:/Wintool/emacs/home/notify.ps1 "$token" "$name" &

シェルに実行権限を加えておきます。
wsl2 terminal> chmod a+x terminal_notifier.sh

[準備3] emacsに、org-alertを導入して通知時の内容を変更する

emacsに、org-alertを導入します。
https://github.com/spegoraro/org-alert
その中のファイルで、alert.elを使って出力する部分を改造し、先程のpowershellをlisp shell-command で呼び出すようにします。

org-alert.el
(Defun org-alert--dispatch ()
  (let* ((entry (org-alert--parse-entry))
	 (head (car entry))
	 (time (cadr entry)))
    (if time
	(when (org-alert--check-time time)
-	  (alert (concat time ": " head) :title org-alert-notification-title))
+    (shell-command (concat "/mnt/d/Wintool/emacs/home/terminal-notifier.sh"  " '" head "'"  " '"  time "'"))
		(alert head :title org-alert-notification-title))
  (alert head :title org-alert-notification-title))))

[準備4] Init.el設定追加

init.el
(require 'org-alert)
(add-hook 'org-agenda-mode-hook 'org-alert-enable)

動作確認

org-agendaグループ内のorgファイルにSCHEDULED:が定義されたTODOを定義し、org-agendaでアジェンダビューしました。
orgファイルのSCHEDULEDに日時を設定する.png

でました!デスクトップ トースト通知。
Windows側にデスクトップ通知.png

最後に

  • これで打ち合わせに遅れることもなくなるぞ!
  • もっとシンプルな実現方法があれば。
  • outlookの予定表とorg-modeのTODOを同期できたらいいな。
5
0
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
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?