如何在Mac终端中弹出警告窗口?使用Applescript吗?

131

我希望我的程序能够显示一个弹框或者通知,里面展示我自己的文字。这个怎么实现呢?还有,是否可以制作一个带有多个按钮的弹框来设置一个变量?

类似于批处理中的: echo msgbox""<a.vbs&a.vbs

8个回答

241

使用osascript。例如:

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

将“Finder”替换为您想要的任何应用程序。请注意,如果该应用程序在后台运行,则对话框也会出现在后台。要始终显示在前台,请使用“系统事件”作为应用程序:

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

请访问Mac OS X Hints了解更多关于IT技术的内容。


1
AppleScript引擎会自动替换它。只需将该行代码粘贴在AppleScript编辑器中,当您点击运行时,它会在执行之前自动将“app”替换为“application”。 - Anne
12
如果您不想要“取消”按钮,只想要一个“确定”按钮,请将{dialog}替换为{alert}。 - Bart B
嗨,安妮,是否有可能使用终端打开对话框或任何其他方式向连接在局域网上的其他Mac发送消息? - iBhavik
1
13havik,我知道这是一个老话题,但是是的,你可以。 :) 但是你需要登录。例如通过终端ssh。然后调用osascript -e 'display dialog "Hello!"' - Alex Reds
我该如何显示一个没有按钮的对话框或警告框?我想要闪现一系列文本消息,不想有任何按钮出现,并希望控制消息显示的时间长度。 - adamlogan
显示剩余2条评论

71

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

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

如果焦点在,通知将不会显示。 - Dan Dascalescu

62
如果您使用的是带有通知中心的任何Mac OS X版本,则可以使用terminal-notifier gem。首先安装它(您可能需要sudo):
gem install terminal-notifier

然后简单地:
terminal-notifier -message "Hello, this is my message" -title "Message Title"

请参见这篇OS X Daily文章

5
这比旧的osascript工具简单得多,效果也好得多。 - Jonny
这似乎在10.7.5(Lion)中无法工作,显然它没有通知中心。 - Norman H
6
如果你更喜欢使用brew,那么brew install terminal-notifier也可以起作用。 - gklka
6
重要提示:在Mavericks及其后续版本中,不需要执行以下操作,只需使用osascript的[display notification]命令即可触发通知中心通知。Pradeep的回答中提到了该命令。 - alexchandel

13

添加通知的副标题、标题和声音:

使用AppleScript:

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

使用终端/bashosascript

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

可以显示警报而不是通知

没有子标题或声音。

使用AppleScript

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

使用终端/bashosascript

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

bash中添加一行代码以在警报行后播放声音

afplay /System/Library/Sounds/Hero.aiff

AppleScript中添加同一行,让shell脚本完成工作:

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

macOS内置这里选择的声音列表

从一篇关于终端和AppleScript通知的便利文章中改编而来。


7
如果答案为空,这将恢复到先前的应用程序并退出脚本。
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

如果您要求System Events显示对话框,如果它之前没有运行,可能会有一小段延迟。
关于“display dialog”的文档,请在AppleScript编辑器中打开标准附加项的字典,或参见AppleScript语言指南

5

我的 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

4

简单通知

osascript -e 'display notification "你好,世界!"'

带标题的通知

osascript -e 'display notification "你好,世界!" with title "这是标题"'

通知并播放声音

osascript -e 'display notification "你好,世界!" with title "问候" sound name "潜艇"'

带变量的通知

osascript -e 'display notification "'"$TR_TORRENT_NAME 下载完成!"'" with title " ✔ Transmission-daemon"'

来源:https://code-maven.com/display-notification-from-the-mac-command-line


-2
我写了一个脚本来解决这个问题,可以在这里找到。 安装方法:
brew install akashaggarwal7/tools/tsay
使用方法:
sleep 5; tsay

欢迎贡献!


17
“不需要任何额外的软件...只需安装这个”,也就是说那个额外的软件。 - PRS
@PRS 是一个运行在 macOS 上的脚本,它可以执行已经存在的命令,而不是一个软件。 - Akash Agarwal
4
那为什么我需要安装brew和你的脚本呢?你懂我的意思吧? ;) - PRS
不过我要感谢你向我介绍了say命令...今天我学到了新东西!哈哈 - PRS
@PRS 很高兴你发现了 say 命令,它非常有用。关于你的另一个评论,我认为如果你是开发人员,使用 brew 是相当普遍的。至于安装脚本,可以随意打开脚本源代码并将其复制/粘贴到本地文件中,然后 chmod +x 并运行它 ;) - Akash Agarwal
他只是想为其他人提供一种简单的方式来运行他的简单脚本 - 并不是互联网上的每个人都是程序员:D - Brad Parks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接