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.
2 Answers
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
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 anchor
command. 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.
-
-
Is the a good reason you're using
if application "System Preferences" is running then do shell script "killall 'System Preferences'"
overif application "System Preferences" is running then tell application "System Preferences" to quit
? Seems a bit of an overkill to shell out and kill an app that will typically reliably respond to aquit
command. Mar 6, 2019 at 17:03 -
There is a very good reason I use shell script "killall". There are times when System Preferences.app opens a secondary drop-down window (for example... Accessibility/ Dictation/ Dictation Commands...) <-- choosing that will reveal a secondary window. If one of these windows is actually open while trying to use the "tell application system preferences to quit" command, it results with an error and does not close. So just to avoid that, now I always use the "killall" command Mar 6, 2019 at 17:27
-
Re: "There are times when System Preferences.app opens a secondary drop-down window (for example... Accessibility/ Dictation/ Dictation Commands...)"... well you could click the Done button and then issue a
quit
command. :) Mar 6, 2019 at 17:59 -
@user3439894 I think you're missing the point. Read the side note I added to the bottom of my original post. I am all ears if you have a better solution. Mar 7, 2019 at 5:32
I've just tried to do the same thing and I believe this is not possible yet.
In the Script Editor app you can see the "library" for documentation on the system events. It does mention the dark mode
and highlight color
, but not the accent color
.
appearance preferences object n : A collection of appearance preferences properties
- appearance (blue/graphite) : the overall look of buttons, menus and windows - font smoothing (boolean) : Is font smoothing on?
- font smoothing style (automatic/light/medium/standard/strong) : the method used for smoothing fonts
- highlight color (blue/gold/graphite/green/orange/purple/red/silver or color) : color used for hightlighting selected text and lists
- recent applications limit (integer) : the number of recent applications to track - recent documents limit (integer) : the number of recent documents to track
- recent servers limit (integer) : the number of recent servers to track
- scroll bar action (jump to here/jump to next page) : the action performed by clicking the scroll bar
- smooth scrolling (boolean) : Is smooth scrolling used?
- dark mode (boolean) : use dark menu bar and dock
-
several years later, and on Mojave, accent color is still not listed an option in the library– Jon zJan 6 at 15:24