Skip to content

Instantly share code, notes, and snippets.

@zaxbux
Created May 12, 2024 07:39
Show Gist options
  • Save zaxbux/83ca97a2bd0d27acce5e091d31aa7c1f to your computer and use it in GitHub Desktop.
Save zaxbux/83ca97a2bd0d27acce5e091d31aa7c1f to your computer and use it in GitHub Desktop.
Get macOS Screensaver and Settings
#!/bin/bash
loggedInUser=$(stat -f "%Su" /dev/console)
osVersion=$(/usr/bin/sw_vers -productVersion)
osVerMajor=$(echo "$osVersion" | /usr/bin/awk -F"." '{print $1}')
#osVerMinor=$(echo "$osVersion" | /usr/bin/awk -F"." '{print $2}')
#osVerPatch=$(echo "$osVersion" | /usr/bin/awk -F"." '{print $3}')
indent() { sed 's/^/ /'; }
urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
function getScreensaverSonoma() {
user=$1
section="AllSpacesAndDisplays" # or SystemDefault
wallpaperStore="/Users/${user}/Library/Application Support/com.apple.wallpaper/Store/Index.plist"
# Type = individual, idle, linked
type=$(plutil -extract "${section}.Type" raw -o - "${wallpaperStore}")
key=""
if [ "$type" = "linked" ]; then
key="Linked"
elif [ "$type" = "individual" ] || [ "$type" = "idle" ]; then
key="Idle"
fi
if [ "$key" = "" ]; then
plutil -extract "SystemDefault" xml1 -o - "$wallpaperStore" | plutil -p -
return
fi
provider=$(plutil -extract "${section}.${key}.Content.Choices.0.Provider" raw -o - "${wallpaperStore}")
screensaverConfigRaw=$(plutil -extract "${section}.${key}.Content.Choices.0.Configuration" raw -o - "${wallpaperStore}")
#plutil -extract "${section}.${key}" xml1 -o - "$wallpaperStore" | plutil -p -
if [ "$provider" = "com.apple.wallpaper.choice.aerials" ]; then
assetID=$(echo "$screensaverConfigRaw" | base64 -d -i - | plutil -extract assetID raw -o - -)
printf "Type: \"%s\", Provider: \"%s\", AssetID: \"%s\", Name: \"%s\"" "$type" "$provider" "$assetID" "$(getAerialName "$assetID")"
echo ""
elif [ "$provider" = "com.apple.wallpaper.choice.screen-saver" ]; then
file=$(echo "$screensaverConfigRaw" | base64 -d -i - | plutil -extract module.relative raw -o - -)
settings=$(getScreensaverSettings "$user" "$(urldecode "$(echo "$file" | sed -E 's/^\s*.*:\/\///g')")")
printf "Type: \"%s\", Provider: \"%s\", File: \"%s\", Settings: %s" "$type" "$provider" "$file" "$settings"
echo ""
else
raw=$(echo "$screensaverConfigRaw" | base64 -d -i - | plutil -p - )
printf "Type: \"%s\", Provider: \"%s\", Raw: %s" "$type" "$provider" "$raw"
fi
}
function getBundleID() {
path=$1
bundleID=$(mdls -name kMDItemCFBundleIdentifier -r "$path")
if [ "$bundleID" = "com.apple.ScreenSaver.iLife-Slideshow-Extension" ]; then
bundleID="com.apple.ScreenSaver.iLifeSlideshows"
fi
}
function getUserDefaults() {
user=$1
domain=$2
sudo -u "$user" defaults -currentHost export "$domain" -
}
function getScreensaverSettings() {
user=$1
path=$2
bundleID=$(getBundleID "$path")
if [ "$bundleID" = "com.apple.ScreenSaver.iLifeSlideshows" ]; then
echo "com.apple.ScreenSaver.iLifeSlideshows $(getUserDefaults "$user" "com.apple.ScreenSaver.iLifeSlideshows" | plutil -p -)"
echo "com.apple.ScreenSaverPhotoChooser $(getUserDefaults "$user" "com.apple.ScreenSaverPhotoChooser" | plutil -p -)"
return
fi
getUserDefaults "$user" "$bundleID" | plutil -p -
}
function getAerialName() {
ID=$1
IDLEASSETS=$(cat "/Library/Application Support/com.apple.idleassetsd/Customer/entries.json")
read -r -d '' JXA <<EOF
function run() {
var entries = JSON.parse(\`$IDLEASSETS\`);
return entries.assets.find((asset) => asset.id === \`$ID\`).localizedNameKey;
}
EOF
NAMEKEY=$( osascript -l 'JavaScript' <<< "${JXA}" )
NAME=$( plutil -extract "$NAMEKEY" raw -o - "/Library/Application Support/com.apple.idleassetsd/Customer/TVIdleScreenStrings.bundle/en.lproj/Localizable.nocache.strings" )
echo "${NAME}"
}
function getScreensaver() {
user=$1
screensaver=$(sudo -u "$user" /usr/bin/defaults -currentHost export "com.apple.screensaver" -)
name=$(plutil -extract moduleDict.moduleName raw -o - - <<< "$screensaver")
path=$(plutil -extract moduleDict.path raw -o - - <<< "$screensaver")
settings=$(getScreensaverSettings "$user" "$path")
printf "Name: \"%s\", Path: \"%s\", Settings: %s" "$name" "$file" "$settings"
}
if [[ $osVerMajor -ge 14 ]]; then
getScreensaverSonoma "$loggedInUser"
else
getScreensaver "$loggedInUser"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment