Background Image
 
Main
 
Nav
Jun
4
2014

Automatic daily random wallpaper

I love wallpapers. When Windows Vista added the feature to shuffle wallpapers from a folder of photos, I was happy. I set it to rotate a bunch of cool Zelda screenshots. But eventually after I'd looked at them all a hundred times, I wanted some new ones. So I went to Google images and found a bunch of cool new photos. But after a week or so, they felt stale again. Gathering up a bunch of new images takes a good bit of time, and by the time I've gathered them they're already starting to feel stale. CHALLENGE: I want a brand new image that I'd never seen before to magically show up on my desktop each day.

Enter scripting! I tinkered away at a script in Python which calls Google's search API using a random phrase of my choosing. I then created a Windows task to run the script 3 minutes after Windows starts.

If you want to install the script, just follow the steps below! I included Windows and Mac instructions.

1: Install and modify script

Just save the script somewhere like your Documents folder. Open the file in a text editor and change the top wallpaper_dir variable to be a folder where your wallpapers will go. Make sure the folder exists and is empty, and don't use it for storing other things as the script deletes its contents daily. Modify the search phrases at the top to your liking. A term from each group (one adjective and one subject) is randomly selected, and then "wallpaper" is added. So for example, a term the script searches for might be "beautiful landscape wallpaper". I found adding "wallpaper" to the search tends to filter out mediocre looking (yet still high resolution) images.

2: Get a Google API key and Search Engine (both free)

Go to the Google dev console, sign in and create a new project. Name it whatever. In the left hand column click "APIs & Auth", and in the list that appears find "Custom Search API" and switch the OFF button at the right to ON. Then in the leftmost column click "Credentials" and under "Public API access" click "Create a new Key" and choose "Browser key". Create the key, and then copy the contents of "API key" to the variable api_key at the top of grab-new-wallpaper.py.

Now go to Google Search Engine console and add a new search engine. Enter "deviantart.com", "flickr.com", and whatever other image sites you want to emphasize (it's not restricted to these only) in the "Sites to search" box, and name your search engine whatever you want, then save it. Now click "Control Panel" to modify the engine. Change "Image Search" to ON. Under "Sites to search" change the dropdown to "Search the entire web but emphasize included sites". Now under "Details" click "Search Engine ID" and copy the value into the engine_id variable at the top of grab-new-wallpaper.py.

3: Install Python

You'll need to have Python 2.xx (the latest Python 2 version before Python 3 which is experimental) installed to run the script. I believe Mac OS has it preinstalled, which you can test by typing "python" at the terminal. It is lightweight, only 50mb installed. You can download it here. In the installer, on the third screen make sure to select "Add python.exe to path", it will not install by default for some reason. That way (irrelevant in this case but useful nonetheless) you can type python at a command prompt and be able to use the Python interpreter, or type python path/to/script.py and run a python script. After it installs, you should be able to double click to run .py files. If not, right click the grab-new-wallpaper.py file and "Open with..." and "Choose default program" then select python.exe.

Double click to run the grab-new-wallpaper.py, and it should fetch an image and put it in your wallpaper directory. Copy and paste this image in place so that you have 2 images, or just paste any 2 random images into this folder for the next step.

4: Set up wallpaper in OS settings

Windows: Right click on the desktop, select "Personalize", then "Desktop Background" at the bottom left. Browse to select your wallpaper directory, and click OK. Then click "Select all" at the right to select all (both) of the images in that directory. At the bottom change "Picture position" to "Fill" and "Change picture every" to "10 seconds". Then "Save changes". This step is important as it will tell Windows to change the desktop background when a new image appears in the directory.

Mac: Go to System Preferences, Desktop & Screensaver, then add your wallpaper directory, and check "Change picture: Every 5 seconds" and select "Fill Screen" at the top.

5: Set up the scheduled task

Windows: Go to Control Panel > Administrative Tools > Task Scheduler, or on Windows 7 just hit the Windows key and type "task sch". Click "Create Task" at the top right, then enter "Wallpaper grab" in the name field. On the "Triggers" tab, add a new trigger, then select "At startup" at the top and check "Delay the task for" and change 15 to 3 minutes, then click OK. This will give your internet connection a little time to become active first. If your computer has a slow boot and your internet isn't available that fast, you may need to make this a longer delay. On the Actions tab, add a new Action, and browse to select grab-new-wallpaper.py, then click OK again. Click OK to finish creating the task. When you restart your computer, the script should now run after 3 minutes! If you need to edit the task, open Task Scheduler again, find it at the bottom of the list at the bottom, double click it to open it, then click "Properties" in the column on the right.

Mac: Start the Automator app, choose Application, then in the left column select Utilities, and double click Run Shell Script. Paste in python /Users/XXXX/Documents/grab-new-wallpaper.py making sure the path is correct. Hit Cmd+S to save the application as "Wallpaper grab". Then go to System Preferences, Users & Groups, then Login Items. Click the plus at the bottom, then in the left click Applications and scroll down to find "Wallpaper grab", and add it.

OPTIONAL: Set up a shortcut key

Sometimes you might want to grab a new wallpaper manually, for instance when you don't like the image it chose (rare but occasionally happens, there will be a watermark or just a boring image or something). You can navigate to your script location and double click it to run it, or you could set up a shortcut key like I did. To do this, download and install AutoHotKey. Then save my hotkey list somewhere permanent. Right click it and "Edit script" to modify the location of your grab-new-wallpaper.py. As you can see it contains shortcuts to change wallpaper (Win+W), empty the Recycle Bin (Win+R), and restart the computer (Win+F4). Save and close the file, then double click it to open it in AutoHotKey. Then go to "Start > All Programs", find the shortcut for AutoHotKey, right click and "Send to > Desktop". Then take that shortcut on the desktop and drag it into "Start > All Programs", then into the "Startup" folder. That will make AutoHotKey run when Windows starts.