Mac 上的 Bash 脚本创建信息弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1887146/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:28:34  来源:igfitidea点击:

Bash Script on a Mac creates a info popup

macosbash

提问by Arcath

Is there a way in bash on a mac to draw a pretty info box that displays a simple message like "please save all files to /Users/......"

有没有办法在 Mac 上的 bash 中绘制一个漂亮的信息框,显示一条简单的消息,例如“请将所有文件保存到 /Users/......”

回答by Shirkrin

You can run fragments of applescript from you bash scripts. A simple popup would look like this:

您可以从 bash 脚本运行 applescript 的片段。一个简单的弹出窗口如下所示:

#!/bin/bash
/usr/bin/osascript <<-EOF

    tell application "System Events"
        activate
        display dialog "Hello world"
    end tell

EOF

This will feed the applescript between the EOF tags to osascript and execute it
(resulting in a Hello World popup).

这会将 EOF 标记之间的 Applescript 提供给 osascript 并执行它
(导致 Hello World 弹出窗口)。

回答by mivk

An alternative to osascript "System Events" would be to install cocoaDialog.

osascript“系统事件”的替代方法是安装cocoaDialog

cocoaDialog has the disadvantage that it must be installed, but it seems to be much more flexible than the "System Events".

cocoaDialog 的缺点是必须安装,但它似乎比“系统事件”灵活得多。

The license is GPL, so you can freely redistribute it, since it is a separate application.

许可证是 GPL,因此您可以自由地重新分发它,因为它是一个单独的应用程序。

(osascript was littering my terminal with error messages (at least under Lion) and with return values, it didn't let me do popups with timeouts, and seemed to require specific quoting which made it hard to use variables in the texts.)

(osascript 用错误消息(至少在 Lion 下)和返回值散布我的终端,它不允许我使用超时弹出窗口,并且似乎需要特定的引用,这使得很难在文本中使用变量。)