Posted

0 replies · 0 reposts · 1 likes

Just finish writting backgroup wallpaper rotation script.  It can auto rotate wallpaper images from a directory, if using crotab  to invoke the script periodically (crontab -u -e ). chgwp.sh ------------------- #!/bin/bash #Usage: #Gnome #chgwp.sh "" "" "" "" #$1, , [-integer to +integer],optional, default value: 0 #$2, , [DirectoryPath], optional, default value: /usr/share/backgrounds/ #$3, , [StorageFilePath], optional, default value: /tmp/wpnow.tmp #$4, , [Image file format], optional, default value: .jpg$\|.jpeg$\|.gif$\|.png$\|.svg$\|.tiff$ #example  chgwp.sh "1" "/tmp/backgrounds/" "/tmp/wpnow.tmp" ".jpg$\|.png$\|.svg$\|.tiff$" OFFSET=${1:-0} WALLPAPER_DIR=${2:-"/usr/share/backgrounds/"} STORAGE=${3:-"/tmp/wpnow.tmp"} IMGFORMAT=${4:-".jpg$\|.jpeg$\|.gif$\|.png$\|.svg$\|.tiff$"} HM=$(echo $HOME | sed 's;/;\\/;g') WALLPAPER_DIR=$( echo $WALLPAPER_DIR | sed -r "s/^~/$HM/") #Replace ~ with home directory uri # Make sure we can connect to the desktop environment export DISPLAY=":0" export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" #modulo ,,, print out offset_remainder #Bash builtin modulo "%" couldn't manage a negative dividend number, thus creating a custom modulo function solve this problem modulo () {     dividend="$1"     divisor="$2"     inioffset="${3:-0}"          absoffset=$(( dividend < 0 ? -dividend : dividend )) # take absolute value     absremain=$(( absoffset % divisor ))          [[ $absoffset -eq 0 ]] && { printf "%s" "$inioffset"; return; } #exit if offset lenght is same filelist          offset_remainder=$((inioffset + dividend))          if [[ $offset_remainder -lt 0 ]]     then         if [[ $((inioffset - absremain)) -le 0 ]] #Bash can't process negative modulo value         then             offset_remainder=$((( divisor + inioffset - absremain) % divisor ))         else             offset_remainder=$((( inioffset - absremain) % divisor ))         fi     else         offset_remainder=$((offset_remainder % divisor))     fi              printf "%s" "$offset_remainder" } curpos=0 LIST_LENGTH=0 echo $WALLPAPER_DIR #Stop executing if directory couldn't be found [[ ! -d "$WALLPAPER_DIR" ]] && { echo -e "\033[1;31mError:\033[0m Wallpaper Directory: \033[1;32m$WALLPAPER_DIR\033[0m couldn't be found. \033[1;33mStop.\033[0m "; exit; } #Create new storage file, if storage file couldn't be found [[ -f "$STORAGE" ]] && { echo "Found $STORAGE"; curpos=$(cat "$STORAGE"); } || { echo "Counldn't find $STORAGE. Create a new file $STORAGE, previous file position set to 0"; curpos=0; touch $STORAGE; } #Set the position to 0, if the current position isn't a non-negative integers. The position of the first element is 0, the last position is length of list - 1 [[ ! $curpos =~ ^[0-9]+$ ]] && echo "0" > $STORAGE; readarray -d '' tmpLst  < <(find "$WALLPAPER_DIR" -type f | sort -n | grep -i "$IMGFORMAT") #find the image format file in the directory declare -A imglist # define a list i=0 #copy list, first index begin with 0 for itm in ${tmpLst[@]};     do     imglist[$i]=$itm     ((i++)) done unset tmpLst #delete variable to save memory LIST_LENGTH=${#imglist[@]} #the number of files in the directory #Stop execute code if it couldn't find any file in the directory [[ $LIST_LENGTH -lt 1 ]] && { echo -e "\033[1;31mError:\033[0m Couldn't find any file in : \033[1;32m$WALLPAPER_DIR\033[0m. \033[1;33mStop.\033[0m\n\r"; exit; } id=$(modulo "$OFFSET" "$LIST_LENGTH" "$curpos") #Get the position of image uri in a list. iteration. Example: modulo "2" "5" "2" =>4, modulo "-3" "5" "2" => 4, modulo "1" "5" "2" =>3 SELECTED_WALLPAPER="${imglist[$id]}" # Get the image uri from list. CURRENT_WALLPAPER="" COLOR_SCHEME="$(gsettings get org.gnome.desktop.interface color-scheme)" [ "$COLOR_SCHEME" = "'prefer-dark'" ] && CURRENT_WALLPAPER=$(gsettings get org.gnome.desktop.background picture-uri-dark) || CURRENT_WALLPAPER=$(org.gnome.desktop.background picture-uri) CURRENT_WALLPAPER=$(echo "$CURRENT_WALLPAPER" | sed 's/file:\/\///' | sed "s/'//g") #Delete file:// #Stop execute code if new index number is same as current index number. [[ $id -eq $curpos ]] && { echo -e "Wallpaper doesn't change."; exit; } || { echo -e "Wallpaper has changed \n\rfrom \033[1;32m$CURRENT_WALLPAPER\033[0m\n\rto \033[1;32m$SELECTED_WALLPAPER\033[0m. List position: \033[1;32m$id\033[0m."; echo -e "Write the List position to \033[1;32m$STORAGE \033[0m\n\r"; echo "$id" > $STORAGE; } # Determine the current color scheme to decide which property to set COLOR_SCHEME=$(gsettings get org.gnome.desktop.interface color-scheme) # Change the wallpaper based on the color scheme # Set the wallpaper for dark theme, if COLOR_SCHEME='prefer-dark', else set for light theme [ "$COLOR_SCHEME" = "'prefer-dark'" ] && gsettings set org.gnome.desktop.background picture-uri-dark "file://$SELECTED_WALLPAPER" || gsettings set org.gnome.desktop.background picture-uri "file://$SELECTED_WALLPAPER"

View this post on Gab