Toggle menu bar visibility in MacOS 26 Tahoe

Below is my current code but it doesn’t seem to work at all…
If possible could anyone help me fix it, pretty please!!!

use AppleScript version "2.8"
use scripting additions

-- Open the Menu Bar pane
do shell script "open 'x-apple.systempreferences:com.apple.ControlCenter-Settings.extension?MenuBar'"

tell application "System Events"
	tell application process "System Settings"
		set frontmost to true
		
		-- wait for the window & scroll area to exist
		repeat until (exists window 1)
			delay 0.1
		end repeat
		set w to window 1
		repeat until (exists scroll area 1 of w)
			delay 0.1
		end repeat
		set sa to scroll area 1 of w
		
		-- find the (unnamed) popup by its value, anywhere inside the scroll area
		set p to missing value
		repeat 200 times
			try
				set p to first pop up button of (entire contents of sa) ¬
					whose value is in {"Never", "In Full Screen Only", "On Desktop Only", "Always"}
				exit repeat
			on error
				delay 0.1
			end try
		end repeat
		if p is missing value then error "Could not locate the pop-up button. Check Accessibility/Automation permissions."
		
		-- choose the other setting (Never ↔ In Full Screen Only)
		set curVal to value of p
		if curVal is "Never" then
			set targetItem to "In Full Screen Only"
		else
			set targetItem to "Never"
		end if
		
		try
			perform action "AXScrollToVisible" of p
		end try
		perform action "AXPress" of p
		delay 0.2
		click menu item targetItem of menu 1 of p
	end tell
end tell

@cikip82178
Welcome, cikip!
I’m not very good at UI scripting, so I’ll leave the System Events part to others.

As for everything else:
The menu bar settings are configured as a combination of two values.
So you can get the current settings from NSUserDefaults.

Of course, you can also use:
defaults read -g AppleMenuBarVisibleInFullscreen
defaults read -g _HIHideMenuBar

to read (and even change) the current values.
I think the key point is just being able to get the current settings…
Hope this helps!

Option AppleMenuBarVisibleInFullscreen _HIHideMenuBar bool
Always 0 1 false:true
Never 1 0 true:false
On Desktop Only 1 1 true:true
In Full Screen Only 0 0 false:false
#!/usr/bin/env osascript
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application

####################
#setting record
set recordSettingRev to {|01|:"Always", |10|:"Never", |00|:"In Full Screen Only", |11|:"On Desktop Only"} as record

#MakeDict
set ocidSettingRevDict to refMe's NSMutableDictionary's alloc()'s init()
ocidSettingRevDict's setDictionary:(recordSettingRev)
log ocidSettingRevDict as record

####################
#NSUserDefaults
set appUserDefaults to refMe's NSUserDefaults's standardUserDefaults()
set ocidRootDict to appUserDefaults's persistentDomainForName:(".GlobalPreferences")

