阿猫的博客

阿猫的博客

macOS 命令完成后展示一条通知

2025-07-08

执行以下命令可以展示一条通知:

osascript -e 'display notification "The command finished" with title "Success"'

所以在 .zshrc 中定义一个函数:

function notifyMe () {
  if [ $? -eq 0 ]; then
    osascript -e 'display notification "The command finished" with title "Success"'
  else
    osascript -e 'display notification "The command failed" with title "Failed"'
  fi
}

在运行某些需要比较长时间的程序时,执行以下命令:

> some_program; notifyMe

在执行完便能收到一条通知:

甚至可以设置通知音效(见下面第二条链接)

References

How to Use macOS Notifications with Command-line Programs – Small Sharp Software Tools
MacOS: notify when the terminal command is finished