Random Wallpaper by Time of the Day

Update (11/30/2019): Linux version.


I want my wallpapers to change each X minutes automagically. You are probably thinking: "but there is an option in the System preferences to do just that!". Well not the way I want.

Around dusk or dawn I want a specific set of wallpapers to be displayed, during the morning I want another set, during the afternoon another and at night yet another. Also, the swap time for the build-in settings is hard coded to preset values. So if I want to set them in System preferences to change say each 12 minutes, I am out of luck because the timeout option is not available. But with this solution we can set any timeout.

What do We Need?

Aside from the wallpapers themselves, not much else. Everything we need is build-in inside every OS X system.

We'll use an Applescript script to do the "math" and calculate what time it is "now" and set a random wallpaper based on that time. And we also will be using cron to execute the script at each X minutes so the wallpaper will keep changing after some time.

Initial Setup

Open Finder and go to your Pictures folder, there create a folder called Wallpaper. Inside this Wallpaper folder create 4 more folders with the names:

  • Afternoon
  • DuskDawn
  • Morning
  • Night

So the hierarchy will look like this:

Now place your DuskDawn Morning Afternoon Night wallpapers in their respective folders.

Open /Utilities/Script Editor.app and paste the script that contains our time sensitive function:

macOS script

set dusk to "DuskDawn"
set morning to "Morning"
set afternoon to "Afternoon"
set dawn to "DuskDawn"
set night to "Night"
set h to hours of (current date)
set currentTime to morning

if (h > 7 and h < 12) then
    set currentTime to morning
else if (h ≥ 12 and h < 16) then
  set currentTime to afternoon
else if (h ≥ 16 and h < 19) then
  set currentTime to dawn
else if (h ≥ 19 or h < 4) then
    set currentTime to night
else if (h ≥ 4 or h < 7) then
 set currentTime to dusk
end if

on fetchImage(folderName)
    tell application "Finder"
       return some file of folder ("Pictures:Wallpaper:" & folderName) of    home as text
    end tell
end fetchImage

tell application "Finder"
       set wallImage to my fetchImage(currentTime)
       set desktop picture to wallImage
    end try
end tell

You can see that the if and else if statements are setting the time variable in currentTime that in turn will be used to fetch images from a specific folder, in this case morning afternoon, etc.

Now press CTRL + S to save the script, make sure you save it as a Script. Save the file to some location inside your HDD. We'll use this path later to automate the change. So take note of the path where you are saving the script.

Note: If you don't want the wallpapers to change automatically you can save it as an .app (Application) instead of script, then move the new application into your /Applications folder and drag it to the Dock.

Automation with Cron

If you run the script it should now be functional, but we don't want to run it manually. We'll use cron to run the script each X minutes. The amount of time between each execution is a personal decision. I've used 10 minutes.

Open /Applications/Utilities/Terminal.app and set your editor to nano:

export EDITOR='nano'

Lets list the cronjobs we have there (probably none at this point):

 crontab -l

Now lets add the line that will tell cron to execute this regularly. Open crontab in edit mode:

 crontab -e

And type/paste inside the "magic" line:

 */10 * * * * osascript /Users/MY_USER/Documents/Scripts/Wallpaper.scpt

Change the path and filename accordingly to where you saved the script.

This line above is telling the cron daemon to execute the script once every 10 minutes of every hour of every day of every week of every year. You can change that 10 (minute) value to any number between 1 and 59.

Check http://www.cronmaker.com. it has an excellent and easy cron calculator.

When done editing save the cronfile by pressing CTRL + O then press Enter to accept the name and write the changes. CTRL + X to exit nano.

That's it! If everything was done accordingly the script will pick a random wallpaper from one of the folders we've created, which one will be determined by the current time of the day (or night), and change the desktop background to it.



hour=$(date +%R | sed 's/\:.*//')

if [ "$hour" -ge 7 ] && [ "$hour" -lt 12 ]
elif [ "$hour" -ge 12 ] && [ "$hour" -lt 17 ]
elif [ "$hour" -ge 17 ] && [ "$hour" -lt 18 ]
elif [ "$hour" -ge 18 ] && [ "$hour" -le 23 ]
elif [ "$hour" -ge 0 ] && [ "$hour" -lt 6 ]
elif [ "$hour" -ge 6 ] && [ "$hour" -lt 7 ]

imgFile=$(ls -1 $imgPath | shuf -n 1 | awk '{print $NF}')

export bgNow=""$imgPath$imgFile""

/usr/bin/gsettings set com.deepin.wrap.gnome.desktop.background picture-uri "$bgNow"
{{ message }}

{{ 'Comments are closed.' | trans }}