wallpaper-set-random.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # TODO: implement filters on images with imagemagick
  4. display_help(){
  5. echo "Usage: ${0##*/} [path-to-search-for-wallpapers] [center|fill|max|scale|tile]"
  6. exit 2
  7. }
  8. [[ $# -lt 1 || ! -d $1 ]] && display_help
  9. ROOT_PATH="$(realpath "$1")"
  10. wallpaper_paths_file="${HOME}/.wallpapers-paths_${ROOT_PATH//\//_}.txt"
  11. feh_wallpaper_flag="--bg-${2:-max}"
  12. fill_wallpapers_file(){
  13. find \
  14. -L "$ROOT_PATH" \
  15. -mount \
  16. -type f \
  17. -size +500k \
  18. -size -10M \
  19. -iregex ".*\.\(png\|jpe?g\)$" \
  20. -not -iname "*screenshot*" \
  21. 2>/dev/null | shuf > "$wallpaper_paths_file"
  22. }
  23. check_if_wallpaper_correct(){
  24. wallpaper="$1"
  25. echo "Setting ${wallpaper}"
  26. if [[ -z $wallpaper ]]; then
  27. echo "ERROR: File is empty? ${wallpaper}"
  28. exit 2
  29. fi
  30. }
  31. set_wallpaper(){
  32. wallpaper="$1"
  33. check_if_wallpaper_correct "$wallpaper"
  34. if pgrep gnome-shell; then
  35. gsettings set org.gnome.desktop.background picture-uri "file:///${wallpaper}"
  36. # other options: none, wallpaper, centered, scaled, stretched
  37. gsettings set org.gnome.desktop.background picture-options scaled
  38. # color=$(convert "$wallpaper" -scale 1x1\! -format '%[pixel:u]' info:-)
  39. # gsettings set org.gnome.desktop.background primary-color "$color"
  40. elif pgrep mate; then
  41. gsettings set org.mate.background picture-filename "$wallpaper"
  42. else
  43. feh "$wallpaper" "$feh_wallpaper_flag"
  44. fi
  45. }
  46. get_next_wallpaper(){
  47. head -n1 "$wallpaper_paths_file"
  48. sed -i '1d' "$wallpaper_paths_file" # delete first wallpaper path
  49. }
  50. get_remaining_wallpaper_info(){
  51. echo "Remaining wallpapers: $(wc -l $wallpaper_paths_file)"
  52. }
  53. if [[ ! -f $wallpaper_paths_file || $(awk 'END {print NR}' "$wallpaper_paths_file") -le 1 ]]; then
  54. fill_wallpapers_file
  55. fi
  56. set_wallpaper "$(get_next_wallpaper)"
  57. get_remaining_wallpaper_info