Using Alfred to toggle dark mode

Here’s a simple way I’m using Alfred to toggle Dark Mode on and off.

  1. Create a new workflow with a keyword trigger e.g. “dark”.
  2. Set up an action to run an Apple Script:
 tell application "System Events"
 
     tell appearance preferences
 
         set dark mode to not dark mode
 
     end tell
 
     end tell
end alfred_script

Easy!

However, what I’d like to do is customise the script to switch the desktop background to the dark Mojave background if dark mode is enabled and then switch back to light, or dynamic when disabled.

Any ideas on how I could do this?

2 Likes

Also happy to discover that Ulysses v.14’s option in View > Appearance > Match System works without any scripting on High Sierra - just use Alfred to switch modes and Ulysses will follow.

here’s an Applescript that will change the desktop to the light or dark version according to the Dark Mode setting. If you don’t want the desktop to change because you’re using the Dynamic, then juste remove that part in the script. You’re script acts as a toggle, so it would also work as is if you keep the dynamic desktop. I’m using it in Keyboard Maestro but I guess you trigger it the way you want. HTH.

tell application "System Events"
set myMode to dark mode of appearance preferences
set desktopCount to count of desktops -- needed if more then 1 monitor

if myMode = false then
	
	set dark mode of appearance preferences to true
	repeat with desktopNumber from 1 to desktopCount
		tell desktop desktopNumber
			set picture to "/Library/Desktop Pictures/Mojave Night.jpg"
		end tell
	end repeat
else
    set dark mode of appearance preferences to false
	repeat with desktopNumber from 1 to desktopCount
		tell desktop desktopNumber
			set picture to "/Library/Desktop Pictures/Mojave Day.jpg"
		end tell
	end repeat
end if	
end tell
1 Like

Thanks for the tip! I’ll give this a go.

Thanks for the advice Jeff. However, I can’t seem to get this to work. I added it as a separate script after my initial script (the one that toggles dark mode). I also changed “Mojave Day” to “Mojave Dynamic” but am obviously doing something wrong:

on alfred_script(q)
tell application "System Events"
set myMode to dark mode of appearance preferences
set desktopCount to count of desktops -- needed if more then 1 monitor

if myMode = false then
	
	set dark mode of appearance preferences to true
	repeat with desktopNumber from 1 to desktopCount
		tell desktop desktopNumber
			set picture to "/Library/Desktop Pictures/Mojave Night.jpg"
		end tell
	end repeat
else
    set dark mode of appearance preferences to false
	repeat with desktopNumber from 1 to desktopCount
		tell desktop desktopNumber
			set picture to "/Library/Desktop Pictures/Mojave Dynamic.jpg"
		end tell
	end repeat
end if	
end tell
end alfred_script

Also, is there any advantage using Keyboard Maestro over Alfred?

@paulminors you can’t change the name of the file that the script calls, the part where it says

set picture to “/Library/Desktop Pictures/Mojave Dynamic.jpg”

because it’s not a jpeg but a different kind of image. Did’t try, but if you put

set picture to “/Library/Desktop Pictures/Mojave.heic”

But, since the Dynamic is not the Dark version, then you either stick to it or you change between Light and Night.

As for Alfred vs Keyboard Maestro, it’s really up to you. I have it both in Launchbar and keyboard maestro. I’ve set a macro that is in my global palette and in the KM menu, and it just points to a script file. OR I can call the script from Launchbar, which ever I feel like at the moment.

Oh, and don’t put two script inside the same file! You can save my script in a file, and save your version in ANOTHER file, and then call which ever you want from Alfred, KM, or LB.

Ah, that got it working. Thanks Jeff. Appreciate your help!

Exactly what I was wanting. Thank you!