This applescript code by user regulus6633 works great for toggling the "Automatically hide and show the menu bar on desktop" system preference in macOS 12.1:
tell application "System Events"
tell dock preferences to set autohide to not autohide
end tell
Does anyone know how to do the same for "Automatically hide and show the menu bar in full screen"?
The same is not applicable to the Menu Bar as with the mentioned Dock preference and as such you can use UI Scripting to achieve the goal.
The example AppleScript code, shown below, was tested in Script Editor under macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
Example AppleScript code:
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
-- # Open to Dock & Menu Bar
tell application "System Preferences" to ¬
reveal pane "com.apple.preference.dock"
-- Toggle the Automatically hide and show the menu bar in full screen checkbox.
tell application "System Events"
tell application process "System Preferences"
tell window 1
repeat until exists (first checkbox whose title is "Automatically hide and show the menu bar in full screen")
delay 0.2
end repeat
click (first checkbox whose title is "Automatically hide and show the menu bar in full screen")
end tell
end tell
end tell
delay 0.2
tell application "System Preferences" to quit