0

我有一个对话框,有两个按钮,“确定”和“取消”,我想让“确定”按钮启动一个网站,取消按钮将停止脚本。现在“确定”和“取消”按钮都在启动网站。我在这里错过了什么?

osascript -e 'tell app "System Events" to display dialog "Things are broke \r \rPress OK to launch Google" buttons {"Cancel", "OK"}'
if [ "button returned:OK" ]; then 
    open "http://www.google.com"
else
    exit 0
fi
4

1 回答 1

2

有几个选项。第一个可能更容易:

osascript -e 'tell app "System Events" to display dialog "Things are broke \r \rPress OK to launch Google" buttons {"Cancel", "OK"}' >/dev/null 2>&1
if [ $? -eq 0 ]; then open "http://www.google.com"; else exit 0; fi

或者捕获并解析这样的输出osascript

res=$(osascript -e 'tell app "System Events" to display dialog "Things are broke \r \rPress OK to launch Google" buttons {"Cancel", "OK"}' 2>/dev/null)

然后你可以像这样检查:

if [[ $res == *OK* ]]; then 
  echo OK
fi
于 2014-06-20T11:17:18.837 回答