如何制作 Mac 终端弹出窗口/警报?苹果脚本?

发布于 2024-10-30 18:59:58 字数 163 浏览 1 评论 0 原文

我希望能够让我的程序显示警报、通知以及显示我的自定义文本的任何内容。这是怎么做到的?另外,是否可以制作一个带有多个设置变量的按钮的按钮?

类似于批处理: echo msgbox""

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

闻呓 2024-11-06 18:59:58

使用 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 提示

Use osascript. For example:

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:

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

Read more on Mac OS X Hints.

万水千山粽是情ミ 2024-11-06 18:59:58

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

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

Use this command to trigger the notification center notification from the terminal.

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
草莓酥 2024-11-06 18:59:58

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

gem install terminal-notifier

然后简单地:

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

另请参阅此 OS X 每日帖子

If you're using any Mac OS X version which has Notification Center, you can use the terminal-notifier gem. First install it (you may need 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.

雨后彩虹 2024-11-06 18:59:58

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

使用AppleScript

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

使用terminal/bash< /strong> 和 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."

使用terminal/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")

可以从此处选择声音

摘自关于终端和 applescript 通知的便利文章。

Adding subtitle, title and a sound to the notification:

With AppleScript:

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

With terminal/bash and osascript:

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

An alert can be displayed instead of a notification

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

With AppleScript:

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

With terminal/bash and osascript:

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

Add a line in bash for playing the sound after the alert line:

afplay /System/Library/Sounds/Hero.aiff

Add same line in AppleScript, letting shell script do the work:

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.

幸福不弃 2024-11-06 18:59:58

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

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 语言指南

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.

For documentation about display dialog, open the dictionary of Standard Additions in AppleScript Editor or see the AppleScript Language Guide.

最笨的告白 2024-11-06 18:59:58

简单通知

osascript -e '显示通知“hello world!”'

带有标题的通知

osascript -e '显示通知“hello world!”带有标题“这是标题”'

通知并发出声音

osascript -e '显示通知“hello world!”标题为“Greeting”声音名称“Submarine”'

带变量的通知

osascript -e '显示通知“'“$TR_TORRENT_NAME 已完成下载!”'”标题为“✔ Transmission-daemon”'

学分: https://code-maven.com/display -来自-mac-命令行的通知

Simple Notification

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

Notification with title

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

Notify and make sound

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

Notification with variables

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

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

孤寂小茶 2024-11-06 18:59:58

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

And my 15 cent. A one liner for the mac terminal etc just set the MIN= to whatever and a message

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

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
身边 2024-11-06 18:59:58

我制作了一个脚本来解决这个问题,位于此处
安装:
brew 安装 akashaggarwal7/tools/tsay
用法:
睡5; tsay

欢迎贡献!

I made a script to solve this which is here.
Installation:
brew install akashaggarwal7/tools/tsay
Usage:
sleep 5; tsay

Feel free to contribute!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文