Extension Attribute to show users' Appearance setting: Light, Dark, or Auto

john_sherrod
Contributor II

This is definitely random, but if you're ever curious about what percentage of your users have their Mac's appearance set to Light, Dark, or Auto, I wrote an extension attribute script that will display that info!

#!/bin/sh

# Displays the Appearance status for the currently logged-in user's account: Light, Dark, or Auto.
# Script will return the info as an extension attribute.
# Author: jwsherrod@mac.com
# Version 2.0 : 08-07-2020

loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

if [ -e "/Users/$loggedInUser/Library/Preferences/.GlobalPreferences.plist" ]; then
    AppleInterfaceStyleVal=$(/usr/bin/defaults read /Users/$loggedInUser/Library/Preferences/.GlobalPreferences.plist AppleInterfaceStyle)
    AppleInterfaceStyleSwitchesAutomaticallyVal=$(/usr/bin/defaults read /Users/$loggedInUser/Library/Preferences/.GlobalPreferences.plist AppleInterfaceStyleSwitchesAutomatically)
fi

if [ $AppleInterfaceStyleVal ]; then
    echo "<result>$AppleInterfaceStyleVal</result>"

elif [ $AppleInterfaceStyleSwitchesAutomaticallyVal -eq 1 ]; then
    echo "<result>Auto</result>"

else echo "<result>Light</result>"

fi

exit 0
2 REPLIES 2

tlarkin
Honored Contributor

Neat trick, played around with the plist in Python. I suppose this is only for tracking what a user prefers since apps should handle this natively, but I came up with this

>>> from SystemConfiguration import SCDynamicStoreCopyConsoleUser
>>> from CoreFoundation import CFPreferencesCopyAppValue
>>> from Foundation import NSHomeDirectoryForUser
>>> 
>>> user, uid, gid = SCDynamicStoreCopyConsoleUser(None, None, None)
>>> home = NSHomeDirectoryForUser(user)
>>> userfile = home + '/Library/Preferences/.GlobalPreferences.plist'
>>> keys = ['AppleInterfaceStyle', 'AppleInterfaceStyleSwitchesAutomatically']
# just loop and see what we get
>>> for k in keys:
...     print(CFPreferencesCopyAppValue(k, userfile))
... 
Dark
None
>>> 
# test for None values
>>> for k in keys:
...     result = CFPreferencesCopyAppValue(k, userfile)
...     if result is not None:
...         print(result)
... 
Dark
>>>

chadlawson
Contributor
Contributor

This is pretty cool! Thank you for sharing.