Select from pop up menu in System Settings

Does anyone have a tidy method for selecting a given setting from a pop up menu in System Settings?

Can you give a specific example?

Certainly. The AppleScript model for System Settings appears to have been implemented by three different raccoons who didn’t talk to each other. It’s inconsistent, incomplete and those variations vary throughout the various panes.

I would love to be able to query the menu items of pop up menus and address them by name but those don’t yield any success for me so far.

Here I’m just poking the menu and swapping the selection to the index that it should be, checking for a correct match and swapping if it doesn’t match. This isn’t desired, It’s just where I am with testing at this point. Some of these pop up buttons have more than two options so swap and check is even worse than here.

Ideally I’d check to see if it’s already set correct, and if not then I’d change the pop up to the desired setting, QED.

Edit: non-dependent code. This works, but is not nice.

Control_Center_WiFi_Show_In_Menu_Bar(true)
Control_Center_WiFi_Show_In_Menu_Bar(false)

on Control_Center_WiFi_Show_In_Menu_Bar(booleanSwitch)
	
	if booleanSwitch then
		set desiredSetting to "Show In Menu Bar"
	else
		set desiredSetting to "Don’t Show in Menu Bar"
	end if
	tell application "System Settings"
		reveal anchor "WiFi" of pane "Control Center"
			delay 1
end tell
	tell application "System Events"
		tell application process "System Settings"
			tell window 1
				set popUpButtonReference to pop up button "Wi‑Fi" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1
				set currentSetting to value of popUpButtonReference
				if currentSetting is not desiredSetting then
					tell popUpButtonReference
						click
						tell menu 1
							click menu item 2 
						end tell
						if value is not desiredSetting then
							click
							tell menu 1
								click menu item 1
							end tell
						end if
					end tell
				end if
				return value of popUpButtonReference
			end tell
		end tell
	end tell
end Control_Center_WiFi_Show_In_Menu_Bar

Better, here I’m determining which index should be used and setting the pop up correctly each time.

Control_Center_WiFi_Show_In_Menu_Bar(true)
Control_Center_WiFi_Show_In_Menu_Bar(false)

on Control_Center_WiFi_Show_In_Menu_Bar(booleanSwitch)
	--Open pane and anchor
	tell application "System Settings"
		reveal anchor "WiFi" of pane "Control Center"
		--delay 1
	end tell
	--Customize for each anchor.
	set popUpButtonName to "Wi‑Fi"
	set groupNumber to 1
	set MenuItemList to {"Show In Menu Bar", "Don’t Show in Menu Bar"}
	--Derive the correct setting text.
	if booleanSwitch then
		set desiredSetting to contents of item 1 of MenuItemList
	else
		set desiredSetting to contents of item 2 of MenuItemList
	end if
	--Confirm current setting, make changes.
	tell application "System Events"
		tell application process "System Settings"
			tell window 1
				set popUpButtonReference to pop up button popUpButtonName of group groupNumber of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1
				set currentSetting to value of popUpButtonReference
				if currentSetting is not desiredSetting then
					tell popUpButtonReference
						click
						tell menu 1
							if currentSetting is (contents of item 1 of MenuItemList) then
								click menu item 2
							else
								click menu item 1
							end if
						end tell
					end tell
				end if
				return value of popUpButtonReference
			end tell
		end tell
	end tell
end Control_Center_WiFi_Show_In_Menu_Bar

Still a messy way to achieve something that should be just

if currentSetting is not desiredSetting then
	tell PopUpButtonReference to click menu item desiredSetting of menu 1
end