Advertisement
mosaid

changer.sh

Jun 16th, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.87 KB | None | 0 0
  1. #!/bin/bash
  2. #####################################################
  3. ## use this script with crontab and some global keyboard shortcut
  4. ## commands to `sed` its 18th line  (by some script)
  5. ## and enjoy your best wallpapers
  6. ## BY MOSAID
  7. #####################################################
  8. ####                                   directories                                      ####
  9. #####################################################
  10. D0="/media/mosaid/My_Data/Images"                                 # alt H
  11. D1="$D0/collectionWallpapers"                                            # alt G
  12. D2="$D0/001 images/wallhaven/myBackgrunds"                # alt F
  13. D3="$D0/001 images/wallhaven/bg0s"
  14. D4="/home/mosaid/Pictures/wallhaven/myBackgrunds"
  15. D5=""
  16. D6=""
  17. #####################################################
  18. DIR=$D0               ### changed by global keyboard shortcuts
  19. #####################################################
  20. DIRECTORY="/media/mosaid/WindowsDir/Users/mosaid/GoogleDrive/linux/scripts0/wallp"
  21. PREV="$DIRECTORY/.prev"
  22. IMAGES="$DIRECTORY/.wlist"  
  23. FAV="$DIRECTORY/.fav"
  24. NOTES="$DIRECTORY/.notes"
  25. LAST="$DIRECTORY/.last"
  26. #####################################################
  27. function printhelp () {
  28.     string1=$1
  29.     string2=$2
  30.     pad=$( printf '%0.1s' " "{1..60} )
  31.     padlength=25
  32.     printf '\t%s' "$string1"
  33.     printf '%*.*s' 0 $(( padlength - ${#string1}  )) "$pad"
  34.     printf '%s\n' "$string2"
  35. }
  36. function selectPIC () {
  37.    PIC=$( find "$DIR/" -type f -printf "%p\n" | shuf -n1 )
  38.    if  grep -w "$PIC"  "$PREV" >/dev/null
  39.         then return 0  # $PIC is found in history      
  40.    fi
  41.    tmp=$(dirname "$PIC" )
  42.    if  grep -w "$tmp"  "$IMAGES" >/dev/null
  43.      then return 1  # $DIR is listed in $IMAGES
  44.      else return 0  # $DIR not listed in $IMAGES
  45.    fi
  46. }
  47. #####################################################
  48. ####                            handle the args                                     ####
  49. #####################################################
  50. case  "$1"  in
  51.     "a" | "-a" | "--appearance"   )  # mate appearance properties
  52.         mate-appearance-properties >/dev/null 2>&1 &
  53.         ;;
  54.     "p" | "-p" | "--previous" )  #previous wallpaper
  55.         if [ "$DIR" == "" ] ; then
  56.                 SF=$( cat "$FAV" | wc -l )
  57.                 LF=$( cat "$LAST" )
  58.                 let " LF = LF - 1 "
  59.                 if (( $LF <= 1 )) ; then LF=$SF ; fi
  60.                 PIC=$(  sed  -n "$LF"p  "$FAV"  )  
  61.                 echo "$LF" > "$LAST"
  62.             else
  63.                 PIC=$(cat $PREV | tail -2 |sed -n "1p" )   
  64.         fi
  65.         sed -i '$ d' $PREV
  66.         truncate -s -1 $PREV
  67.         ;;
  68.     "c" | "-c" | "--current" )  #print path to current wallpaper
  69.         PIC=$(cat $PREV | tail -1 )
  70.         echo "$PIC"
  71.         ;;
  72.     "o" | "-o" | "--open"  )  #open  current wallpaper
  73.         PIC=$(cat $PREV | tail -1 )
  74.         gnome-open "$PIC"
  75.         ;;
  76.     "f" | "-f" | "--fav"  )  # add current wallpaper to favourites
  77.         PIC=$(cat $PREV | tail -1 )
  78.         L=$( cat "$FAV"  | grep -n "$PIC" | cut -f1 -d: | head -n 1 )
  79.         re='^[0-9]+$'
  80.         if  !  [[ $L =~ $re ]] ; then echo "$PIC" >> $FAV ; else echo "already a favourite" ; fi
  81.         ;;
  82.     "r" | "-r" | "--remove"  )  #remove current wallpaper from favourites"
  83.         PIC=$(cat $PREV | tail -1 )
  84.         L=$( cat "$FAV"  | grep -n "$PIC" | cut -f1 -d: | head -n 1 )
  85.         re='^[0-9]+$'
  86.         if  [[ $L =~ $re ]] ; then   sed -i  "$L"d "$FAV" ; else echo "not a favourite" ; fi
  87.         ;;
  88.     "n" )   #save a note about current wallpaper
  89.         if [ -z "$2"  ] ; then
  90.             echo "usage : `basename $0`  < n >  < note > "
  91.             else
  92.             PIC=$(cat $PREV | tail -1 )
  93.             echo "" >>"$NOTES"
  94.             echo "$2" >>"$NOTES"
  95.             echo "$PIC" >>"$NOTES"
  96.         fi
  97.         ;;
  98.     "nn" | "notes" | "--notes" )
  99.         cat "$NOTES"
  100.         ;;
  101.     "-h" | "h" | "help" | "--help" )
  102.         printhelp "h, -h, --help" "print this help"
  103.         printhelp "" "next wallpaper"
  104.         printhelp "a, -a, --appearance" "mate appearance"
  105.         printhelp "p, -p, --previous"  "previous wallpaper"
  106.         printhelp "c, -c, --current" "print path to current wallpaper"
  107.         printhelp "o, -o, --open" "gnome open current wallpaper"
  108.         printhelp "f, -f, --fav"  "add current wallpaper to favourites"
  109.         printhelp "r, -r, --remove" "remove current wallpaper from favourites"
  110.         printhelp "n [note]"    "save a note about current wallpaper"
  111.         printhelp "nn, notes, --notes"  "print saved notes"
  112.         ;;
  113.     "" )  # no aguments , next wallpaper
  114.             if [ "$DIR" == "" ]  
  115.                 then      #select pic from favourites
  116.                         LF=$( cat "$LAST" )
  117.                         SF=$( cat "$FAV" | wc -l )
  118.                         let " LF += 1"
  119.                         if (( $LF > $SF )) ; then LF=1 ; fi
  120.                         echo "$LF" > "$LAST"
  121.                         PIC=$(  sed  -n "$LF"p  "$FAV"  )  
  122.                 else     #select random pic from $DIR preferably not previously used  and $DIR is listed in $IMAGES
  123.                             result=0
  124.                             count=0
  125.                             while [ $result == 0 ] && (( $count < 20 )) ; do
  126.                                     selectPIC
  127.                                     result=$?
  128.                                     let " count += 1 "
  129.                             done
  130.             fi
  131.             printf "\n$PIC" >> $PREV   # save pic in history
  132.         ;;
  133.     * ) # default
  134.         echo "command not recognized use --help"
  135.         ;;
  136. esac
  137. #####################################################
  138. PID=$(pgrep mate-session)
  139. export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
  140. #####################################################
  141. ###             go ahead if $PIC is a valid image file                       ###
  142. #####################################################
  143. if `file "$PIC" | grep -w image >/dev/null`  ; then
  144.     gsettings set org.mate.background picture-filename "$PIC"
  145.     dim=$( identify -format '%w  %h' "$PIC"  )
  146.     dim=( $dim )
  147.     if ((  ${dim[0]} <= 1920 )) && ((  ${dim[1]} <= 1080 ))
  148.         then option='stretched'
  149.         else option='scaled'
  150.     fi
  151.     gsettings set org.mate.background picture-options "$option"
  152. fi
  153. #####################################################
  154. ####                         truncate history                                        ####
  155. #####################################################
  156. SIZE=100
  157. length=$(cat $PREV | wc -l)
  158. if (( $length > $SIZE )) ; then
  159.     let " length = length - $SIZE "
  160.     sed  -i "1,$length"d $PREV
  161. fi
  162. ##################  THE END    ########################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement