7

After upgrading to macOS Mojave, I was wondering if there is a setting which would automatically turn on Dark Mode at a certain time of day?

I only found the setting to turn it on and off manually.

  • what setting, where – Ruskes Sep 27 '18 at 2:30
  • 2
    I have an AppleScript that makes the change (it's actually the same setting that controlled the "Dark Menu Bar and Dock" in earlier versions), so it's probably possible to whip something up with launchctl... – SilverWolf Sep 27 '18 at 2:38
11
+50

In case someone is interested in an answer that doesn't require you to install anything but does require you to touch code just a little... Here you go....

How to toggle dark mode with Applescript:

tell application "System Events"
    tell appearance preferences
        set dark mode to not dark mode
    end tell
end tell

How to turn ON dark mode with Applescript:

tell application "System Events"
    tell appearance preferences
        set dark mode to true
    end tell
end tell

How to turn OFF dark mode with Applescript:

tell application "System Events"
    tell appearance preferences
        set dark mode to false
    end tell
end tell

There are pretty robust applications out there that can run scripts or actions at a specific time, but these applications are mostly fairly extensive and expensive. I did want to mention one because it allows you to do more than just toggle the dark mode: Keyboard Maestro - It's sort of like Automator, but better. Don't let the outdated look fool you.

I would use launchd, because it doesn't require you to install anything. It does require you to touch a little bit of code, but nothing major.

How to trigger Applescript at a specific time with Launchd property list file (plist):

  1. You need to make a new .plist file in ~/Library/LaunchAgents (~ represents your user folder)

    • If you want to make it run for all users, put it in /Library/LaunchAgents (Macintosh HD).
    • This .plist is just a text file that you can edit in any text editor and save as text with the extensions .plist.
    • You likely have existing files in those folders that you can duplicate to create your own. If you open an existing .plist file with TextEdit.app, you won't accidentally save it as a .rtf file or something.
  2. Copy & paste this in the .plist file:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.time.trigger.darkmode</string>
    <key>Program</key>
    <string>/usr/bin/osascript</string>
    <key>ProgramArguments</key>
    <array>
      <string>osascript</string>
      <string>/Users/joonaspaakko/Desktop/toggle-darkmode.scpt</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
      <dict>
        <key>Hour</key>
        <integer>21</integer>
        <key>Minute</key>
        <integer>20</integer>
      </dict>
    </array>
  </dict>
</plist>
  • The line below <string>osascript</string> points to the Applescript .scpt file that should be triggered. You will change this in step 3 after creating a script file.
  • The array below <key>StartCalendarInterval</key> that says Hour and Minute is where you set the time. Current set time is H 21 M 20 = 9:20pm

These steps are maybe a little backwards since I already have the script location set in the .plist but let's create the .scpt file next.

  1. Run Script Editor application /Applications/Utilities/Script Editor.app.
    • Paste in whichever Applescript you wish to use (At the top of the answer in case you forgot)
    • You can test run the Applescripts in this application by pressing the play button.
    • Save as Script file. If you're just using the script yourself, save it somewhere under your home directory. If you're setting this up for all accounts you'll need to store it in a location everyone can access, such as /Library/Scripts.
    • Return to your .plist file from step 2, and change the line below <string>osascript</string> to point at your new Applescript (.scpt file).
  2. When both files are created, an important step is to log out (or restart) because these launch agents start running in the background on launch.
    • If you want to stop it from running, you can move the .plist from the folder or just delete it.
  • 1
    Please make clear that the line below <string>osascript</string> has to be changed. (as it currently contains your user folder. Also make clear that the files should be in a different location if the are to be run global. – Gert-Jan Roeleveld Dec 27 '18 at 9:27
  • @Gert-JanRoeleveld, to be honest, I'm not quite sure how I would go about doing that. These are both things I mention in the answer and I can see how they could be missed or even misinterpreted, but I'm unable to twist those words in a sensible way to make it clearer. The best I could come up with was to change the bullet points to numbers to sort of reinforce the idea they are important to read, rather than just small hints. I don't know though. Feel free to edit the answer if you have ideas on how to make it better. – Joonas Dec 27 '18 at 17:27
  • I used this initially, and gave it points for thoroughness, but it's worth also looking at apple.stackexchange.com/a/346808/25450 to see how to put the script inside the plist, merging steps 2 and 3. – nruth Dec 30 '18 at 14:12
6

f.lux can switch between Mojave's light/dark modes at sunrise/sunset ("Color Effects"):

OS X Dark theme at sunset: Uses the normal theme during the day and switches to dark theme (dock and menu bar) each night at sunset. Disabling f.lux will also disable dark theme.

  • Ooh I forgot about this app! – Geoff Nixon Dec 27 '18 at 18:52
3

There is no native OS way at the moment, but you can use some software like

» NightOwl

NightOwl

or write your own script in the Apple Script Editor to do it by click

tell application "System Events"

  tell appearance preferences

    set dark mode to not dark mode

  end tell

end tell
2

Another option is a launchd plist I created for myself and shared on Github.

My profile is set to 9pm and 7:15am, you can change this easily. I have included a single terminal command to install it. It does not install any software, uses only standard macOS tools. In fact I have pretty much done the same as what Joonas suggested you do in his answer, and shared it so you don't have to type it out.

https://github.com/superman-lopez/switchdarkmode

1

As @Gert-JanRoeleveld mentioned, @Joonas largely has the right approach. However there are a few missing details to be clarified/improved upon:

  • As mentioned, if you want this launchd script to automatically kick in whenever you log in, it needs to be placed in ~/Library/LaunchAgents or /Library/LaunchAgents, and the filename should match the "Label" key, i.e., com.time.trigger.darkmode.plist. It needs to be a plain text file. It won't start until you next log in unless you launchctl load ~/LaunchAgents/com.time.trigger.darkmode.plist for example.
  • The implementation (as currently written) currently calls an external AppleScript that lives at /Users/joonaspaakko/Desktop/toggle-darkmode.scpt. Other users will not have access to this file, and thus the script will not work for anyone else (this is generally considered bad practice).

Instead, include the AppleScript within the plist itself:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
  <dict>
    <key>Label</key>
      <string>com.time.trigger.darkmode</string>
    <key>Program</key>
      <string>/usr/bin/osascript</string>
    <key>ProgramArguments</key>
    <array>
      <string>osascript</string>
      <string>-e</string>
      <string>tell app "System Events" to tell appearance preferences to set dark mode to not dark mode</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
      <dict>
        <key>Hour</key>
          <integer>21</integer>
            <key>Minute</key>
          <integer>20</integer>
      </dict>
   </array>
  </dict>
</plist>

  • (There is no need to use Script Editor or create an AppleScript file at all.)
  • Also, unless I'm mistaken: credit for the 'toggle' functionality (reading the value of dark mode with a negated boolean within the command) really needs to go to Brett Terpstra.
  • Also, FWIW, there's a lovely implementation at github.com/katernet/darkmode where light and dark mode is toggled at sunset and sunrise based on location and date. – Geoff Nixon Dec 27 '18 at 18:43
  • I like this change having the script within the plist. I went with local.toggle-dark-mode for the script/label but I guess that's down to how you want to manage the files. I like to know at a glance which ones are mine. – nruth Dec 30 '18 at 13:53
1

Another 3rd party option is LightsOff.

It also enables controlling which apps switch themes, and can also switch them for some other 3rd party apps with light/dark themes, though not all.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .