【问题标题】:How do I make a Mac Terminal pop-up/alert? Applescript?如何制作 Mac 终端弹出/警报?苹果脚本?
【发布时间】:2011-08-01 01:52:14
【问题描述】:

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

类似于批处理: echo msgbox""<a.vbs&a.vbs

【问题讨论】:

    标签: macos popup terminal applescript alert


    【解决方案1】:

    我编写了一个脚本来解决这个问题,它是here。 安装:
    brew install akashaggarwal7/tools/tsay
    用法:
    sleep 5; tsay

    请随意贡献!

    【讨论】:

    • “不需要任何额外的软件......只需安装这个”就是额外的软件。
    • @PRS 它是一个运行 macOS 可用命令的脚本,而不是软件。
    • 那我为什么要安装 brew 和你的脚本呢?明白了吗? ;)
    • 不过,我要感谢你向我介绍了 say 命令……我今天学到了一些新东西!大声笑
    • @PRS 很高兴您遇到 say 命令,它非常有用。关于您的其他评论,我认为如果您是开发人员,那么您可以使用 brew。关于安装脚本,请随意打开脚本的源代码并将其复制/粘贴到本地文件中,然后 chmod +x 并运行它;)
    【解决方案2】:

    简单通知

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

    带有标题的通知

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

    通知并发出声音

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

    带有变量的通知

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

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

    【讨论】:

      【解决方案3】:

      在通知中添加subtitletitlesound

      使用 AppleScript

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

      使用 terminal/bashosascript:

      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."
      

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

      sounds to choose from here内置的macOS列表。

      转述自 terminal and applescript notifications 上的一篇方便的文章。

      【讨论】:

        【解决方案4】:

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

        gem install terminal-notifier
        

        然后简单地说:

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

        另见this OS X Daily post

        【讨论】:

        • 这简直比旧的 osascript 的东西好多了。
        • 这似乎在 10.7.5 (Lion) 中不起作用,显然其中没有通知中心。
        • brew install terminal-notifier 如果您喜欢酿造,也可以使用。
        • PSA: 在 Mavericks 和后来这不是必需的,只需使用 osascript 的 display notification,下面 Pradeep 的回答中会提到。
        【解决方案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"'
        

        一个额外的例子,用于激发组合更多命令的灵感;这也将使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
        

        【讨论】:

          【解决方案6】:

          使用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 Hints

          【讨论】:

          • 等等,你可以把application缩写成app吗?
          • AppleScript 引擎会自动替换它。只需在 AppleScript Editor 中粘贴引号之间的行,当您点击 Run 时,它会在执行前自动将 app 替换为 application。
          • 另一个打字保护:你不需要“end if”、“end repeat”等,只要“end”就可以了,AppleScript会插入第二个单词。
          • 如果您不想要“取消”按钮而只想要一个“确定”按钮,请将 {dialog} 替换为 {alert}。
          • 13havik,我知道这是个老话题,但是是的,你可以。 :) 但是您需要登录。例如通过终端 ssh。然后调用 osascript -e 'display dialog "Hello!"'
          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

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

            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 Language Guide

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-07-14
              • 1970-01-01
              • 1970-01-01
              • 2011-02-05
              • 2021-05-29
              • 2020-10-12
              相关资源
              最近更新 更多