Random Wallpaper by Time of the Day
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 hardcoded 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.
Finder and go to your
Pictures folder, there create a folder called
Wallpapers. Inside this
Wallpapers folder create 4 more folders with the names:
So the hierarchy will look like this:
Now place your
Night wallpapers in their respective folders. The name of the image files don't matter.
/Utilities/Script Editor.app and paste the script that contains our time sensitive function:
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" try set wallImage to my fetchImage(currentTime) set desktop picture to wallImage end try end tell
You can see that the
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.
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
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.
/Applications/Utilities/Terminal.app and set your editor to
Lets list the
cronjobs we have there (probably none at this point):
Now lets add the line that will tell
cron to execute this regularly. Open crontab in edit mode:
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
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.