Mazen Alsenih
Mac OS - Toggle dark mode ON/OFF by a single keyboard shortcut

Mac OS - Toggle dark mode ON/OFF by a single keyboard shortcut

2024-07-23

327

As a programmer, you know how crucial it is to have the right environment while working. Sometimes, dark mode helps reduce eye strain during late-night coding sessions, while light mode is perfect for daytime work. If you find yourself frequently switching between these modes on your Mac and in VSCode, this guide is for you. Here's how you can set up a quick and efficient way to toggle between dark and light modes on macOS Sonoma.

Why Toggle Modes?

Switching between dark and light modes can significantly impact your comfort and productivity. Dark mode is excellent for low-light environments, reducing glare and making it easier on the eyes. Light mode, on the other hand, is perfect for well-lit environments, keeping everything bright and clear. As a developer, having the ability to switch quickly can make your workday much smoother.

Setting Up a Quick Toggle with Automator and AppleScript

Let's dive into the steps to create a quick toggle for dark and light modes using Automator and AppleScript.

Step 1: Open Automator

First things first, open Automator on your Mac. You can do this by pressing Command + Space to bring up Spotlight, then typing "Automator" and hitting Enter.

Step 2: Create a New Quick Action

  • Choose "Quick Action" when prompted to select a type for your document.
  • Set "Workflow receives current" to "no input" in "any application."
[@portabletext/react] Unknown block type "image", specify a component for it in the `components.types` prop

Step 3: Add the AppleScript Action

  • In Automator, use the search bar to find "Run AppleScript" and drag it to the workflow area.
  • Replace the default script with this:

Apple Script:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

This script is simply toggles the current appearance setting.

[@portabletext/react] Unknown block type "image", specify a component for it in the `components.types` prop

Step 4: Save the Quick Action

  • Save your Quick Action by going to File > Save and name it something like "Toggle Dark Mode."

Step 5: Assign a Keyboard Shortcut

Now, let's make it super easy to trigger this action with a keyboard shortcut:

  1. Open "System Preferences" and go to "Keyboard" > "Shortcuts."
  2. Select "Services" from the left-hand menu.
  3. Find "Toggle Dark Mode" in the list and assign a keyboard shortcut, like Command + Shift + D.
[@portabletext/react] Unknown block type "image", specify a component for it in the `components.types` prop

Now, by pressing Command + Shift + D it should toggle between dark and light mode 😃.

Or using Spotlight for Quick Access

If you're more of a Spotlight user, you can trigger the quick action there too:

  • Press Command + Space to open Spotlight.
  • Type "Toggle Dark Mode" (or the name you saved it as) and hit Enter.

That's all! 😉, hope you like it.

Post author

Mazen Alsenih

Mazen Alsenih

MacOS

DarkMode

Shortcut

Apple