【问题标题】:How to make osascript display dialog while script continues other commands如何在脚本继续其他命令时使 osascript 显示对话框
【发布时间】:2017-07-14 18:58:44
【问题描述】:

所以我正在寻找运行让我们说以下脚本

    #!/bin/bash/
    do
    echo "Starting script"
    osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer"'
    do somecommands
    done

我希望当“执行某些命令”在后台运行而不终止该显示时,显示保持在屏幕上。这可能吗?

谢谢

【问题讨论】:

标签: bash macos terminal applescript


【解决方案1】:

您可以通过在后台运行osascript 来做到这一点,如下所示:

#!/bin/bash/

echo "Starting script"
osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer"' &
# do other stuff

但是,您将遇到 2 个新问题 - 如何在完成时关闭对话框以及超时。

所以,如果你想稍后关闭对话框,你会想知道它的进程 ID,所以你应该在启动后台作业后捕获它:

osascript -e .... &
pid=$!
# Do some other stuff 
# kill the dialog
kill $pid

不幸的是,这似乎并没有使对话消失 - 也许其他人可以提供帮助。

其次,如果您正在做一些耗时的事情,对话框将超时,因此您可能需要添加这样的超时时间,例如 100 秒:

osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer" giving up after (100)' &

也许这是更好的方法,运行一个循环,如果超时到期而你仍然很忙,重新显示对话框,如果你完成了不要重新显示对话框 - 那你就不用了最后有如何让它消失的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2015-04-24
    • 2017-10-31
    相关资源
    最近更新 更多