首页 > 解决方案 > bash 直到使用 applescript 对话框。如果一分钟内没有输入响应,循环会不断弹出相同的对话框

问题描述

我有一个脚本,它提示用户使用苹果脚本在我的 bash 脚本中输入他们的名字。它似乎工作得很好,但是如果他们在大约 1 分钟内没有回答提示,它会再次提示,因为我有一个循环。每次提示时,另一个窗口会出现在另一个窗口之上。所以我要么需要终止对话框,但我无法弄清楚,或者我的循环不是每分钟都在尝试,我也不知道该怎么做。下面是我的脚本的样子。

PromptDisplayName ()
{
DISPLAYNAME=$(/usr/bin/osascript << EOF
tell application "System Events" activate set DISPLAYNAME to display dialog "Enter Full Name: " default answer "John Doe" with icon note buttons {"OK"} default button 1 with title "Enter Full Name" return text returned of DISPLAYNAME as string
end tell
EOF
)

Loop until Displayname is in the correct format
until [[ "$DISPLAYNAME" =~ ([a-zA-Z]){3,}[[:blank:]]([a-zA-Z]+){3,} ]]
do
PromptDisplayName
done

}

标签: bashapplescript

解决方案


有人在另一个论坛上为我回答了这个问题,但这是我为解决它所做的。

bash 循环在 2 分钟计时器上,所以我只是将它添加到我的 applescript 行的末尾,在 119 之后放弃。见下面的行。

tell application "System Events" activate set DISPLAYNAME to display dialog "Enter Full Name: " default answer "John Doe" giving up after 119


推荐阅读