####################
#value AppleMenuBarVisibleInFullscreen integer
set boolInFull to (ocidRootDict's objectForKey:("AppleMenuBarVisibleInFullscreen"))'s integerValue()

####################
#value _HIHideMenuBar integer 
set boolHide to (ocidRootDict's objectForKey:("_HIHideMenuBar"))'s integerValue()

#Value
set itemKey to ("" & boolInFull & boolHide & "") as text
log (ocidSettingRevDict's objectForKey:(itemKey)) as text
return (ocidSettingRevDict's objectForKey:(itemKey)) as text
1 Like

Hello, any chance to toggle without restart of the app? My following shell script toggles but is only taken into account after restart of an app. GUI scripting could help, because toggling via System Settings » Menu Bar is immediately active.

# If, like me, you always want the menu bar to be visible, but want to watch videos in full-screen mode without the menu bar, use the following script to switch between them.

# Script toggles "Automatically hide and show the menu bar" between
#  • "In Full Screen Only" for Presentation Mode (no UI, e.g. Full Screen Video) and
#  • "Never" for Full Screen app usage.
# ┌─────────────────────┬─────────────────────────────────┬────────────────┐
# │                     │ AppleMenuBarVisibleInFullscreen │ _HIHideMenuBar │
# ├─────────────────────┼─────────────────────────────────┼────────────────┤
# │ Always              │ false                           │ true           │
# │ On Desktop          │ true                            │ true           │
# │ In Full Screen Only │ false                           │ false          │
# │ Never               │ true                            │ false          │
# └─────────────────────┴─────────────────────────────────┴────────────────┘

#!/bin/bash

AppleMenuBarVisibleInFullscreen=$(defaults read .GlobalPreferences AppleMenuBarVisibleInFullscreen)
echo "AppleMenuBarVisibleInFullscreen = $AppleMenuBarVisibleInFullscreen"

if [ "$(defaults read .GlobalPreferences AppleMenuBarVisibleInFullscreen)" = "1" ]; then
	echo "Set 'Automatically hide and show the menu bar' to 'In Full Screen Only'"
	defaults write .GlobalPreferences AppleMenuBarVisibleInFullscreen -bool false
	defaults write .GlobalPreferences _HIHideMenuBar -bool false
else
	echo "Set 'Automatically hide and show the menu bar' to 'Never'"
	defaults write .GlobalPreferences AppleMenuBarVisibleInFullscreen -bool true
	defaults write .GlobalPreferences _HIHideMenuBar -bool false	
fi

killall SystemUIServer

I finally made it with Apple Script GUI Scripting (only works when called via Spotlight or manually, not via Services Menu or keyboard shortcut). At the end it switches back to the Full Screen Video Space with the Mission Control keyboard shortcut ctrl → (activate it in System Settings » Keyboard). This could not perfectly work (switch back to last app does not work for me because of the way Mac OS treats full screen spaces). I created this script with the help of ChatGPT.

My “Toggle Autohide of Menubar in Fullscreen” shortcut:
https://www.icloud.com/shortcuts/2e91ec843b81435e9a858cb8b61876ea

Apple Script:

-- If, like me, you always want the menu bar to be visible, but want to watch videos in full-screen mode without the menu bar, use the following script to switch between them.
-- Script toggles "Automatically hide and show the menu bar" between
--  • "In Full Screen Only" for Presentation Mode (no UI, e.g. Full Screen Video) and
--  • "Never" for Full Screen app usage.

-- Runs 8 seconds.
-- Only works when manually called or via Spotlight. Services Menu and Keyboard Shortcut does not work.
-- Should work with English, German, Spanish, French, Turkish, Russian, Arabic, Italian, Portuguese, Japanese, Chinese. Add your language version of "menu bar" to the list menuBarLabels.
-- At the end it switches back to the Full Screen Video Space with the Mission Control keyboard shortcut `ctrl →` (activate it in System Settings » Keyboard). This could not perfectly work (switch back to last app does not work  because of the way Mac OS treats full screen spaces).
-- Created with the help of AI.

-- Measure execution time
set startTime to (current date)

-- Remember current App to return there
tell application "System Events"
	set frontApp to name of first application process whose frontmost is true
end tell


set menuBarLabels to {"menu bar", "Menüleiste", "barra de menú", "barre de menus", "menü çubuğu", "панель меню", "شريط القائمة", "barra dei menu", "barra de menu", "メニューバー", "菜单栏"}

on findMenuBarPopup(theContainer, labelsList)
	tell application "System Events"
		repeat with el in (UI elements of theContainer)
			try
				if role of el is "AXPopUpButton" then
					set elName to (name of el) as string
					set elDesc to (description of el) as string
					repeat with lbl in labelsList
						if elName contains lbl or elDesc contains lbl then return el
					end repeat
				end if
			end try
			try
				set found to my findMenuBarPopup(el, labelsList)
				if found is not missing value then return found
			end try
		end repeat
	end tell
	return missing value
end findMenuBarPopup

open location "x-apple.systempreferences:com.apple.ControlCenter-Settings.extension"
--delay 1

tell application "System Events"
	tell process "System Settings"
		try
			-- Wait until main window exists
			repeat until exists window 1
				delay 0.1
			end repeat
			
			set thePopup to my findMenuBarPopup(window 1, menuBarLabels)
			if thePopup is missing value then error "Menu bar pop-up not found."
			click thePopup
			set menuItems to menu items of menu 1 of thePopup
			set currentValue to value of thePopup as string
			if currentValue is not (name of item 4 of menuItems) then
				set targetItem to item 4 of menuItems
			else
				set targetItem to item 3 of menuItems
			end if
			click targetItem
		end try
	end tell
end tell

--return to previous frontmost app
if frontApp is not "System Settings" then
	tell application frontApp to activate
	
	-- switch back to previous space (Ctrl + Right Arrow)
	tell application "System Events"
		key code 124 using control down -- 124 = Right Arrow
	end tell
	
end if


set endTime to (current date)
set execTime to endTime - startTime
return "Menu bar toggle executed in " & execTime & " seconds."