Skip to content

Instantly share code, notes, and snippets.

@munckymagik
Last active January 29, 2020 09:17
Show Gist options
  • Save munckymagik/7b532e0f960aa0a9f4b54db8e71bf4e0 to your computer and use it in GitHub Desktop.
Save munckymagik/7b532e0f960aa0a9f4b54db8e71bf4e0 to your computer and use it in GitHub Desktop.
An Apple JXA script that toggles dark mode for the macOS desktop and Terminal
#!/usr/bin/osascript -l JavaScript
// Update these with your chosen themes for Apple Terminal
const TERMINAL_DARK_THEME_NAME = "YourDarkTheme"
const TERMINAL_LIGHT_THEME_NAME = "YourLightTheme"
const app = Application.currentApplication()
app.includeStandardAdditions = true
const SysEvents = Application("System Events")
const Terminal = Application("Terminal")
const darkTheme = Terminal.settingsSets.byName(TERMINAL_DARK_THEME_NAME)
const lightTheme = Terminal.settingsSets.byName(TERMINAL_LIGHT_THEME_NAME)
// What is the current setting?
const isDark = SysEvents.appearancePreferences().darkMode()
// Toggle it
SysEvents.appearancePreferences().darkMode = !isDark
// Select the associated mode for the terminal
let newSettings = darkTheme;
if (isDark) {
newSettings = lightTheme
}
// Make sure new tabs and windows respect the new setting too
Terminal.defaultSettings = newSettings
// Update any open windows and tabs
for (let window of Terminal.windows()) {
for (let tab of window.tabs()) {
tab.currentSettings = newSettings
}
}
console.log("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment