Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何使Mac终端弹出/警报?苹果书?_Macos_Popup_Terminal_Applescript_Alert - Fatal编程技术网

Macos 如何使Mac终端弹出/警报?苹果书?

Macos 如何使Mac终端弹出/警报?苹果书?,macos,popup,terminal,applescript,alert,Macos,Popup,Terminal,Applescript,Alert,我希望能够让我的程序显示一个警报,通知,任何显示我的自定义文本。这是怎么做到的?另外,是否可以用几个按钮来设置一个变量 与批次类似: echo msgbox”“使用。例如: osascript -e 'tell app "Finder" to display dialog "Hello World"' 将“Finder”替换为您想要的任何应用程序。注意,如果该应用程序是后台的,则该对话框也将出现在后台。要始终显示在前台,请使用“系统事件”作为应用程序: osascript -e 'tell

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

与批次类似:
echo msgbox”“使用。例如:

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版本,您可以使用gem。首先安装它(您可能需要
sudo
):

然后简单地说:

terminal-notifier -message "Hello, this is my message" -title "Message 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编辑器中打开标准添加词典或查看。

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

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

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

我制作了一个脚本来解决这个问题。你不需要任何额外的软件。 安装:
brew安装akashaggarwal7/tools/tsay

用法:
sleep5;tsay

请随意贡献

在通知中添加副标题标题声音: 使用AppleScript

display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"
display alert "Alert title" message "Your message text line here."
使用终端/bash
osascript

osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'
osascript -e 'display alert "Alert title" message "Your message text line here."'
可以显示警报而不是通知 不带副标题也不带强硬的声音

使用AppleScript

display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"
display alert "Alert title" message "Your message text line here."
使用终端/bash
osascript

osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'
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的列表

摘自上一篇方便的文章。

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

附标题的通知
osascript-e'显示标题为“This is the title”的通知“hello world!”

通知并发出声音
osascript-e'显示标题为“问候”的通知“hello world!”,声音名称为“潜艇”

带有变量的通知
osascript-e'display notification'“$TR_TORRENT_NAME已完成下载!“'“带标题”✔  传输守护程序“”


信用证:

等等,您可以将应用程序缩写为app?AppleScript引擎将自动替换它。只需在AppleScript编辑器中的引号之间粘贴一行,当您点击Run时,它会在执行前自动将应用程序替换为应用程序。另一个键入保护程序:您不需要“如果结束”、“重复结束”等,只需“结束”即可,AppleScript将插入第二个单词。如果您不需要“取消”按钮,只需“确定”按钮将{dialog}替换为{alert}。13havik,我知道这是一个老话题,但是的,你可以。:)但是,您需要登录。e、 通过终端ssh。然后调用osascript-e'显示对话框“Hello!”“这比旧的osascript要好得多。这在10.7.5(Lion)中似乎不起作用,显然没有通知中心。
brew安装终端通知程序
如果您喜欢brew也可以。PSA:在Mavericks上,以后不需要,只需使用osascript,Pradeep在下面的回答中提到了这一点。“不需要任何额外的软件…只需安装这个”这是额外的软件。@PRS这是一个运行macOS可用命令的脚本,而不是一个软件。那么为什么我必须安装brew和您的脚本?明白我的意思吗?;)不过我要感谢你把我介绍给赛伊司令部。。。今天我学到了一些新东西!lol@PRS很高兴你遇到了say command,它非常有用。关于您的其他评论,我认为如果您是开发人员,brew对您来说非常常见。关于安装脚本,我们可以随意打开脚本的源代码,并将其复制/粘贴到本地文件中,然后chmod+x并运行;)