AppleScript to Toggle Desktop Widgets

I’m a fan of the new Sonoma Desktop Widgets. However, I also make a living making screencasts and having all those widgets on screen can sometimes be a pain. There is a setting you can toggle under the Desktop & Dock settings:

The problem with this, of course, is that it’s a pain in the neck to get there. Who wants to do all that mousing and clicking? I wanted to automate this button so I can run a script that sets me up for screencasting and, among other things, turns off Desktop Widgets. So here’s a script that does just that. This was built with help from the AppleScript Sensei himself, Sal Soghoian. Below is a link to the zipped script file, as well as the script in plain text. I run it as part of a Keyboard Maestro macro that I’ve tied to a keyboard trigger. Enjoy.


tell application "System Settings"
	activate
	reveal anchor "Widgets" of pane id "com.apple.Desktop-Settings.extension"

	repeat
		set currentPane to get current pane
		if currentPane is pane id "com.apple.Desktop-Settings.extension" then
			exit repeat
		else
			delay 1
		end if
	end repeat
end tell

delay 1

tell application "System Events"
	tell process "System Settings"
		set targetControl to checkbox "Show Widgets" of group 6 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
		click targetControl
	end tell
end tell

tell application "System Settings" to quit