Tips: How to toggle Dark Mode in Mojave with a keyboard shortcut or the Touch Bar

Posted:
in macOS edited August 2020
Apple's macOS Mojave is coming this Fall, and it's bringing Dark Mode, a feature that users have requested for years. AppleInsider shares one way to make it easier to turn Dark Mode on and off, without needing to enter System Preferences every time.





We're currently on the first beta of Mojave, and Dark Mode looks great, but it's a little bothersome to switch in-between both modes. To switch to and from Dark Mode at the moment, users have to enter the System Preferences, select General, then select their desired mode.

By taking advantage of Automator, we can create a custom script that will allow us to toggle between light and dark mode with a simple keyboard shortcut, or a button on the MacBook Pro's Touch Bar.

Credit goes to Thomas Verschoren for coming up with this idea.


Creating the Automator Script

To get started, open the Automator app, and click New Document. Select Contextual Workflow and click Choose.




In the WorkFlow input selection dropdown menu, choose No Input. If you have a TouchBar, you can also change the image that'll be used for the button.

Within the Actions search bar, type "Apple" and double-click on Run AppleScript. Go down to the script box, select all, and delete.

Copy and paste this code into the script box:
tell application "System Events"
tell appearance preferences
set dark mode to not dark mode
end tell
end tell



You can test out the script by clicking the Play icon. If Dark Mode switches on and off, the script works.

Go to File, click Save, and name it Dark Mode, or another appropriate title.

Creating the Keyboard Shortcut

Open System Preferences, click on Keyboards, then go to the Shortcuts tab.

Click on Services, and look for Dark Mode at the bottom of the list. Make sure the box next to Dark Mode is checked, and click on Add Shortcut.

Enter the keyboard combination you want to use. Note that it will not override global shortcuts, like Command-X, so you will need to experiment with different shortcuts until you find one that works.

For this example, we mapped it to Command-B, but it wouldn't work until we set the Send File to Bluetooth Device to use a different key combination, as that shortcut was already in use.

When this keyboard shortcut is used, macOS automatically toggles between light and dark mode.

Take note that it won't work in other apps that are using the same shortcut as you are. For example, iTunes uses Command-B to hide the column browser, so the above won't work while iTunes is the currently-used app..

Touch Bar Shortcut

Head to System Preferences once more, click on Extensions, then select Touch Bar. Make sure the Dark Mode workflow is enabled.




Go back to Keyboard settings and click Customize Controlstrip, which will be available if you have a Touch Bar model.

You should be able to drag your Dark Mode workflow onto the Touch Bar. Once added, a simple tap of the button will toggle Dark Mode on.
fastasleep
«1

