summaryrefslogtreecommitdiff
path: root/scripts/random_wallpaper.py
blob: 55bb61805b48017a94fc1aa841c972b0fcec4686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)