【发布时间】:2011-08-01 01:52:14
【问题描述】:
我希望能够让我的程序显示警报、通知,以及显示我的自定义文本的任何内容。这是怎么做到的?另外,是否可以用几个按钮来设置一个变量?
类似于批处理:
echo msgbox""<a.vbs&a.vbs
【问题讨论】:
标签: macos popup terminal applescript alert
我希望能够让我的程序显示警报、通知,以及显示我的自定义文本的任何内容。这是怎么做到的?另外,是否可以用几个按钮来设置一个变量?
类似于批处理:
echo msgbox""<a.vbs&a.vbs
【问题讨论】:
标签: macos popup terminal applescript alert
我编写了一个脚本来解决这个问题,它是here。
安装:brew install akashaggarwal7/tools/tsay
用法:sleep 5; tsay
请随意贡献!
【讨论】:
osascript -e 'display notification "hello world!"'
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
【讨论】:
使用 AppleScript:
display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"
使用 terminal/bash 和 osascript:
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."
使用 终端/bash 和 osascript:
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")
sounds to choose from here内置的macOS列表。
转述自 terminal and applescript notifications 上的一篇方便的文章。
【讨论】:
如果您使用任何具有通知中心的 Mac OS X 版本,您可以使用 terminal-notifier gem。首先安装它(你可能需要sudo):
gem install terminal-notifier
然后简单地说:
terminal-notifier -message "Hello, this is my message" -title "Message Title"
【讨论】:
brew install terminal-notifier 如果您喜欢酿造,也可以使用。
还有我的 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
【讨论】:
使用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。
【讨论】:
使用该命令从终端触发通知中心通知。
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
【讨论】:
如果答案为空,这会将焦点恢复到先前的应用程序并退出脚本。
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
如果您告诉系统事件显示对话框,如果之前没有运行,则会有一小段延迟。
有关显示对话框的文档,请在 AppleScript 编辑器中打开标准添加字典或查看AppleScript Language Guide。
【讨论】: