macos 如何制作 Mac 终端弹出/警报?苹果文字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5588064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a Mac Terminal pop-up/alert? Applescript?
提问by JShoe
I want to be able to have my program display an alert, notice, whatever that displays my custom text. How is this done? Also, is it possible to make one with several buttons that sets a variable?
我希望能够让我的程序显示警报、通知,无论显示我的自定义文本。这是怎么做的?另外,是否可以制作一个带有多个设置变量的按钮?
Similar to batch's:
echo msgbox""<a.vbs&a.vbs
类似于批处理:
echo msgbox""<a.vbs&a.vbs
回答by Anne
Use osascript
. For example:
使用osascript
. 例如:
osascript -e 'tell app "Finder" to display dialog "Hello World"'
Replacing “Finder” with whatever app you desire. Note if that app is backgrounded, the dialog will appear in the background too. To always show in the foreground, use “System Events” as the app:
用您想要的任何应用程序替换“Finder”。请注意,如果该应用程序是后台的,该对话框也会出现在后台。要始终显示在前台,请使用“系统事件”作为应用程序:
osascript -e 'tell app "System Events" to display dialog "Hello World"'
Read more on Mac OS X Hints.
阅读有关Mac OS X 提示的更多信息。
回答by Enrico Carlesso
If you're using any Mac OS X version which has Notification Center, you can use the terminal-notifiergem. First install it (you may need sudo
):
如果您使用任何具有通知中心的 Mac OS X 版本,您可以使用终端通知程序gem。首先安装它(您可能需要sudo
):
gem install terminal-notifier
and then simply:
然后简单地:
terminal-notifier -message "Hello, this is my message" -title "Message Title"
See also this OS X Daily post.
另请参阅此 OS X Daily post。
回答by Pradeep Vairamani
Use this command to trigger the notification center notification from the terminal.
使用该命令从终端触发通知中心通知。
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
回答by Lri
This would restore focus to the previous application and exit the script if the answer was empty.
如果答案为空,这会将焦点恢复到前一个应用程序并退出脚本。
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
If you told System Events to display the dialog, there would be a small delay if it wasn't running before.
如果您告诉 System Events 显示对话框,如果它之前没有运行,将会有一个小的延迟。
For documentation about display dialog, open the dictionary of Standard Additions in AppleScript Editor or see the AppleScript Language Guide.
有关显示对话框的文档,请在 AppleScript 编辑器中打开 Standard Additions 词典或参阅AppleScript 语言指南。
回答by K. Kilian Lindberg
And my 15 cent. A one liner for the mac terminal etc just set the MIN= to whatever and a message
还有我的 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"'
A bonus example for inspiration to combine more commands; this will put a mac put to standby sleep upon the message too :) the sudo login is needed then, a multiplication as the 60*2 for two hours goes aswell
组合更多命令的灵感奖励示例;这也会让 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
回答by andyNilson
Adding subtitle, titleand a soundto the notification:
向通知添加子标题、标题和声音:
With AppleScript:
使用AppleScript:
display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"
With terminal/bashand osascript
:
使用终端/bash和osascript
:
osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'
An alertcan be displayed instead of a notification
一个警报可以显示代替的通知
Does not take the sub-heading nor the sound tough.
不采用副标题,也不采用强硬的声音。
With AppleScript:
使用AppleScript:
display alert "Alert title" message "Your message text line here."
With terminal/bashand osascript
:
使用终端/bash和osascript
:
osascript -e 'display alert "Alert title" message "Your message text line here."'
Add a line in bashfor playing the soundafter the alert line:
在bash 中添加一行用于在警报行之后播放声音:
afplay /System/Library/Sounds/Hero.aiff
Add same line in AppleScript, letting shell scriptdo the work:
在AppleScript 中添加相同的行,让shell 脚本完成工作:
do shell script ("afplay /System/Library/Sounds/Hero.aiff")
List of macOS built in sounds to choose from here.
Paraphrased from a handy article on terminal and applescript notifications.
转述自一篇关于终端和 Applescript 通知的方便文章。
回答by Mark
A simular question and answer is available at: https://superuser.com/questions/246353/how-to-make-the-terminal-do-a-pop-up-alert-in-osx
一个类似的问题和答案可在:https: //superuser.com/questions/246353/how-to-make-the-terminal-do-a-pop-up-alert-in-osx