Inside a bash script, I'm closing the front window of whatever application is foreground. I'd like to know if the command failed (when the foreground application doesn't have a front window. I tried this, but even though the warning beep is heard indicating the cmd-W failed, the script always prints "Command Succeeded". What's the easy way to detect that there's no front window?
osascript -e 'tell application "System Events" to keystroke "w"
using {command down}'
if [ $? -eq 0 ] ; then
printf "Command succeeded\n"
else
printf "command failed\n"
fi
osascript
command will allwaysexit 0
because all it's doing is pressing ⌘W. You're going have to trap a different way. Maybe first check to see if the frontmost app actually has a visible window before trying to useosascript
and System Events to press keys.