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

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

這篇文章分享了,如何用程式化的方式在 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!

也許你也會想看看