#!/usr/bin/env bash set -euo pipefail # Tidy a macOS Desktop. Safe to run via: curl -fsSL | bash # Screenshots → archive, PDFs → pdfs, other images and CSVs → others. if [[ "$(uname -s)" != "Darwin" ]]; then echo "clean.sh is meant for macOS." >&2 exit 1 fi DESKTOP="${HOME}/Desktop" ARCHIVE="$DESKTOP/archive" PDFS="$DESKTOP/pdfs" OTHERS="$DESKTOP/others" mkdir -p "$DESKTOP" "$ARCHIVE" "$PDFS" "$OTHERS" cd "$DESKTOP" lower_ext() { printf '%s' "${1##*.}" | tr '[:upper:]' '[:lower:]' } is_screenshot() { local name="${1##*/}" [[ "$name" =~ ^[Ss]creen[[:space:]]*[Ss]hot ]] || [[ "$name" =~ ^[Ss]creenshot ]] } is_image() { case "$(lower_ext "$1")" in png|jpg|jpeg|gif|webp|bmp|tiff|tif|heic|heif) return 0 ;; *) return 1 ;; esac } move_file() { local src="$1" local dest_dir="$2" local base="${src##*/}" local dest="$dest_dir/$base" if [[ -e "$dest" ]]; then local stem="${base%.*}" local suffix="${base##*.}" local n=1 if [[ "$stem" == "$base" ]]; then while [[ -e "$dest_dir/${base}-$n" ]]; do n=$((n + 1)); done dest="$dest_dir/${base}-$n" else while [[ -e "$dest_dir/${stem}-$n.${suffix}" ]]; do n=$((n + 1)); done dest="$dest_dir/${stem}-$n.${suffix}" fi fi mv "$src" "$dest" echo "Moved: $base -> ${dest#$DESKTOP/}" } for item in "$DESKTOP"/*; do [[ -e "$item" ]] || continue [[ -f "$item" ]] || continue base="${item##*/}" [[ "$base" == "clean.sh" || "$base" == ".DS_Store" ]] && continue if is_screenshot "$base"; then move_file "$item" "$ARCHIVE" elif [[ "$(lower_ext "$base")" == "pdf" ]]; then move_file "$item" "$PDFS" elif [[ "$(lower_ext "$base")" == "csv" ]] || { is_image "$item" && ! is_screenshot "$base"; }; then move_file "$item" "$OTHERS" fi done echo "Done. Folders: archive, pdfs, others."