使用osascript按钮不能正常工作

时间:2014-06-20 11:08:37

标签: bash button scripting osascript

我有一个对话框,有两个按钮," OK"和"取消",我想要" OK"按钮启动一个网站,取消按钮将停止脚本。现在,"确定和"取消"按钮正在启动网站。我在这里缺少什么?

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

1 个答案:

答案 0 :(得分: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