1

我有一个在 Mac OS X(X.5 到 X.8)机器上使用的 bash 脚本。在它是一个对话框情况,要求通过按“确定”继续脚本或通过按“打盹”让脚本打盹。那部分正在工作。

但是,我正在测试脚本并且无法立即按下任一按钮,并且在一两分钟后(我没有计时),脚本继续执行其余过程,让对话框留在屏幕上。

我的印象是脚本必须等待用户输入?

部分有问题的脚本:

osascript -e '告诉应用程序“Finder”激活'

返回=osascript -e 'tell app "Finder" to display dialog "Text goes here. Please select OK or Snooze" buttons {"OK", "Snooze"} default button 1 with title "Text Here" with icon caution'

    ############  BEGIN LOOP HERE  ##############

while [ "$return" == "按钮返回:贪睡"]

        do
        Runs every 4 hours
        sleep 14400
        osascript -e 'tell app "Finder" to activate'
        return=`osascript -e 'tell app "Finder" to display dialog "Text goes here. Please select OK or Snooze" buttons {"OK", "Snooze"} default button 1 with title "Text Here" with icon 2'`

完毕

###### 在这里结束循环
    if [ "$return" == "button returned:OK" ]
        then
                    run the installer script here
            fi
4

2 回答 2

1

尝试将 return 语句的整个右侧放在反引号中,这将执行 osascript 并将“return”设置为该程序返回的字符串:

return=`osascript -e 'tell app "Finder" to display dialog "Text goes here. Please select OK or Snooze" buttons {"OK", "Snooze"} default button 1 with title "Text Here" with icon caution'`

否则,您只是将返回设置为“=”右侧的字符串。

于 2012-09-24T17:25:42.617 回答
0

要忽略超时错误,您可以试试这个。我们需要一个脚本来提示用户在密码到期前 14 天或更短的时间内更改密码。如果他们忽略提示,则脚本将因退出 0 而终止。您可以根据需要编辑此行为,但重点是,如果除了我的条件测试之外还返回任何"button returned:OK"其他内容,则脚本将跳过我不知道的内容不想让它运行

if [ $daysRemaining -lt $daysBeforeExpire ]
    then
        echo "$userName has $daysRemaining day(s) remaining to change their password"
        echo "Informing User..."
        return=`osascript -e "tell application \"System Events\" to display dialog \"You have $daysRemaining day(s) to change your password... Logout and change password now?\" buttons {\"Later\",\"OK\"}"`
        if [[ "button returned:OK" = $return ]]
            then 
            echo "Returned OK"
            echo "Asking to log out."
            osascript -e "tell application \"System Events\" to log out"
        else
            echo "Returned Later - Skipping user log out"
            exit 0
        fi
    else
        echo "$daysRemaining day(s) remaining to change password"
fi

所以在你的情况下,这应该有效

    if [[ "button returned:OK" = $return ]]
        then
             run the installer script here
        else 
             sleep 100 #sleep the script OR
             exit 0    #end it
        fi
于 2013-08-20T14:27:50.237 回答