macOS

Dynamic Random Wallpaper by Time

Intro

It's been a while since I wanted to do this, but I always lazy my way out of it. Yesterday I revamped my wallpaper collection. I've removed some, added some and basically edit them all to "fix" color, size, brightness, contrasts and so on. So I decided to go head and work on the proposition.

And what is the proposition?

Well 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 around the dawn another and at night yet another. Also the swap time for the build-in settings is hardcoded, that is, the amount of time is fixed, so if I want to set them to change say each 12 minutes, I am out of luck because that amount of time is not available.

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 and to 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.

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

  • DuskDawn
  • Morning
  • Afternoon
  • Night

So the hierarchy will look like this:

Folders

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

Open /Utilities/Script Editor.app and paste the following inside:

The 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 timeNow to morning

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

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

tell application "Finder"
    try
        set displayPicture to my getImage(timeNow)
        set desktop picture to displayPicture
    end try
end tell

You can see that the if and else if statements are controling the variable time h to set current timeNow that in turn will be used to fetch images from a specific folder, in this case morning afternoon, etc. So for instance:

if (h > 7 and h < 12) then
    set timeNow to morning

If h (time variable) is...

> (bigger than) 7...

and < (is smaller than) 12...

set timeNow/folderName to it:

set timeNow to morning

For now let's go with the regular script above. Now press CMD + S to save the script, make sure you save it as a Script. Save the file to some location inside your HDD. I've saved mine to:

/Users/MY_USER/Documents/System/Scripts/Wallpaper.scpt

Note1: We'll use this path later to automate the change. So take note of the path where you are saving the script.

Note2: If you don't want the wallpapers to change automatically you can save as an .app (Application) instead of script, then move the new application into your /Applications folder and drag it to the Dock. Every time you click on it it will change the wallpaper based on the time of the day.

Personally I choose to make both. So it changes automagically, but I also can click on this app and it will change the wallpaper on demand.

Automation with Cron

If you run the script it should now be functional. But we don't want to run manually we want everything automatic. We'll use cron to keep executing this script each X minutes again and again. The amount of time between each execution is a personal decision. I've set to execute each 10 minutes.

Open /Applications/Utilities/Terminal.app and set your editor to nano, as it is easier (to explain and to use).

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:

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

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

This line above is telling the cron daemon to execute the script once every 10 minutes. You can change that 10 (minutes) to any number between 1 and 59. Or if you wish to use another setting, for instance change only three time per day, or four time each week, you can do that too!

Check this webpage https://crontab.guru/, it has an excellent and easy cron calculator.

Now lets save the cronfile, press CMD + O then press Enter to write the changes. CMD + 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. And it will repeat the process each X seconds.

Enjoy your dynamic time sensitive automatic wallpaper setup!