如何制作Mac终端弹出/警报?AppleScript的?

JSh*_*hoe 97 macos terminal alert applescript popup

我希望能够让我的程序显示警报,通知,显示我的自定义文本.这是怎么做到的?此外,是否可以使用几个按钮设置变量?

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

Ann*_*nne 198

使用osascript.例如:

osascript -e 'tell app "Finder" to display dialog "Hello World"' 
Run Code Online (Sandbox Code Playgroud)

用您想要的任何应用程序替换"Finder".请注意,如果该应用程序是背景的,则对话框也将显示在后台.要始终显示在前台,请使用"系统事件"作为应用程序:

osascript -e 'tell app "System Events" to display dialog "Hello World"'
Run Code Online (Sandbox Code Playgroud)

阅读更多关于Mac OS X的提示.

  • 如果您不想要"取消"按钮但只想要一个"确定"按钮,请用{alert}替换{dialog}. (6认同)

Enr*_*sso 60

如果您使用的是具有Notification Center的任何Mac OS X版本,则可以使用terminal-notifier gem.首先安装它(您可能需要sudo):

gem install terminal-notifier
Run Code Online (Sandbox Code Playgroud)

然后简单地说:

terminal-notifier -message "Hello, this is my message" -title "Message Title"
Run Code Online (Sandbox Code Playgroud)

另请参阅此OS X每日帖子.

  • 这比旧的osascript东西要好得多. (5认同)
  • **PSA:**在Mavericks上以后不需要,只需使用osascript的[显示通知](https://apple.stackexchange.com/questions/57412/how-can-i-trigger-a-notification-中心通知来自一个苹果 - 或 - shel),这在Pradeep的答案中提到. (5认同)
  • 如果您喜欢冲泡,`brew install terminal-notifier`也可以使用. (4认同)

Pra*_*ani 57

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

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
Run Code Online (Sandbox Code Playgroud)


and*_*Oak 11

向通知添加子标题标题声音

使用AppleScript

display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"
Run Code Online (Sandbox Code Playgroud)

使用终端/bashosascript

osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'
Run Code Online (Sandbox Code Playgroud)

一个警报可以显示代替的通知

不采用副标题,也不采用强硬的声音。

使用AppleScript

display alert "Alert title" message "Your message text line here."
Run Code Online (Sandbox Code Playgroud)

使用终端/bashosascript

osascript -e 'display alert "Alert title" message "Your message text line here."'
Run Code Online (Sandbox Code Playgroud)

bash 中添加一行用于在警报行之后播放声音

afplay /System/Library/Sounds/Hero.aiff
Run Code Online (Sandbox Code Playgroud)

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

do shell script ("afplay /System/Library/Sounds/Hero.aiff")
Run Code Online (Sandbox Code Playgroud)

可从此处选择的 macOS 内置声音列表。

转述自一篇关于终端和 Applescript 通知的方便文章。


Lri*_*Lri 6

如果答案为空,这会将焦点恢复到以前的应用程序并退出脚本.

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
Run Code Online (Sandbox Code Playgroud)

如果您告诉系统事件显示对话框,如果之前没有运行,则会有一个小延迟.

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


K. *_*erg 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"'
Run Code Online (Sandbox Code Playgroud)

组合更多命令的灵感奖励示例;这也会让 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
Run Code Online (Sandbox Code Playgroud)


tej*_*s n 5

简单通知

\n

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

\n

带标题的通知

\n

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

\n

通知并发出声音

\n

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

\n

带变量的通知

\n

osascript -e \'display notification "\'"$TR_TORRENT_NAME has finished downloading!"\'" with title " \xe2\x9c\x94 Transmission-daemon"\'

\n

学分: https: //code-maven.com/display-notification-from-the-mac-command-line

\n