Search code examples
pythoncronwallpaperxfce

Python script to change wallpaper depending on time of day


I'm running Linux Mint 13 with xfce. Using the script in this thread, I was able to get the cronjob running with this format:

PATH=/usr/bin/python/:/usr/bin/python3/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# The Wallpaper Changer
0 * * * * /home/tessitura/8BitDay/set.py

... but I'm running into some problems with the script itself. As is, changing nothing but the directory name, it gives me the following error:

Traceback (most recent call last):
  File "./set.py", line 19, in <module>
    os.rename('now.png', current)    
OSError: [Errno 2] No such file or directory

I've tried tweaking the code a little, and it sort of works; the wallpaper changes, but now.png ends up getting deleted, resulting in a blank image when the cronjob runs. Here's what I have now:

#!/usr/bin/python3    

# Finds the current hour
import datetime
time = int(str(datetime.datetime.now().time()).split(":")[0])    

# Needed for renaming files
import os    

# List of all files in the folder
files = ['05-Evening.png', 'set.py', '07-Night.png', '01-Morning.png', '03-Afternoon.png', '06-Late-Evening.png', '08-Late-Night.png', '04-Late-Afternoon.png', '02-Late-Morning.png', 'now.png']   

# Finds which wallpaper is currently set
directory = "/home/tessitura/8BitDay/"
for filename in os.listdir(directory):
    files.remove(files[files.index(filename)])
    current = ''.join(filename)    

# Puts back the current wallpaper
path = os.path.join(directory, 'now.png')
os.rename(path, current)    

# Gets out the new wallpaper based on time
if 0 <= time <= 3:
    os.rename('08-Late-Night.png', 'now.png')
elif 4 <= time <= 5:
    os.rename('01-Morning.png', 'now.png')
elif 6 <= time <= 10:
    os.rename('02-Late-Morning.png', 'now.png')
elif 11 <= time <= 14:
    os.rename('03-Afternoon.png', 'now.png')
elif 15 <= time <= 16:
    os.rename('04-Late-Afternoon.png', 'now.png')
elif 17 <= time <= 18:
    os.rename('06-Late-Evening.png', 'now.png')
elif 19 <= time <= 23:
    os.rename('07-Night.png', 'now.png')    

# Refreshes the desktop
os.system("xfdesktop --reload")

UPDATE: Blckknght's solution fixes the script. Everything is fine in Mint 13, but I've since upgraded to Mint 17.1, and I'm running into problems again. The script works just fine standalone, but this time, the issue is with crontab. Running the hourly cronjob results in this:

Failed to parse arguments: Cannot open display: 

I changed the cronjob to this...

PATH=/usr/bin/python/:/usr/bin/python3/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@hourly DISPLAY=:0.0 /home/tessitura/8BitDay/set.py > /home/tessitura/set.log 2>&1

Which gives me this error:

Failed to connect to session manager: Failed to connect to the session manager: SESSION_MANAGER environment variable not defined

** (xfdesktop:9739): WARNING **: xfdesktop: already running, quitting.

Solution

  • Your current code is more complicated than it needs to be because you are renaming the files, and thus need to rename the current now.png file back to its original name.

    It would be much simpler to copy the file appropriate for the time to the new name, overwriting the existing file at that location if one exists. Because you're copying rather than renaming, you never need to reverse the process.

    Here's a version of your code that does this using shutil.copy:

    import datetime
    import os
    import shutil
    
    time = datetime.datetime.now().hour  # no need for string parsing to get the hour
    
    # Gets out the new wallpaper based on time
    if 0 <= time <= 3:
        shutil.copy('08-Late-Night.png', 'now.png')
    elif 4 <= time <= 5:
        shutil.copy('01-Morning.png', 'now.png')
    elif 6 <= time <= 10:
        shutil.copy('02-Late-Morning.png', 'now.png')
    elif 11 <= time <= 14:
        shutil.copy('03-Afternoon.png', 'now.png')
    elif 15 <= time <= 16:
        shutil.copy('04-Late-Afternoon.png', 'now.png')
    elif 17 <= time <= 18:
        shutil.copy('06-Late-Evening.png', 'now.png')
    elif 19 <= time <= 23:
        shutil.copy('07-Night.png', 'now.png')    
    
    # Refreshes the desktop
    os.system("xfdesktop --reload")