Mac - 如何用程式發出本機系統通知

分享如何在 Mac 系統中使用程式碼觸發系統通知(System Notification)。對於需要執行長時間程序的使用者,本文提供一套自動化提醒方案,讓程式在任務結束後主動通知開發者,有效節省等待時間並提升工作流程效率。

發佈
閱讀
約 1 分鐘
字數
357
⚠️ 舊文提醒:本文發佈於約 4 年前,部分內容或指令可能已過時,請斟酌參考。

有時候你的程式會需要等待一段時間再回來看
如果有辦法可以在程式結束時,可以用某種方式通知你
就可以省下一些時間

這篇文章分享了,如何用程式化的方式在 Mac 發出系統通知

Sometimes, you need to execute some process for a while.
If there is a way to notify you when your process is finished.
It will save a lot of time.

Right here, I show you how to activate a Mac system notification programmatically.

系統通知 System Notification

這是發出系統通知的指令
This is the command that can activate a system notification.

1
osascript -e 'display notification "hello world!" with title "Script Editor"'

如果在 Python 中呼叫,直接呼叫 os.system 函式就可以了
If you are using Python, you can just call os.system.

1
2
import os
os.system(f'osascript -e \'display notification "hello world!" with title "Script Editor"\'')

執行結果 Execute result
image 57

聲音通知 Sound Notification

聲音通知可以支援中英文
However, sound notification can support English and Mandarin.

1
say 通知

如果在 Python 中呼叫,那就會變成
If you are using Python, you can do it like this.

1
2
import os
os.system("say 通知")

完成 👍
Done!

也許你也會想看看

輸入關鍵字開始搜尋 · ↑↓ 選擇 · Enter 開啟