Skip to content

Instantly share code, notes, and snippets.

@jorvis
Created November 29, 2016 06:50
Show Gist options
  • Save jorvis/7151726e30c714a6d27822d517473c51 to your computer and use it in GitHub Desktop.
Save jorvis/7151726e30c714a6d27822d517473c51 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
This assumes you're using either Gnome3 or Unity and have a directory of wallpapers
here:
~/Pictures/Wallpaper
You probably want to set this as a cron to execute at a certain interval:
Every 15 minutes:
$ crontab -e
# Edit this file to introduce tasks to be run by cron.
#
# m h dom mon dow command
*/15 * * * * /home/jorvis/bin/rotate_wallpaper.py
"""
import datetime
import os
import pwd
import random
import subprocess
def main():
current_user = pwd.getpwuid(os.getuid()).pw_name
image_directory = "/home/{0}/Pictures/Wallpaper".format(current_user)
new_image = random.choice(os.listdir(image_directory))
new_image_path = "{0}/{1}".format(image_directory, new_image)
cmd = "gsettings set org.gnome.desktop.background picture-uri 'file://{0}'".format(new_image_path)
run_command(cmd)
def run_command(cmd):
return_code = subprocess.call(cmd, shell=True)
if return_code != 0:
raise Exception("ERROR: [{2}] Return code {0} when running the following command: {1}".format(return_code, cmd, datetime.datetime.now()))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment