diff options
Diffstat (limited to 'scripts/random_wallpaper.py')
-rwxr-xr-x | scripts/random_wallpaper.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/scripts/random_wallpaper.py b/scripts/random_wallpaper.py new file mode 100755 index 0000000..55bb618 --- /dev/null +++ b/scripts/random_wallpaper.py @@ -0,0 +1,55 @@ +#!/usr/bin/python +# Reads a given directory and set a random +# wallpaper using the images on that dir. + +import subprocess +import os +import os.path +import sys +import random + +if not 'WALLPAPERDIR' in os.environ: + sys.stderr.write('WALLPAPERDIR variable not set, using default value.\n') + d = os.path.expandvars('${HOME}/${FVWM_USERDIR}/images') +else: + d = os.path.expandvars('${WALLPAPERDIR}') + +sys.stderr.write('Trying directory %s.\n'%d) +if not os.path.isdir(d): + sys.stderr.write('Path %s is not a directory, aborting.\n'%d) + sys.exit(-1) + +files = os.listdir(d) +random.seed() +try: + f = random.choice(files) +except IndexError: + sys.stderr.write('No files in dir %s, aborting.\n'%d) + sys.exit(-1) + +sys.stderr.write('Selected file %s.\n'%f) + +try: + p = subprocess.Popen('xdpyinfo', stdout = subprocess.PIPE) + for line in p.stdout: + if 'dimensions' in line: + res = line.split()[1] + break + sys.stderr.write('X screen resolution is %s.\n'%res) +except (OSError, IndexError, NameError): + sys.stderr.write('Error getting X screen resolution, aborting.\n') + sys.exit(-1) + +try: + subprocess.check_call(['convert', os.path.join(d, f), '-resize', res, os.path.expandvars('${FVWM_USERDIR}/tmp/current_wall.png')]) +except subprocess.CalledProcessError: + sys.stderr.write('Error resizing the wallpaper. Is Imagemagick installed?\n') + sys.exit(-1) + +try: + subprocess.check_call(['feh', '--bg-center', os.path.expandvars('${FVWM_USERDIR}/tmp/current_wall.png')]) +except subprocess.CalledProcessError: + sys.stderr.write('Error setting the wallpaper. Is feh installed?\n') + sys.exit(-1) + +sys.exit(0) |