Skip to content

Instantly share code, notes, and snippets.

@urandom
Last active May 26, 2019 19:10
Show Gist options
  • Save urandom/a51cdacab2522ff8ae32c7c22e79039e to your computer and use it in GitHub Desktop.
Save urandom/a51cdacab2522ff8ae32c7c22e79039e to your computer and use it in GitHub Desktop.
Change gnome3's wallpaper from a random one in a directory
#!/usr/bin/python3
import os
import random
import subprocess
import sys
import urllib.request, urllib.parse, urllib.error
from os.path import isfile, join
if len(sys.argv) == 1:
raise RuntimeError("No wallpaper path")
path = sys.argv[1]
wallpapers = [f for f in os.listdir(path) if isfile(join(path, f))]
wallpaper = random.choice(wallpapers)
wallpaper = urllib.parse.quote(join(path, wallpaper))
subprocess.check_call(["/usr/bin/gsettings", "set", "org.gnome.desktop.background", "picture-uri", "file://{}".format(wallpaper)])
subprocess.check_call(["/usr/bin/gsettings", "set", "org.gnome.desktop.screensaver", "picture-uri", "file://{}".format(wallpaper)])
*/10 * * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus DISPLAY=:0 ~/.local/bin/change-wallpaper /usr/share/wallpapers
[Unit]
Description=Rotate GNOME background
[Service]
Type=oneshot
Environment=DISPLAY=:0
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
ExecStart=~/.local/bin/change-wallpaper /usr/share/wallpapers
[Install]
WantedBy=basic.target
[Unit]
Description=Rotate GNOME wallpaper timer
[Timer]
OnCalendar=*:0/10
Persistent=true
Unit=gnome-background-change.service
[Install]
WantedBy=gnome-background-change.service
systemctl --user enable gnome-background-change.timer
systemctl --user start gnome-background-change.timer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment