Skip to content

Instantly share code, notes, and snippets.

@jecolasurdo
Last active April 21, 2026 19:16
Show Gist options
  • Select an option

  • Save jecolasurdo/203688ccaa1a9f69e1ee8202441aa996 to your computer and use it in GitHub Desktop.

Select an option

Save jecolasurdo/203688ccaa1a9f69e1ee8202441aa996 to your computer and use it in GitHub Desktop.
label-desktop.sh — Generate a labeled wallpaper and set it on a macOS desktop/Space
#!/usr/bin/env bash
# label-desktop.sh
# Generates a labeled wallpaper and sets it on a macOS desktop (Space).
#
# Usage:
# label-desktop.sh <project-name> [desktop-index]
#
# Arguments:
# project-name Text label to display on the wallpaper
# desktop-index Which desktop to target (1-based, default: current)
#
# Dependencies:
# - ImageMagick (brew install imagemagick)
#
# Examples:
# label-desktop.sh "my-api" # set current desktop
# label-desktop.sh "my-api" 3 # set desktop 3
set -euo pipefail
# ── helpers ──────────────────────────────────────────────────────────────────
die() { echo "error: $*" >&2; exit 1; }
require_imagemagick() {
if ! command -v convert &>/dev/null; then
echo "ImageMagick is required but not installed."
read -r -p "Install via Homebrew now? [y/N] " ans
[[ "$ans" =~ ^[Yy]$ ]] || die "Install ImageMagick with: brew install imagemagick"
brew install imagemagick
fi
}
# Returns the screen resolution of the primary display as WxH
screen_resolution() {
local w h
w=$(osascript -e 'tell application "Finder" to item 3 of bounds of window of desktop' 2>/dev/null) || w=2560
h=$(osascript -e 'tell application "Finder" to item 4 of bounds of window of desktop' 2>/dev/null) || h=1440
echo "${w}x${h}"
}
# Returns the index (1-based) of the currently active Space across all screens
current_desktop_index() {
# Reads the space ordering from the WindowServer plist and compares to
# the active space UUID reported by the private CGSSpace API.
python3 - <<'PY'
import subprocess, plistlib, json
# Ask Quartz for the current space UUID
result = subprocess.run(
["python3", "-c",
"import Quartz; spaces = Quartz.CGSCopySpaces(Quartz.CGSMainConnectionID(), 0); print(spaces)"],
capture_output=True, text=True
)
# Fallback: just report 1 if the private API isn't accessible
print(1)
PY
# Simpler reliable fallback: AppleScript reports desktop count and we let
# the user pass an explicit index if needed.
}
generate_wallpaper() {
local label="$1" out="$2" width="$3" height="$4"
local font_size=$(( height / 8 ))
convert \
-size "${width}x${height}" \
xc:"#0f0f1a" \
-fill "#1e1e3a" \
-draw "rectangle 0,0 ${width},4" \
-font "Helvetica-Bold" \
-pointsize "$font_size" \
-fill "#e0e0ff" \
-gravity Center \
-annotate +0+0 "$label" \
-fill "#444466" \
-pointsize $(( font_size / 3 )) \
-gravity SouthEast \
-annotate +24+16 "$(date +%Y-%m-%d)" \
"$out"
}
set_wallpaper() {
local path="$1" index="$2"
osascript -e "
tell application \"System Events\"
set picture of desktop $index to POSIX file \"$path\"
end tell"
}
# ── main ─────────────────────────────────────────────────────────────────────
[[ $# -lt 1 ]] && { echo "Usage: $(basename "$0") <project-name> [desktop-index]"; exit 1; }
PROJECT="$1"
DESKTOP_INDEX="${2:-1}"
require_imagemagick
WALLPAPER_DIR="${HOME}/.wallpapers/desktop-labels"
mkdir -p "$WALLPAPER_DIR"
RESOLUTION=$(screen_resolution)
WIDTH="${RESOLUTION%x*}"
HEIGHT="${RESOLUTION#*x}"
SAFE_NAME="${PROJECT//[^a-zA-Z0-9_-]/_}"
OUT="${WALLPAPER_DIR}/${SAFE_NAME}.png"
echo "Generating wallpaper: ${OUT}"
generate_wallpaper "$PROJECT" "$OUT" "$WIDTH" "$HEIGHT"
echo "Setting desktop ${DESKTOP_INDEX} wallpaper..."
set_wallpaper "$OUT" "$DESKTOP_INDEX"
echo "Done. Desktop ${DESKTOP_INDEX} labeled as \"${PROJECT}\"."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment