Skip to content

Instantly share code, notes, and snippets.

@martin-braun
Last active January 25, 2024 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-braun/d9a3fd02bc992a29a1e138b16b3337ae to your computer and use it in GitHub Desktop.
Save martin-braun/d9a3fd02bc992a29a1e138b16b3337ae to your computer and use it in GitHub Desktop.
macOS - Create a "light and dark" wallpaper
ldwallpaper() {
if [ $# -lt 3 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Creates a light and dark wallpaper for macOS."
echo "Usage: ldwallpaper [-h] [--help] light_image dark_image out_heic"
echo ""
return 129
fi
command -v "exiv2" >/dev/null 2>&1 || { echo "exiv2 is not installed." >&2; return 1; }
command -v "heif-enc" >/dev/null 2>&1 || { echo "libheif is not installed." >&2; return 1; }
test -s "$1" || { echo "Light image does not exist." >&2; return 1; }
test -s "$2" || { echo "Dark image does not exist." >&2; return 1; }
test ! -e "$3" || { echo "Output image already exists." >&2; return 1; }
f="$(basename "$1")"; n="${f%.*}"; e="${f##*.}"
tmp="$(mktemp -d)"
cp "$1" "$tmp/$f"
cat << EOF > "$tmp/$n.xmp"
<?xpacket?>
<x:xmpmeta xmlns:x="adobe:ns:meta">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns">
<rdf:Description xmlns:apple_desktop="http://ns.apple.com/namespace/1.0"
apple_desktop:apr=
"YnBsaXN0MDDSAQMCBFFsEAFRZBAACA0TEQ/REMOVE/8BAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAFQ=="/>
</rdf:RDF>
</x:xmpmeta>
EOF
exiv2 -i X in "$tmp/$f"
{ test "$e" = "png" && heif-enc -L "$tmp/$f" "$2" -o "$3"; } || heif-enc "$tmp/$f" "$2" -o "$3"
rm -r "$tmp"
}
@martin-braun
Copy link
Author

martin-braun commented Jan 24, 2024

Allows you to convert a light and a dark wallpaper into one HEIC which can be set as desktop background in macOS, so that the proper version will be used depending on your system settings. If you use light mode, the light wallpaper will be shown. If you use dark mode, the dark wallpaper will be shown.

I built the function based on https://remove.codes/01-dynamic-wallpaper (credit where credit is due)

Interestingly, macOS doesn't indicate the file to be light/dark mode compatible, but it works anyways.

Example usage:

brew install exiv2 libheif
source ldwallpaper.inc
ldwallpaper light_wallpaper.jpg dark_wallpaper.jpg out_wallpaper.heic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment