Toggle Autohide macOS Menu bar
This AppleScript will toggle (check / uncheck) the box within System Preferences to:
'Automatically hide and show the menu bar on desktop'
tell application "System Preferences"
reveal pane id "com.apple.preference.dock"
end tell
delay 0.5
tell application "System Events" to tell process "System Preferences" to tell window "Dock & Menu Bar"
click checkbox "Automatically hide and show the menu bar on desktop"
end tell
delay 0.5
quit application "System Preferences"
Why?
I had a need to present ‘material’ in a scenario where I did not want the presentation tool to be ‘full screen’. For this to look acceptable, naturally the menu bar needed to be tucked away! Within macOS Monterey this option can be found:
System Preferences > Dock & Menu Bar > Automatically hide and show the menu bar on desktop
However there is no keyboard shortcut to activate this. Therefore it was necessary for me to build my own mechanism to enable / disable this function.
One pivotal variable within this script is the ‘pane id’, discovering the ID for a specific pane within System Preferences required the invocation of another AppleScript.
-- Open System Preferences.app and click into desired pane/setting. Then, run this script to find out name (Pane ID).
tell application "System Preferences"
set CurrentPane to the id of the current pane
set the clipboard to CurrentPane
display dialog "Current Pane ID: " & CurrentPane & return & return & "Pane ID has been copied to the clipboard."
end tell
This script comes from apetronix.com/find-pane-id-for-system-preferences-app
All credit to apetronix
TLDR: How? - Toggling
Ok, that’s great! You have created a pretty simple AppleScript that toggles autohide the menu bar, but, now what? How is this script any more efficient than manually checking the box within the System Preferences?
Once we have a working AppleScript we can bind the activation of this script to a keyboard shortcut using a utility such as Keyboard Maestro, or… build it into an Alfred Workflow.
2023 update - A better way
In response to a comment made via Disqus I started looking into this again!
This same result can be achieved with very little effort using Spotlight replacement 'Raycast' + 'Toggle Menu Bar' extension. Which can very easily be assigned a keyboard binding within Raycast.
Since discovering Raycast it has quickly replaced Alfred and many other utility apps such as Magnet, Moom, Keyboard Maestro.
I strongly recommend giving Raycast a try!