#!/bin/bash
# PixelTracing - remove only Finder's download-source extended attribute.
# Usage: bash remove-where-from.sh /path/to/image.png [more image paths]
# No recursion, sudo, network access, or quarantine changes.
set -u
if [ "$(uname -s)" != Darwin ]; then echo 'This script is for macOS only.' >&2; exit 1; fi
if [ "$#" -eq 0 ]; then echo 'Supply one or more image file paths. Drag files into Terminal after the script path.'; exit 1; fi
attr='com.apple.metadata:kMDItemWhereFroms'
failed=0
for file in "$@"; do
  case "$file" in /*) ;; *) file="$PWD/$file";; esac
  if [ ! -f "$file" ] || [ -L "$file" ]; then printf 'Skipped (not a regular image file): %s\n' "$file" >&2; failed=1; continue; fi
  case "$file" in *.[jJ][pP][gG]|*.[jJ][pP][eE][gG]|*.[pP][nN][gG]|*.[wW][eE][bB][pP]) ;; *) printf 'Skipped (unsupported extension): %s\n' "$file" >&2; failed=1; continue;; esac
  if ! attrs=$(/usr/bin/xattr "$file"); then failed=1; continue; fi
  if printf '%s\n' "$attrs" | /usr/bin/grep -Fxq "$attr"; then
    if /usr/bin/xattr -d "$attr" "$file"; then printf 'Removed Where from: %s\n' "$file"; else failed=1; fi
  else printf 'No Where from attribute: %s\n' "$file"; fi
done
exit "$failed"
