Apple - Have Mojave's Dark Mode switch PyCharm theme to Darcula

I have come up with a (hacky) way to change themes in JetBrains editors using bash and AppleScript. An example for PyCharm would be something like:

#!/bin/bash

# Switch macOS dark mode on and off
osascript -e '
    tell application "System Events"
        tell appearance preferences
            set dark mode to not dark mode
        end tell
    end tell
'

# Change PyCharm theme

# Assumes default key mappings
# Switch to dark theme if macOS dark mode is on
# 2>/dev/null to suppress error if macOS dark mode is off
if defaults read -g AppleInterfaceStyle 2>/dev/null | grep -q "Dark"; then
    osascript -e '
        if application "PyCharm" is running then
            -- switch focus to PyCharm
            tell application "PyCharm" to activate
            tell application "System Events"
                -- open Quick Switch Theme menu
                keystroke "`" using {control down}
                -- Select Look and Feel
                keystroke "5"
                delay 0.4
                -- Select Darcula
                keystroke "2"
            end tell
        end if
        '
# Switch to light theme if macOS dark mode is off
else
    osascript -e '
        if application "PyCharm" is running then
          tell application "PyCharm" to activate
            tell application "System Events"
                keystroke "`" using {control down}
                keystroke "5"
                delay 0.4
                keystroke "1"
            end tell
        end if
        '
fi

I actually use it as a part of a larger script but the code above should work fine for PyCharm only (or any other JetBrains IDE, just replace application name). If you experience issues, I would suggest increasing/introducing delays between keystrokes like the delay 0.4 in the code above.


Update: Chrome now fully supports dark mode in Chrome 73 and JetBrains IDEs support dark mode in 2020.3.


Since PyCharm and other JetBrains IDE's are made in Java, they will not be able to use the native macOS Dark mode. Any system settings you apply also most likely won't affect Java applications, unless the developer specifically adds support for it, which probably won't happen due to the complexities of using system features with Java.(I've made some apps in Java, and it's currently impossible to use the macOS Dark Mode)

Many native apps will support Dark Mode without the developer having to do anything, but some that use custom UI views and controls, like Chrome, will need to add support for it manually. Dark mode support for Chrome is in progress(see here). SourceTree also uses custom controls and therefore doesn't support Dark Mode yet.