Created
September 7, 2020 16:35
-
-
Save orjanv/f9b40516243214b34eadc3c15a9f2f75 to your computer and use it in GitHub Desktop.
Dual monitor wallpaper on Ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| #! /bin/bash -vx | |
| # input: | |
| # new_background.sh -reset - to reset background to the previous picture | |
| # new_background.sh -set - to set a new background | |
| # | |
| # Found at https://askubuntu.com/questions/1194187/alternatives-to-hydrapaper-for-dual-monitor-wallpapers | |
| lastImage=$HOME/.new_background_last_image.txt | |
| DIR="/usr/share/backgrounds" | |
| joinImage=$HOME/.new_background_last_image.jpg | |
| # choose 2 images from dir | |
| number=$(ls -1 $DIR | wc -l | awk '{print $1}') | |
| mapfile -t screenSize < <( xrandr | grep -e "^DP" | awk ' { print $0 }' ) | |
| if [ ${#screenSize[@]} != 2 ] | |
| then | |
| echo there don\'t appear to be 2 screens - jobs ends | |
| exit | |
| fi | |
| tot=0 | |
| prevH=0 | |
| prevW=0 | |
| prevFile="" | |
| found=0 | |
| SET=-1 | |
| case "$1" in | |
| -reset ) SET=0 | |
| ;; | |
| -set ) SET=1 | |
| ;; | |
| esac | |
| if [ "$SET" -eq -1 ] | |
| then | |
| echo enter either -set or -reset | |
| exit | |
| fi | |
| # reset previous picture | |
| if [ "$SET" -eq 0 ] | |
| then | |
| URI=$(cat $lastImage) | |
| gsettings set org.gnome.desktop.background picture-options 'zoom' | |
| gsettings set org.gnome.desktop.background picture-uri "${URI}" | |
| exit | |
| fi | |
| # find 2 pictures | |
| while [ $tot -lt $number ] | |
| do | |
| # go throguh files at random until 2 have the same height | |
| randomN=$[ ( $RANDOM % $number ) ] | |
| fileName=$(ls -1 $DIR/* | grep -e jpg -e png | awk 'NR=='$randomN' {print $0}') | |
| if [ x$prevFile == x$fileName ] | |
| then | |
| continue | |
| fi | |
| if [ x"" == x$fileName ] | |
| then | |
| continue | |
| fi | |
| if [ x$prevFile != x$fileName ] && [ x"" != x$fileName ] | |
| then | |
| ww=$(convert $fileName -print "Size: %wx%h\n" /dev/null | awk '{ print $2}') | |
| w=$(echo $ww | awk -F x ' {print $1 }') | |
| h=$(echo $ww | awk -F x ' {print $2 }') | |
| if [ $h -eq $prevH ] | |
| then | |
| found=1 | |
| break | |
| fi | |
| fi | |
| prevW=$w | |
| prevH=$h | |
| prevFile=$fileName | |
| tot=$[$tot+1] | |
| done | |
| if [ $found -eq 0 ] | |
| then | |
| echo no files found # may be solved by running the program again | |
| exit | |
| fi | |
| # new width = width of screen * original height of image / height of screen | |
| # = 1920 2160(e.g.) 1080 | |
| pref=$(expr $(mktemp -u) : ".*\.\(.*\)") | |
| outputFile=(${pref}_1 ${pref}_2) | |
| files=( $fileName $prevFile ) | |
| widths=( $w $prevW) | |
| heights=( $h $prevH) | |
| # do each file | |
| reverse=0 # if the second screen is the first in the screenSize list reverse=1 | |
| for I in 0 1; | |
| do | |
| # size of screen I | |
| mapfile -t tt < <(echo ${screenSize[$I]} | grep -E -o '[0-9]+x[0-9]+\+[0-9]+\+[0-9]+' | sed s'/[x+]/ /g' | awk ' { print $1,"\n", $2, "\n", $3, "\n", $4 } ') | |
| # this is the start position of the screen | |
| # if not 0 and I=0 then it is the 2nd screen and reverse images when joining | |
| offset=${tt[2]} | |
| if [ $I == 0 ] && [ $offset != 0 ] | |
| then | |
| reverse=1 | |
| fi | |
| let "newWidth = ${heights[$I]}*${tt[0]}/${tt[1]}" | |
| convert -resize ${newWidth}x${heights[$I]}! ${files[$I]} ${outputFile[$I]} | |
| done | |
| # join the files | |
| if [ $reverse == 0 ] | |
| then | |
| convert +append ${outputFile[0]} ${outputFile[1]} $joinImage | |
| else | |
| convert +append ${outputFile[1]} ${outputFile[0]} $joinImage | |
| fi | |
| URI=$(gsettings get org.gnome.desktop.background picture-uri) | |
| # save the previous file name | |
| echo $URI > $lastImage | |
| uri=file:"//$joinImage" | |
| gsettings set org.gnome.desktop.background picture-options 'spanned' | |
| gsettings set org.gnome.desktop.background picture-uri "${uri}" | |
| rm ${outputFile[0]} ${outputFile[1]} | |
| # end-of-file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment