Dynamic Random Wallpaper by Time
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 the proposition is?
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 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 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 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.
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.
/Utilities/Script Editor.app and paste the following inside:
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 controling the variable time
h to set current
currentTime 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 currentTime to morning
h (time variable) is...
> (bigger than)
< (is smaller than)
set currentTime/folderName to it:
set currentTime to morning
For now let's go with the regular script above. 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. I've saved mine to:
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.
/Applications/Utilities/Terminal.app and set your editor to
nano, as it is easier (to explain and to use).
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 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 you can do that too!
Check this webpage http://www.cronmaker.com/, it has an excellent and easy cron calculator.
Now lets save the cronfile, press
CTRL + O then press
Enter to 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. And it will repeat the process each X seconds.
Enjoy your dynamic time sensitive automatic wallpaper setup!