Toggle show/hide desktop icons using System Settings

to avoid unwanted Finder interactions:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- https://www.youtube.com/watch?v=MOdIFEU1WV8

tell application "System Settings"
	delay 1
	-- get properties
	reveal pane id "com.apple.Desktop-Settings.extension"
	tell pane id "com.apple.Desktop-Settings.extension"
		reveal anchor "Desktop"
	end tell
end tell

tell application "System Events"
	-- activate
	delay 0.5
	tell application process "System Settings"
		repeat while (get name of window 1) ≠ "Desktop & Dock"
			delay 0.1
		end repeat
		-- Desktop & Dock
		tell window "Desktop & Dock"
			-- discovery only
			-- get entire contents
			tell scroll area 1 of group 1 of list 2 of splitter group 1 of list 1
				tell group 4
					-- set showIcons to value of checkbox "Show Items" as boolean
					-- if showIcons then click checkbox "Show Items"
					click checkbox "Show Items"
					delay 0.5
				end tell
			end tell
		end tell
	end tell
end tell