如何让 osascript 显示对话框,同时脚本继续执行其他命令

问题描述 投票:0回答:1

所以我正在考虑运行下面的脚本

    #!/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

我希望当“do somecommands”在后台运行时显示屏保留在屏幕上,而不终止该显示。这可能吗?

谢谢

bash macos terminal applescript
1个回答
2
投票

您可以通过在后台运行

osascript
来完成此操作,如下所示:

#!/bin/bash/

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

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

因此,如果您想稍后关闭该对话框,您将需要知道其进程 ID,因此您应该在启动后台作业后捕获它,如下所示:

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

不幸的是,这似乎并没有让对话消失——也许其他人可以帮忙。

UPD:要关闭之前打开的窗口,我们可以执行以下操作:

osascript -e 'tell application "System Events" to click button "Cancel" of window "DIALOG TITLE" of process "System Events"'

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.