Skip to content

XFCE dual monitor script

Peter Levi edited this page Jan 12, 2025 · 1 revision

As I wanted a random image for each of my screens, I changed the "set_wallpaper" screen to do so. It keeps track of the last changed monitor and changes the other. If only one screen is connected it will only update this one. Beware: I'm a bash noob so the script propably could be better, maybe someone will look at it, but it works for now. Also I have removed all non-xfce related code.

Note: This is now outdated - XFCE now uses a different method to change the wallpaper. But the principles remain the same and this can be adapted to the current way, just don't expect it to work out of the box.
Note: This script was build for a laptop setup. It assumes the laptopscreen is always there, and checks for a VGA1 screen to start switching between de monitors.

#!/bin/bash
#
# This script is run by Variety when a new wallpaper is set.
# You can use bash, python or whatever suits you for the script.
# Here you can put custom commands for setting the wallpaper on your specific desktop environment
# or run commands like notify-send to notify you of the change, or you can
# run commands that would theme your browser, login screen or whatever you desire.
#
# The first passed parameter (accessed with $1) is the absolute path to the wallpaper image to be set as wallpaper
# (after effects, clock, etc. are applied).
#
# The second passed parameter ($2) is "auto" when the wallpaper is changed automatically (i.e. regular change), "manual"
# when the user has triggered the change or "refresh" when the change is triggered by a change in quotes, clock, etc.
#
# The third passed parameter ($3) is the absolute path to the original wallpaper image (before effects, clock, etc.)
# and "manual" otherwise - e.g. when the user clicks Previous, Next or Trash.


# Here you may apply some additional custom operations on the wallpaper.
# In the end put the path to the actual wallpaper image file in the WP variable
WP=$1

# Custom: ChangedMonitor file
CMFILE=~/.config/variety/.changedmonitor

# Custom: Set the last changed monitor to 0 if the .changedmonitor file can't be found
CM=0
if [ -f $CMFILE ]; then
	CM=$(cat $CMFILE)
fi

# Custom: Keep track of the last changed monitor if VGA1 is connected
if (xrandr | grep "VGA1 connected" > /dev/null) then
	CM=$((1-CM))
else
	CM=0
fi

# Custom: save CHANGEDMONITOR so we can keep track of the latest changed
echo $CM > $CMFILE

# XFCE
if [ $CM -ne 0 ]; then 
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor1/image-path -s "" 2> /dev/null
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor1/image-path -s "$WP" 2> /dev/null
else 
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "" 2> /dev/null
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$WP" 2> /dev/null
fi

# Show a notification on wallpaper change (only when the change is automatic). Display the original filename, but the post-effects image.
 name=$(echo "$3" | sed 's/\//\n/g'| tail -n 1)
 if [ "$2" == "auto" ]; then notify-send --icon "$WP" "Wallpaper changed" "$name" ; fi
Clone this wiki locally