admin管理员组文章数量:1384238
I am trying to determine which button was pushed in an OSA generated dialog using bash conditionals. However, no matter which button is pushed, the restart command always runs. Any thoughts?
#!/bin/sh
selection=$(osascript -e 'display dialog "A restart is required for this software to install properly..." buttons {"Test", "Restart Later", "Restart Now"} default button "Restart Now"')
echo $selection
if [[ $selection="button returned:Restart Now" ]]; then
echo "Would Restart..."
#sudo shutdown -r now
elif [[ $selection="button returned:Restart Later" ]]; then
$(osascript -e 'display dialog "Software was not installed." buttons {"Ok"} default button "Ok" with icon caution')
else
echo "Nothing selected!"
fi
exit 0;
I am trying to determine which button was pushed in an OSA generated dialog using bash conditionals. However, no matter which button is pushed, the restart command always runs. Any thoughts?
#!/bin/sh
selection=$(osascript -e 'display dialog "A restart is required for this software to install properly..." buttons {"Test", "Restart Later", "Restart Now"} default button "Restart Now"')
echo $selection
if [[ $selection="button returned:Restart Now" ]]; then
echo "Would Restart..."
#sudo shutdown -r now
elif [[ $selection="button returned:Restart Later" ]]; then
$(osascript -e 'display dialog "Software was not installed." buttons {"Ok"} default button "Ok" with icon caution')
else
echo "Nothing selected!"
fi
exit 0;
Share
Improve this question
asked Mar 18 at 20:51
ezoggezogg
1
2
|
1 Answer
Reset to default 1If you are using bash constructs like [[
, why do you use /bin/sh
in the shebang line? Depending on OS, this could be bash (3.2 on MacOS, 5 on Ubuntu), or dash on Debian, or something else. The shell may even check what it's called as, and switch into some compatibility mode. But you clearly want bash, so just say that.
Then shellcheck will point out the next issue:
if [[ $selection="button returned:Restart Now" ]]; then
^-- SC3010 (warning): In POSIX sh, [[ ]] is undefined.
^-- SC2077 (error): You need spaces around the comparison operator.
Fixing these issues gets me this minimal example:
#!/usr/bin/env bash
selection=$(osascript -e 'display dialog "A restart is required for this software to install properly..." buttons {"Test", "Restart Later", "Restart Now"} default button "Restart Now"')
if [[ $selection = "button returned:Restart Now" ]]; then
echo "Would Restart..."
fi
When I run this and click the "Restart Now" button, I get the expected "Would Restart..." message; when I click any other button I don't get a message, as expected.
I'd recommend you also fix the other shellcheck problems, for good style.
本文标签: applescriptIdentifying OSA button push with bashStack Overflow
版权声明:本文标题:applescript - Identifying OSA button push with bash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744491878a2608782.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
echo $selection
displaying? Does that ever change? – shellter Commented Mar 18 at 23:54