It was recently brought to my attention that my Keyboard Maestro macro for Sidecar doesn’t work under Big Sur. I have Big Sur installed, but my main OS is still Catalina, so I hadn’t yet thought to try the macro under Big Sur.

After a couple of hours of debugging with Script Debugger and Accessibility Inspector – not to mention a good deal of trial and error – I discovered that Big Sur significantly changed the way the menu bar is exposed to AppleScript. (I’ll try to cover that in a separate post; it’s a bit too much to go into in this post.) I finally figured out how to access the right menu bar items with AppleScript and updated the macro’s script to work under Big Sur.

You can download the updated macro here or just create a macro with an Execute AppleScript action. Copy and paste this code into the action:

set deviceName to "The name of your iPad" -- Change this to the name of your iPad

set displayMenu to "" -- Do not change this

tell application "System Events"
	tell its application process "ControlCenter"
		-- Get all menu bar items.
		set menuBarItems to menu bar items of menu bar 1
		
		-- Determine if the Display menu is in the menu bar.
		repeat with mbi in menuBarItems
			if name of mbi contains "Display" then
				set displayMenu to mbi
			end if
		end repeat
		
		-- If the Display menu is in the menu bar, get the Sidecar device.
		-- In Big Sur, it's a toggle button (checkbox) instead of a menu item.
		if displayMenu is not equal to "" then
			click displayMenu
			set deviceToggle to checkbox 1 of scroll area 1 of group 1 of window "Control Center" whose title contains deviceName
			
			-- Click the device.
			click deviceToggle
			
			-- The menu name changes when Sidecar is toggled, so we need to get the Display menu again, then click to close it.
			set displayMenu to (first menu bar item whose name contains "Display") of menu bar 1
			click displayMenu
		else
			-- If the Display menu isn't in the menu bar, display an error message.
			set errorMessage to "Display menu not found in menu bar. Open System Preferences > Dock & Menu Bar. Set Display to \"Show in Menu Bar > Always.\""
			display dialog errorMessage with icon caution
		end if
	end tell
end tell

You need to change two things before you can use this code. First, change “The name of your iPad” (in the first line of the script) to the real name of your iPad (leave the quotation marks around the name). Second, open System Preferences > Dock & Menu Bar, and set the Display menu to always show in the menu bar.

Dock & Menu Bar Preferences
Dock & Menu Bar Preferences