Comments

  • Reply 1 of 33
    CheeseFreezeCheeseFreeze Posts: 1,247member
    AppleScript syntax looks ridiculous. In an attempt to make it user friendly, it's really not.
    Why not just switch to Swift syntax? Looks much more standard and looks way more friendly.
  • Reply 2 of 33
    SoliSoli Posts: 10,035member
    Thanks! Now all I need to do is figure out how to tie this to Night Shift and/or ambient light results.
    watto_cobragvitoro
  • Reply 3 of 33
    Eric_WVGGEric_WVGG Posts: 966member
    AppleScript syntax looks ridiculous. In an attempt to make it user friendly, it's really not.
    Why not just switch to Swift syntax? Looks much more standard and looks way more friendly.
    They actually added Javascript a while back, but don't go near it, it's a disaster.

    I hadn't heard about this ability for Automator to be added to the Touch Bar. Huge step in the right direction. I'm a (rare) huge fan of the Touch Bar, but it's all due to Better Touch Tool customizations that are outside of Apple's designs.

    Would like to see Apple Insider do a feature on the "BTT scene", hopefully I can get around to putting together a Youtube video on my own setup before that happens… http://vas3k.com/blog/touchbar/

    /edit: clarification, that is a link to someone else's setup, I have yet to document my own
    edited June 2018 caladanianwatto_cobra
  • Reply 4 of 33
    SoliSoli Posts: 10,035member
    AppleScript syntax looks ridiculous. In an attempt to make it user friendly, it's really not.
    Why not just switch to Swift syntax? Looks much more standard and looks way more friendly.
     What is this Swift syntax for Automator that would be more "friendly" than AppleScript?
    watto_cobra
  • Reply 5 of 33
    caladaniancaladanian Posts: 380member
    Cool
    watto_cobra
  • Reply 6 of 33
    Eric_WVGGEric_WVGG Posts: 966member
    Soli said:
    AppleScript syntax looks ridiculous. In an attempt to make it user friendly, it's really not. 
    Why not just switch to Swift syntax? Looks much more standard and looks way more friendly.
     What is this Swift syntax for Automator that would be more "friendly" than AppleScript?
    It's very difficult to learn or memorize "the right way" to say something in AppleScript, because it looks like English (where there's no one right way to say anything), but it's actually syntactically strict (like any programming language). So you might take a statement like…

    set message to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    … but try to write it like…
    make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    … or…
    set new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    … or…
    set message to new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    See the problem? These all "look" fine, but only the first one actually works.

    Swift is C-syntax, which makes familiar to anyone who does C or Javascript which means basically every programmer alive. What we want is…
    let message = Message(subject:theSubject, content:theContent, visible:true)
    watto_cobra
  • Reply 7 of 33
    SoliSoli Posts: 10,035member
    Eric_WVGG said:
    Soli said:
    AppleScript syntax looks ridiculous. In an attempt to make it user friendly, it's really not. 
    Why not just switch to Swift syntax? Looks much more standard and looks way more friendly.
     What is this Swift syntax for Automator that would be more "friendly" than AppleScript?
    It's very difficult to learn or memorize "the right way" to say something in AppleScript, because it looks like English (where there's no one right way to say anything), but it's actually syntactically strict (like any programming language). So you might take a statement like…

    set message to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    … but try to write it like…
    make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    … or…
    set new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    … or…
    set message to new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    See the problem? These all "look" fine, but only the first one actually works.

    Swift is C-syntax, which makes familiar to anyone who does C or Javascript which means basically every programmer alive. What we want is…
    let message = Message(subject:theSubject, content:theContent, visible:true)
    I don't see a solution outside of AI's using AppleScropt. If Swift is more "friendly" then what is the workflow to create it using Swift over AppleScript?
    watto_cobra
  • Reply 8 of 33
    Eric_WVGGEric_WVGG Posts: 966member
    If Automator could do Swift, this cited sample…

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

    would read something like…

    let systemPreferences = Application("System Preferences")
    systemPreferences.set(\.darkMode, !systemPreferences.darkMode)

    some pedant can ding me for systemPreferences being an optional… point is, there would be a correct, unambiguous way to say this.
    watto_cobra
  • Reply 9 of 33
    Eric_WVGG said:
    Soli said:
    AppleScript syntax looks ridiculous. In an attempt to make it user friendly, it's really not. 
    Why not just switch to Swift syntax? Looks much more standard and looks way more friendly.
     What is this Swift syntax for Automator that would be more "friendly" than AppleScript?
    It's very difficult to learn or memorize "the right way" to say something in AppleScript, because it looks like English (where there's no one right way to say anything), but it's actually syntactically strict (like any programming language). So you might take a statement like…

    set message to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    … but try to write it like…
    make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    … or…
    set new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    … or…
    set message to new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

    See the problem? These all "look" fine, but only the first one actually works.

    Swift is C-syntax, which makes familiar to anyone who does C or Javascript which means basically every programmer alive. What we want is…
    let message = Message(subject:theSubject, content:theContent, visible:true)
    C is complied langiuage not scripting language. It is not about memorizing. AppleScript and JavaScript are scripting languages. - nothing to do with compiled. That means differences are justifiable and they make sense.

    Do not squeeze languages into the same bucket based on syntax. It is not smart idea as they are processed by OS differently and require different ooptimizations if any. Some companies like Microsoftr learned hard way this fact over last two, three decades. Some things are possible and some work giving side effects that then you have to deal with. It is immature to turn one in another. That is also why TypeScript was born and it is "compiled" into lower level instructions - JavaScript. Time will only show if this was good idea.

    It only looks like it is easy to align syntax, but when you consider purpose things become different.
    SoliEric_WVGG
  • Reply 10 of 33
    danuffdanuff Posts: 33member
    Your videos are awesome. Thanks for the tips and keep them coming.
    watto_cobra
  • Reply 11 of 33
    danuffdanuff Posts: 33member
    AppleScript has been around for a while but is very under utilized. Glad to see AppleInsider bring it into the spot light again.
    watto_cobra
  • Reply 12 of 33
    Eric_WVGGEric_WVGG Posts: 966member
    Do not squeeze languages into the same bucket based on syntax. It is not smart idea as they are processed by OS differently and require different ooptimizations if any.
    That is a good point, but I still argue that basing a programming language on a spoken language is an inherently flawed idea.
  • Reply 13 of 33
    netroxnetrox Posts: 1,415member
    AppleScript syntax looks ridiculous. In an attempt to make it user friendly, it's really not.
    Why not just switch to Swift syntax? Looks much more standard and looks way more friendly.

    It is user friendly once you know the rules of syntax. Why is that different from English rules? 

    https://en.wikipedia.org/wiki/AppleScript

  • Reply 14 of 33
    ivanhivanh Posts: 597member
    Record this: “hey Siri, turn on the dark mode” with your own voice and play the sound clip whenever you want to enter dark mode.  Doesn’t it work?
  • Reply 15 of 33
    Hi for some reason this the Dark mode keyboard shortcut  worked on my Macbook and also the  the Mojave beta was running so well I added it to the Imac 
    followed the exact same process to get dark mode using a keyboard shortcut and it got an error message and clunked out ..
    It works in automator ? 

    tell application "System Events"

    tell appearance preferences

    set dark mode to not dark mode

    end tell

    end tell


  • Reply 16 of 33
    Works in automator, but when I run it using the command I get the error, "The action “Run AppleScript” encountered an error: “Not authorized to send Apple events to System Events.”
  • Reply 17 of 33
    Works in automator, but when I run it using the command I get the error, "The action “Run AppleScript” encountered an error: “Not authorized to send Apple events to System Events.”
    Me too.  Any ideas?
  • Reply 18 of 33
    Melanie_isMelanie_is Posts: 1unconfirmed, member
    Thanks - but still need help. Got the script to run, save, even got a keyboard shortcut - it works fine. Couldn't find a shortcut that I didn't hate, though, and want a touchbar button. I can not get that to work! So frustrating. Any ideas?
  • Reply 19 of 33
    simeousimeou Posts: 2unconfirmed, member
    I did everything but i still can find the icon i the touch bar
  • Reply 20 of 33
    simeousimeou Posts: 2unconfirmed, member
    The button is in workflow button can we put the dark mode directly on the Touch Bar?

Sign In or Register to comment.