Seeking Better Approach: Toggling Menu Bar Visibility

I have a script that toggles the visibility of the menu bar between Always and Never on macOS Sonoma (14.5). However, I find the current implementation involving a pop-up button to be less than ideal as it visually interacts with the interface. Here’s the existing script:

tell application "System Settings"
	reveal pane id "com.apple.ControlCenter-Settings.extension"
	delay 0.5
end tell

tell application "System Events"
	tell window 1 of application process "System Settings"
		tell pop up button "Automatically hide and show the menu bar" of group 9 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1
			click
			delay 0.1
			if value = "Never" then
				keystroke "A"
			else
				keystroke "N"
			end if
			delay 0.1
			keystroke return
		end tell
	end tell
end tell

tell application "System Settings"
	delay 0.5
	quit
end tell

Is there a more efficient or less intrusive method to achieve the same functionality without visibly interacting with the pop-up button? Any insights or alternative approaches would be greatly appreciated.

Welcome preseren,

I don’t believe that there is any method to alter this setting that doesn’t interact with the UI. This code does allow for more precise control of the settings.

Edit: Added handler to actually swap the setting from “Always” to “Never” rather than just the generic handler.

     --Running under AppleScript 2.8, MacOS 14.5
    --6/28/24 • https://forum.latenightsw.com/u/paulskinner
    use AppleScript version "2.4"
    use scripting additions
    
    controlcenter_Swap_Alway_and_Never()
    
    on controlcenter_set_Menu_Bar_Options(desiredSetting)
        if theSetting is in {"Never", "In Full Screen Only", "On Desktop Only", "Always"} then
            tell application "System Settings"
                reveal pane "control center"
            end tell
            do shell script "open x-apple.systempreferences:com.apple.controlcenter-settings.extension"
            tell application "System Events"
                repeat until group 7 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1 of application process "System Settings" exists
                    delay 0
                end repeat
                tell pop up button "Automatically hide and show the menu bar" of group 7 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1 of application process "System Settings"
                    perform action "AXPress" --click
                    delay 1 --faster systems can reduce this value or eliminate the delay altogether
                    pick menu item desiredSetting of menu 1
                end tell
            end tell
        end if
    end controlcenter_set_Menu_Bar_Options
    
    
    
    on controlcenter_Swap_Alway_and_Never()
        tell application "System Settings"
            reveal pane "control center"
        end tell
        do shell script "open x-apple.systempreferences:com.apple.controlcenter-settings.extension"
        tell application "System Events"
            repeat until group 7 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1 of application process "System Settings" exists
                delay 0
            end repeat
            tell pop up button "Automatically hide and show the menu bar" of group 7 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1 of application process "System Settings"
                set curVal to value
                if curVal is not in {"Never", "Always"} then
                    set desiredSetting to "Never"
                else
                    if curVal is "Never" then
                        set desiredSetting to "Always"
                    else
                        set desiredSetting to "Never"
                    end if
                end if
                perform action "AXPress" --click
                delay 1 --faster systems can reduce this value or eliminate the delay altogether
                pick menu item desiredSetting of menu 1
            end tell
        end tell
    end controlcenter_Swap_Alway_and_Never
1 Like