Can a shortcut be created to toggle menubar visibility (that would persist, as the dock's does)?
I can choose in Syst Prefs to have the menubar show always or hide with auto-show (auto-show being show on hover or show on focus-by-keyboard-shortcut).
But when I'm working, I want the menubar always visible. And when I'm recreating, I want it usually hidden.
I like that with the dock I can hit commandoptiond to show or hide the dock, and it stays shown or hidden until I press the same shortcut again.
I wish the menubar worked this way too. Can such a shortcut be created somehow?
You asked, "Can a shortcut be created to toggle menubar visibility (that would persist, as the dock's does)?" and the short answer is, yes.
However, the longer answer is, while it's technically and natively possible to create an Automator Service workflow that gets assigned a keyboard shortcut to hide/unhide the Menu bar in OS X 10.11 and later, including the current macOS, it's not without its issues.
- You'd have to assign the Automator Service a keyboard shortcut that does not interfere with an existing shortcut in every app that might have focus when you triggered the service's keyboard shortcut.
- Every app that has focus when the service's keyboard shortcut is triggered would have to be added to System Preferences > Security & Privacy > Privacy > Accessibility, in order for the service's keyboard shortcut to work.
A possible workaround to the second point above would be if there's a third-party app that can be globally set to trigger the Automator Service work flow (or the AppleScript code as an AppleScript script or app not using an Automator Service). This might include apps like Alfred, FastScripts, Karabiner, Karabiner-Elements, Keyboard Maestro, etc., and having not tested these third party apps under this particular scenario I can only offer that as something to look into.
So, how about an AppleScript app you could place in the Dock, so it's readily available to click on, that will toggle the state of the Menu bar? You'd only have to add that AppleScript app to System Preferences > Security & Privacy > Privacy > Accessibility, in order for it to work.
In lieu of a third-party app or as an Automator Service and just as a plain AppleScript app, you do have a keyboard shortcut built in by way of Spotlight, in that you press ⌘space and the first character or two of the name you gave to the AppleScript app and then press enter. As an example, name it tmb.app for toggle menu bar, the you'd press ⌘spacetmenter to trigger the AppleScript App. After all, I double you already have an app named tm installed.
However, that said, it too is not without possible issues in that it relies on UI Scripting, meaning, it has to open System Preferences to the General settings and click the "Automatically hide and show the menu bar" checkbox and close System Preferences. This means once you triggered the app, you have to let it run and not manually steal focus from System Preferences while the UI Scripting events take place. In other words, you have to stop multitasking for a couple of seconds.
In part, one of the issues using AppleScript and or UI Scripting to hide/unhide the Menu bar is it's just not as graceful as hiding the Dock with its built-in keyboard shortcut, but it's at least doable.
That all said, here's the AppleScript code, that can be used in either an AppleScript script/app, Automator Service, or possibly a third party app, that will toggle the state of the Menu bar in OS X 10.11 and later via UI Scripting.
tell application "System Preferences"
reveal pane id "com.apple.preference.general"
delay 1
tell application "System Events"
click checkbox "Automatically hide and show the menu bar" of window "General" of process "System Preferences"
end tell
quit
end tell
Note there is no error checking to ensure this code in run under OS X 10.11 and later so don't try using it in versions of OS X prior to 10.11. Also note that the value of the delay
command may need to be adjusted as necessary under the working conditions of your system.
Making some minor adjustments to the original code from user3439894’s answer. Running this revised code will not bring system preferences app to the front.
tell application "System Preferences"
reveal pane id "com.apple.preference.general"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "General"
click checkbox "Automatically hide and show the menu bar"
end tell
delay 1
quit application "System Preferences"
In some comments to my answer, it has been pointed out where I have the delay command in my script, makes the script fail. On my system this script works.
However this script on my system will not work if I put the delay command before the system event.
I'm posting this as a second answer instead of editing my original answer as this new version of the example AppleScript code accommodates issues raised with the use of the delay
command and where it was placed in the code presented by wch1zpink and myself between our versions of the code.
The new code wraps a much smaller value for the delay
command in a repeat
loop, thus making the over all code run faster while accommodating the timing differential based on the system specifications of the computer it's been run on. Additionally, because of the use of UI Scripting, the two repeat
loops ensures that the UI Elements are available and or have been changed respectively before proceeding within the code.
This code was tested under macOS High Sierra and worked as an AppleScript application, Automator service, as described in my original answer, and as an AppleScript .scpt file triggered by FastScripts with an assigned keyboard shortcut, as with the Automator service.
Example AppleScript code:
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
end if
repeat while running of application "System Preferences" is true
delay 0.01
end repeat
tell application "System Preferences" to ¬
reveal pane id "com.apple.preference.general"
tell application "System Events"
repeat until exists checkbox ¬
"Automatically hide and show the menu bar" of ¬
window "General" of process "System Preferences"
delay 0.01
end repeat
set cbVal to value of checkbox ¬
"Automatically hide and show the menu bar" of ¬
window "General" of process "System Preferences"
click checkbox "Automatically hide and show the menu bar" of ¬
window "General" of process "System Preferences"
repeat until cbVal is not (value of checkbox ¬
"Automatically hide and show the menu bar" of ¬
window "General" of process "System Preferences")
delay 0.01
end repeat
end tell
tell application "System Preferences" to quit
Scroll as necessary to view full code.
Note: The example AppleScript code is just that and, other then included error handling, does not contain any additional error handling as may be appropriate. The onus is upon the used to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
Also note that I am not affiliated with Red Sweater Software, the developer of FastScripts, just a satisfied user of their product.