Skip to content

Instantly share code, notes, and snippets.

@davedelong
Last active September 10, 2022 04:40
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davedelong/850409c512ab2d2a21aa6fa690096d56 to your computer and use it in GitHub Desktop.
Save davedelong/850409c512ab2d2a21aa6fa690096d56 to your computer and use it in GitHub Desktop.
Toggle Dark Mode
on setDarkMode(shouldBeDark)
set paneID to "com.apple.preference.general"
tell application "System Events"
if dark mode of appearance preferences is shouldBeDark then return
end tell
set paneWasOpen to false
(*
If SysPrefs is already running and showing the "General" pane,
then toggling dark mode causes the UI of the pane to become
out-of-sync with actual system appearance. To account for this,
we'll quit SysPrefs if it's open to General prefs, and then
re-open it after we've made the change
*)
if application "System Preferences" is running then
tell application "System Preferences"
if show all is false and id of current pane is paneID then
set paneWasOpen to true
quit
end if
end tell
end if
tell application "System Events"
set dark mode of appearance preferences to shouldBeDark
end tell
if paneWasOpen then
tell application "System Preferences"
launch
delay 3
activate
reveal (the first pane whose id is paneID)
end tell
end if
end setDarkMode
set result to display dialog "Set System Appearance" buttons ["Light Mode", "Dark Mode"]
if button returned of result is "Light Mode" then
setDarkMode(false)
else
setDarkMode(true)
end if
@tucomel
Copy link

tucomel commented Jul 12, 2020

There is a solution to toggle dark/light mode smothly like when choose from com.apple.preference.general panel?
This abrupt change hurt my eyes

@tucomel
Copy link

tucomel commented Jul 13, 2020

There is a solution to toggle dark/light mode smothly like when choose from com.apple.preference.general panel?
This abrupt change hurt my eyes

I adapted your solution in case you want to check
Now running as smooth as it should be
https://gist.github.com/tucomel/2fd454ffabbdb11300a03e20e99b367b

@andrewmackenzie
Copy link

andrewmackenzie commented May 15, 2021

This is GREAT. I modified it a bit to toggle rather than show a dialog, which works great as a KeyMaestro action.

set paneID to "com.apple.preference.general"
set paneWasOpen to false

--If SysPrefs is already running and showing the "General" pane,
--then toggling dark mode causes the UI of the pane to become
--out-of-sync with actual system appearance. To account for this,
--we'll quit SysPrefs if it's open to General prefs, and then
--re-open it after we've made the change

if application "System Preferences" is running then
tell application "System Preferences"
if show all is false and id of current pane is paneID then
set paneWasOpen to true
quit
end if
end tell
end if

tell application "System Events"
if dark mode of appearance preferences is true then
set dark mode of appearance preferences to false
else
set dark mode of appearance preferences to true
end if

if paneWasOpen then
	tell application "System Preferences"
		launch
		delay 3
		activate
		reveal (the first pane whose id is paneID)
	end tell
end if

end tell

@tucomel
Copy link

tucomel commented May 15, 2021

bigsur already do this by itself, this script is only needed before bigsur version

@Write
Copy link

Write commented Apr 6, 2022

bigsur already do this by itself, this script is only needed before bigsur version

yeah ? well Monterey didn't fixed it for me, the auto-toggle is always pure shitshow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment