Created
May 13, 2025 08:04
-
-
Save davext/c00fb25b8f7015a1ab8ff06cedbff800 to your computer and use it in GitHub Desktop.
This bash script sets the mac wallpaper from a label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ──────────────────────────────────────────────────────────────────────────── | |
# make-wallpaper.sh • macOS • ask for a label → black 4-K JPEG wallpaper | |
# ──────────────────────────────────────────────────────────────────────────── | |
set -euo pipefail | |
read -p "Enter the label to show on the wallpaper: " LABEL | |
[[ -z $LABEL ]] && { echo "No text entered – aborting."; exit 1; } | |
export LABEL # pass into Python | |
export JPEG="/tmp/wallpaper_${LABEL// /_}.jpg" | |
python3 - <<'PY' | |
import os, sys, subprocess, importlib | |
def have_pillow() -> bool: | |
try: | |
import PIL # noqa: F401 | |
return True | |
except ModuleNotFoundError: | |
return False | |
if not have_pillow(): | |
print("• Pillow not found – installing to your user site …") | |
# 1) make sure pip exists | |
try: | |
subprocess.check_call([sys.executable, "-m", "pip", "--version"], | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL) | |
except subprocess.CalledProcessError: | |
print(" (bootstrapping pip…)") # macOS CLT Python | |
subprocess.check_call([sys.executable, "-m", "ensurepip", "--upgrade"]) | |
# 2) install pillow | |
try: | |
subprocess.check_call([sys.executable, "-m", "pip", | |
"install", "--quiet", "--user", "pillow"]) | |
except subprocess.CalledProcessError: | |
print("❌ PIP failed. Run this then re-launch the script:\n" | |
" python3 -m pip install --user pillow") | |
sys.exit(1) | |
import importlib | |
importlib.invalidate_caches() | |
if not have_pillow(): | |
print("❌ Pillow still missing after install. Run:\n" | |
" python3 -m pip install --user pillow") | |
sys.exit(1) | |
# ── build the image ───────────────────────────────────────────────────────── | |
from PIL import Image, ImageDraw, ImageFont # type: ignore | |
label = os.environ["LABEL"] | |
out = os.environ["JPEG"] | |
W, H = 3840, 2160 | |
img = Image.new("RGB", (W, H), "black") | |
draw = ImageDraw.Draw(img) | |
fonts = ["/System/Library/Fonts/SFNS.ttf", | |
"/System/Library/Fonts/SFNSText.ttf", | |
"/System/Library/Fonts/Helvetica.ttc"] | |
fontpath = next((p for p in fonts if os.path.exists(p)), None) | |
size = int(H * 0.55) | |
font = ImageFont.truetype(fontpath, size) if fontpath else ImageFont.load_default() | |
while draw.textlength(label, font=font) > W * 0.9: | |
size -= 10 | |
font = ImageFont.truetype(fontpath, size) if fontpath else ImageFont.load_default() | |
draw.text(((W - draw.textlength(label, font=font)) / 2, | |
(H - font.size) / 2), | |
label, font=font, fill="white") | |
img.save(out, "JPEG", quality=95) | |
print("• Created", out) | |
PY | |
# ── apply it to every Desktop/Space ───────────────────────────────────────── | |
osascript - "$JPEG" <<'OSA' | |
on run argv | |
set p to POSIX file (item 1 of argv) | |
tell application "System Events" | |
repeat with d in desktops | |
set picture of d to p | |
end repeat | |
end tell | |
end run | |
OSA | |
echo "✔ Wallpaper applied!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment