2

I have a laptop, and most of the times I use it it's connected to a monitor. But this monitor has a different resolution than the screen of my laptop, so the background image I use on my monitor doesn't fit on my laptop screen. Is there any way that I can make Ubuntu change the wallpapers when my monitor is connected/disconnected?

1
  • I posted my answer, please let me know if you manage setting up. Apr 28, 2015 at 5:05

1 Answer 1

2

It will take a background script to keep track of whether the second monitor is connected or not.

The script below checks every five seconds if that is the case, and sets the wallpaper accordingly.
The script also remembers the set wallpaper for the two states (connected/disconnected) in a hidden file. The file is automatically updated if you change the wallpaper for either one of the situations.

How to use

  1. Copy the script into an empty file, save it as change_wallpaper.py
  2. In the head section of the script, set the name of your second screen (run the command xrandr from a terminal window, to see the screen's name in the output). I left "VGA-0" as an example.
  3. Start the script with the command (from the terminal):

    python3 /path/to/change_wallpaper.py
    
  4. Connect the second screen, wait a few seconds and set the wallpaper. (the wallpaper has to be changed to remember it). Again wait a few seconds. Disconnect the second monitor, set the wallpaper for the single- monitor situation.

If the wallpaper is set for both situations, the script should change the wallpaper automatically. If you change the wallpaper as usual, the script will remember it for the corresponding situation (connected/disconnected)

If all works fine, add it to your Startup Applications: Dash > Startup Applications > Add the command:

/bin/bash -c "sleep 15&&python3 /path/to/change_wallpaper.py"

The script:

#!/usr/bin/env python3
import subprocess
import os
import time

#--- set the name of your secundary screen below
second = "VGA-0"
#---
# the script uses the key org.gnome.desktop.background picture-uri to set/change the wallpaper
key = ["gsettings get ", "gsettings set ",
    "org.gnome.desktop.background picture-uri"]
# set the name of the wallpaper- datafile(s)    
wallbody = os.environ["HOME"]+"/.wallpaperset"
# define the string to be found (or not) in the output of xrandr
tocheck = second+" connected"

def set_wallpaper(w):
    try:
        cmd = key[1]+key[2]+' "'+open(wallbody+str(w)).read().strip()+'"'
        subprocess.Popen(["/bin/bash", "-c", cmd])
    except FileNotFoundError:
        pass

check = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
# first call of xrandr, and get the current wallpaper
xr1 = tocheck in check("xrandr")
if xr1 == True:
    set_wallpaper(2)
else:
    set_wallpaper(1)
wall1 = check(key[0]+key[2])

while True:
    time.sleep(5)
    # second call of xrandr, and get the current wallpaper
    xr2 = tocheck in check("xrandr")
    wall2 = check(key[0]+key[2])
    # if the second screen either connects or disconnects, change the wallpaper
    if (xr1, xr2).count(True) == 1:
        w = 2 if xr2 == True else 1
        set_wallpaper(w)
    # if wallpaper is changed, remember the new wallpaper
    elif wall2 != wall1:
        w = 2 if xr2 == True else 1
        open(wallbody+str(w), "wt").write(wall2)
    xr1 = xr2
    wall1 = wall2
1
  • This is a great script, thanks. I added a line to make the script default to 'spanned' rather than 'zoom'. There's a few more options listed here: access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/… I added "org.gnome.desktop.background picture-options" to the end of the key array, and then repeated the lines in the set_wallpaper try block with cmd = key[1] + key[3] + ' "spanned"' (I just hardcoded the stretch mode I wanted) Jul 13, 2016 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .