Here's how to schedule Dark Mode to turn on at night without any 3rd party apps

Discussion in 'macOS Mojave (10.14)' started by baryon, Sep 28, 2018.

  1. baryon, Sep 28, 2018
    Last edited: Sep 28, 2018

    baryon macrumors 68040

    baryon

    Joined:
    Oct 3, 2009
    #1
    I think it makes a lot of sense to use Dark Mode at night, and to use Light Mode during the day. Dark is easier on the eyes at night, and light is easier to see during the day. Strangely, while Apple has a way of scheduling Night Shift, they did not think of this for Dark Mode.

    I like to avoid having to install 3rd party apps for little things like this, so I figured out a way to do it with AppleScript and Calendar. Here's how.

    1. Open AppleScript Editor and paste this in:

    Code:
    tell application "System Events"
        tell appearance preferences
            set dark mode to not dark mode
        end tell
    end tell
    2. Go to File > Export, and change the File Format to Application. Name it "Toggle Dark Mode" and save it somewhere. This app, when run, will toggle between Dark and Light modes. You can even use this app to manually switch modes if you want.

    3. Open Calendar.

    4. Now we will prepare Calendar for running an app as a timed alert. This is a feature of Calendar, but for some reason it doesn't work for iCloud calendars, so we have to enable the good old "On My Mac" calendar for this. This won't interfere with your other iCloud calendars.

    To do that, here's a little trick I found: Show the left Calendars pane, then select a calendar name that isn't and iCloud calendar - such as the "Birthdays" calendar.

    Then right click the blank white space underneath it, and choose "New Calendar". This should create a new On My Mac calendar at the top of the list.

    Screenshot 2018-09-28 at 16.03.16.png

    5. You should have a new "On My Mac" category appear, with a new calendar within it. Name it whatever you like, I called it "Scripting" as I will be using it to run scripts such as this one.

    Screenshot 2018-09-28 at 16.03.34.png

    6. Create a new event for today, call it "Dark Mode On".

    7. Create a Custom alert. For alert type, choose "Open File".

    Point it to the "Toggle Dark Mode.app" file you created in Step 2.

    Make it run "At the time of event".

    Set the time of the event to when it starts to get dark.

    Screenshot 2018-09-28 at 16.09.28.png Screenshot 2018-09-28 at 16.11.10.png


    8. Set this event to repeat daily.

    9. Duplicate the event and name it "Dark Mode Off". Change its start time to when the sun rises.

    10. In the left Calendar pane, uncheck the "Scripting" calendar so it doesn't clutter up your calendar. It will still work the same.

    That's it. Test it out, it should work! It even runs when your Mac is sleeping (or at least it runs as soon as it wakes up).

    One minor annoyance with it is that the transition from dark to light is abrupt, not smooth like when you change the appearance in System Preferences. I don't know if there's a way to make the AppleScript do that. Any ideas? But 3rd party apps don't do the transition either, so at least it's not any worse.

    Another annoyance is that each time the event runs, you get a notification banner. I don't know how to have the Calendar event just run the app without there also being a notification alert. If you have any ideas, let me know!

    Let me know if it works or if it doesn't, and if you have any improvements, such as disabling the notification banner and making it switch smoothly between dark and light modes!

    Update: It seems that it doesn't always work while my mac is sleeping. It worked twice, but on the third try, it didn't.
     
  2. gnubee, Sep 28, 2018
    Last edited: Sep 28, 2018

    gnubee macrumors newbie

    gnubee

    Joined:
    Sep 21, 2012
    #2
    Instead of using Calendar, maybe use crontab and a shell script that determines if it is time to toggle between dark or light mode. It could check every 5 or 10 min throughout the day.

    A null ~/.darkmode file could be touched when in dark mode and removed when in light mode. Then the script would know if it is already been toggled on or off.

    Use osascript command to run the /path/to/Toggle\ Dark\ Mode.scpt file from within the shell script.

    To get the current time into a variable do something like TIME=`date "+%H:%M"`; will return the current hour and minute, example 15:45.
     
  3. gnubee, Sep 28, 2018
    Last edited: Sep 29, 2018

    gnubee macrumors newbie

    gnubee

    Joined:
    Sep 21, 2012
    #3
  4. gnubee, Sep 28, 2018
    Last edited: Sep 29, 2018

    gnubee macrumors newbie

    gnubee

    Joined:
    Sep 21, 2012
    #4
    When using launchctl, it only asks to gain access to control system events.app when the start / stop times are changed in the bash shell script.

    Currently it is checking every 10 min to see if Dark or Light mode is toggled properly during their assigned time frame.

    If it is manually overridden through the general preference pane, then it wont try to change again until the next light/dark cycle starts.

    I'll try to upload the script and .plist tonight if it doesn't blow up on me before then. :)
     
  5. gnubee macrumors newbie

    gnubee

    Joined:
    Sep 21, 2012
    #5
    I ran into this bash shell script today. It does the same thing I was shooting for and then some. I haven't tried it yet but thought it looked interesting. :)

    https://github.com/katernet/darkmode
     

Share This Page

4 September 28, 2018