Toggle Light Mode/Dark Mode AND Accent Color from one to another

applescriptmojavesystem-prefs

I am new to scripting, so excuse my lack of knowledge. I would like to create a toggle that switches from Light Mode and an accent (and highlight) color of blue to Dark Mode and an accent/highlight color of red. So far, I've been able to create the toggle for Light/Dark mode only, as well as switch just the highlight color (but not toggle it). However, with accent color being a new feature to macOS in Mojave, I'm not sure how to proceed as I can't find the attribute for accent color.

Best Answer

Maybe this applescript code will help a bit

tell application "System Preferences"
    reveal anchor "Main" of pane id "com.apple.preference.general"
end tell

tell application "System Events"
    repeat until exists of checkbox "Dark" of window "General" of application process "System Preferences"
        delay 0.1
    end repeat
    -- Appearance
    click checkbox "Dark" of window "General" of application process "System Preferences"
    -- Accent Color
    click checkbox "Red" of window "General" of application process "System Preferences"
    -- Dropdown Menu For Highlight Color
    click pop up button 1 of window "General" of application process "System Preferences"
    -- Highlight Color
    click menu item "Red" of menu 1 of pop up button 1 of window "General" of application process "System Preferences"
end tell

tell application "System Preferences" to quit

UPDATE:

Taking it one step further, save this following AppleScript code as an application. When this new app is launched, it allows for choosing your different color modes.

property appearanceMode : {"Light", "Dark"}
property accentColors : {"Blue", "Purple", "Pink", "Red", "Orange", "Yellow", "Green", "Graphite"}
property highlightColors : {"Blue", "Purple", "Pink", "Red", "Orange", "Yellow", "Green", "Graphite", "Other"}

activate
set chosenAppearanceMode to (choose from list appearanceMode ¬
    with title "Please Choose Your Appearance Mode" with prompt ¬
    "Please Choose Your Appearance Mode" OK button name ¬
    "OK" cancel button name "CANCEL") as string

if chosenAppearanceMode is "false" then return

activate
set chosenAccentColor to (choose from list accentColors ¬
    with title "Please Choose Your Accent Color" with prompt ¬
    "Please Choose Your Accent Color" OK button name ¬
    "OK" cancel button name "CANCEL") as string

if chosenAccentColor is "false" then return

activate
set chosenHighlightColor to (choose from list highlightColors ¬
    with title "Please Choose Your Highlight Color" with prompt ¬
    "Please Choose Your Highlight Color" OK button name ¬
    "OK" cancel button name "CANCEL") as string

if chosenHighlightColor is "false" then return

try
    if application "System Preferences" is running then do shell script "killall 'System Preferences'"
end try
repeat until application "System Preferences" is not running
    delay 0.1
end repeat

tell application "System Preferences" to reveal anchor "Main" of pane id "com.apple.preference.general"

tell application "System Events"
    repeat until exists of checkbox chosenAppearanceMode of window "General" of application process "System Preferences"
        delay 0.1
    end repeat
    -- Appearance
    click checkbox chosenAppearanceMode of window "General" of application process "System Preferences"
    -- Accent Color
    click checkbox chosenAccentColor of window "General" of application process "System Preferences"
    -- Dropdown Menu For Highlight Color
    click pop up button 1 of window "General" of application process "System Preferences"
    -- Highlight Color
    click menu item chosenHighlightColor of menu 1 of pop up button 1 of window "General" of application process "System Preferences"
end tell

try
    if application "System Preferences" is running then do shell script "killall 'System Preferences'"
end try

enter image description here


SIDENOTE: My reason for using the the do shell script "killall 'System Preferences'" command rather than tell application "System Preferences" to quit is... Let's say, for what ever reason, System Preferences.app is already activated (with, for example, window Sharing/ Remote Management/ Computer Settings open) but not visible or not front most or whatever. In Script Editor, you try to run tell application "System Preferences" to reveal anchor "SpeakableItems" of pane id "com.apple.preference.universalaccess" That command completes with no obvious errors but when you switch over to System Preferences, your "revealed anchor" is not revealed. Ok, no big deal, easy fix. I'll just insert a tell application "System Preferences" to quit command prior to the reveal anchorcommand. Running the updated code this time, you get a (userCanceledErr:-128) error. All of this hassle is because that secondary drop down window in System Preferences is open. In my opinion, the do shell script "killall 'System Preferences'" command seems to be the best solution.