Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created May 4, 2021 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjluoma/9d6b6a1e179a201eb3503c94edf690c2 to your computer and use it in GitHub Desktop.
Save tjluoma/9d6b6a1e179a201eb3503c94edf690c2 to your computer and use it in GitHub Desktop.
Show, change, toggle the menu bar visibility on macOS Big Sur
#!/usr/bin/env zsh -f
# Purpose: Toggle, or check, the menu bar visibility. Tested on macOS 11.
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2021-05-04
NAME="$0:t:r"
SCRIPT_NAME="$0"
if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
fi
OS_VER=$(sw_vers -productVersion | cut -d '.' -f 1)
if [[ "$OS_VER" -lt "11" ]]
then
echo "$0 requires macOS 11 (Big Sur) or later." >>/dev/stderr
exit 1
fi
function autohide-off {
# Don't Indent - BEGIN
/usr/bin/osascript <<EOT
tell application "System Events"
tell dock preferences to set autohide menu bar to false
end tell
EOT
# Don't Indent - END
}
function autohide-on {
# Don't Indent - BEGIN
/usr/bin/osascript <<EOT
tell application "System Events"
tell dock preferences to set autohide menu bar to true
end tell
EOT
# Don't Indent - END
}
function get-status {
CURRENT=$(defaults read NSGlobalDomain _HIHideMenuBar 2>/dev/null)
######################
# 0 = not hidden
# 1 = hidden
#
# Also, if there is no value set, it has probably never been hidden,
# so we assume it is not hidden
if [[ "$CURRENT" = "1" ]]
then
HIDDEN='yes'
VERBOSE='Menu Bar is currently hidden.'
QUIET='1'
else
HIDDEN='no'
VERBOSE='Menu Bar is currently visible.'
QUIET='0'
fi
}
function show-status {
get-status
echo "$NAME: $VERBOSE"
}
function show_usage {
cat <<EOINPUT
'$SCRIPT_NAME' can do two things:
1. Show the current hidden/unhidden status of the menu bar on the Mac.
2. Change that status.
The following options control what '$0' does:
(none) show current status
-h --hide hide the menu bar
-u --unhide make the menu bar visible
-t --toggle if hidden, unhide. If visible, hide
-q --quiet no output, only exit code: 0 for Not Hidden, 1 for Hidden
-s --status show current status
-H --help show this text
EOINPUT
}
if [[ "$#" == "0" ]]
then
show-status
echo "$NAME: use '--help' for usage info."
exit 0
fi
for ARGS in "$@"
do
case "$ARGS" in
-q|--quiet)
# no output, only exit 0 or 1
get-status
exit $QUIET
;;
-t|--toggle)
get-status
if [[ "$HIDDEN" == "yes" ]]
then
autohide-off
show-status
else
autohide-on
show-status
fi
shift
;;
-h|--hide)
autohide-on
show-status
shift
;;
-s|--status)
show-status
shift
;;
-H|--help)
show_usage
exit 0
;;
-u|--unhide)
autohide-off
show-status
shift
;;
-*|--*)
echo " $NAME [Error]: Don't know what to do with arg: $1"
show_usage
shift
;;
esac
done # for args
exit 0
#EOF
@tjluoma
Copy link
Author

tjluoma commented May 4, 2021

Thanks to this thread on the Keyboard Maestro forums for bringing this to my attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment