teh*_*ehp 3 macos terminal zsh
有没有办法在命令完成运行时强制终端成为活动窗口?
当运行冗长的命令或脚本时,在它们完成时以某种方式通知它们将非常有用。
或者,我可以在每次命令完成时通过 macOS 发送通知吗?
在 macOS 10.8+ 上,AppleScript 支持display notification显示通知的命令(可选带有声音)。
这是一个示例,sleep 3用于表示长时间运行的 shell 命令:
sleep 3; osascript -e 'display notification "Done!" with title "Long-running shell command" sound name "Hero"'
Run Code Online (Sandbox Code Playgroud)
您可以将此功能包装在 shell 函数中并将其添加到您的~/.zshrc文件中:
notifyWhenDone() {
local msg=${1:-'Done!'} title=${2:-'Long-running shell command'} sound=${3:-'Hero'}
osascript -e 'display notification "'"$msg"'" with title "'"$title"'" sound name "'"$sound"'"'
}
Run Code Online (Sandbox Code Playgroud)
注:为了使这个完全健壮,你必须确保的可选参数的内容$1,$2以及$3不破的AppleScript命令。
有了这个功能:
sleep 3; notifyWhenDone
Run Code Online (Sandbox Code Playgroud)
如果您还想在完成时激活 Terminal.app,请将以下选项附加到传递给osascript:的参数
-e 'activate application "Terminal"'。