我如何做一个 Mac 终端弹出/警报?

我希望我的程序能够显示一个警告,通知,无论是显示我的自定义文本。这是怎么做到的?还有,有没有可能做一个带有几个按钮来设置一个变量?

类似批次: echo msgbox""<a.vbs&a.vbs

133428 次浏览

使用 osascript,例如:

osascript -e 'tell app "Finder" to display dialog "Hello World"'

用你想要的应用程序替换“ Finder”。注意,如果该应用程序是背景,对话框也会出现在背景中。要总是显示在前台,使用“ System Events”作为应用程序:

osascript -e 'tell app "System Events" to display dialog "Hello World"'

阅读更多 Mac OS X Hints

如果您正在使用任何具有 NotificationCenter 的 MacOSX 版本,则可以使用 终端通知器终端通知器 gem。首先安装它(你可能需要 sudo) :

gem install terminal-notifier

然后简单地说:

terminal-notifier -message "Hello, this is my message" -title "Message Title"

参见 这个 OS X 日报的帖子

这将使焦点恢复到前一个应用程序,并在答案为空时退出脚本。

a=$(osascript -e 'try
tell app "SystemUIServer"
set answer to text returned of (display dialog "" default answer "")
end
end
activate app (path to frontmost application as text)
answer' | tr '\r' ' ')
[[ -z "$a" ]] && exit

如果您告诉 SystemEvents 显示对话框,那么如果它之前没有运行,则会有一个小的延迟。

有关显示对话框的文档,请在 AppleScript 编辑器中打开“标准添加”字典,或参见 AppleScript 语言指南

使用此命令可以从终端触发通知中心通知。

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

还有我的15美分。一个用于 Mac 终端的一行程序,等等,只是将 MIN = 设置为任意值和一条消息

MIN=15 && for i in $(seq $(($MIN*60)) -1 1); do echo "$i, "; sleep 1; done; echo -e "\n\nMac Finder should show a popup" afplay /System/Library/Sounds/Funk.aiff; osascript -e 'tell app "Finder" to display dialog "Look away. Rest your eyes"'

一个额外的例子来激发灵感组合更多的命令; 这将把一个 mac 放置备用睡眠的消息太:)那么需要 sudo 登录,乘以60 * 2两个小时

sudo su
clear; echo "\n\nPreparing for a sleep when timers done \n"; MIN=60*2 && for i in $(seq $(($MIN*60)) -1 1); do printf "\r%02d:%02d:%02d" $((i/3600)) $(( (i/60)%60)) $((i%60)); sleep 1; done; echo "\n\n Time to sleep  zzZZ";  afplay /System/Library/Sounds/Funk.aiff; osascript -e 'tell app "Finder" to display dialog "Time to sleep zzZZ"'; shutdown -h +1 -s

我做了一个脚本来解决这个问题,就是 给你。 安装:
brew install akashaggarwal7/tools/tsay
用法:
sleep 5; tsay

尽管贡献吧!

在通知中添加 字幕书名sound:

With AppleScript:

display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"

terminal/bashosascript一起:

osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'

可以显示 警报而不是 通知

Does not take the sub-heading nor the sound tough.

AppleScript:

display alert "Alert title" message "Your message text line here."

终端/重击osascript一起:

osascript -e 'display alert "Alert title" message "Your message text line here."'

Add a line in Bash for 播放声音 after the alert line:

afplay /System/Library/Sounds/Hero.aiff

AppleScript中添加相同的代码行,让 Shell 脚本完成这项工作:

do shell script ("afplay /System/Library/Sounds/Hero.aiff")

内置在 可供选择的声音中的 macOS 列表。

摘自 终端和应用程序通知上的一篇便利文章。

Simple Notification

osascript -e 'display notification "hello world!"'

Notification with title

osascript -e 'display notification "hello world!" with title "This is the title"'

通知并发出声音

osascript -e 'display notification "hello world!" with title "Greeting" sound name "Submarine"'

带变量的通知

osascript -e 'display notification "'"$TR_TORRENT_NAME has finished downloading!"'" with title " ✔ Transmission-daemon"'

片名: https://code-maven.com/display-notification-from-the-mac-command-line