I like to have random wallpapers while working on PC, a kind of window opened to the world.
Now add to that a set of wonderful and free pictures from Unsplash, and you’ll need nothing else to stay happy and motivated!
For Windows, there are tools that do that we no big deal, like DMT.
For Ubuntu, I didn’t find such tool, so I’m tweaking something here (that’s Linux power :))
Thanks to this post, I have enough help to start. It already uploads random picture and set it as wallpaper.
#!/bin/bash wget -O /tmp/wallpaper.jpg https://source.unsplash.com/random gsettings set org.gnome.desktop.background picture-uri file:///tmp/wallpaper.jpg
Create a script with above code (I’m doing it in /bin folder):
sudo nano unsplash.sh
Ctrl+X, then Y, then Enter.
Make it executable, and run it:
sudo chmod +x unsplash.sh
You’ll see after some seconds that the wallpaper changes to a new one.
If nothing happens or you lost the previous one, it is may be a problem of connection
Now let’s execute it every 5 minutes using crontab, edit it:
Choose an editor with its number, then add at the end the line:
*/5 * * * * /bin/unsplash.sh
Ctrl+X, then Y, then Enter.
Here is a good explanation of crontab syntax, I quote the part:
The stars represent different date parts in the following order:
- minute (from 0 to 59)
- hour (from 0 to 23)
- day of month (from 1 to 31)
- month (from 1 to 12)
- day of week (from 0 to 6) (0=Sunday)
It mean every 5 minutes, every hour, every day, and every month.
For the moment this basic script works fine 🙂
If you want to have images with specifi keyword, in the script, replace :
Where nature and water and keywords, and 1600×900 are the resolution of your screen.
If you have any remark or suggestion please add a comment. Thank you.
Old script before 04/10/2019 is someone still need it:
#!/bin/bash wget -O /tmp/wallpaper.jpg https://unsplash.it/2560/1440/?random gsettings set org.gnome.desktop.background picture-uri file:///tmp/wallpaper.jpg
Here are the improvements I made:
- Create the script in /usr/bin/ instead of /bin/, respecting that /bin/ directory is for resources mandatory for boot step.
- Store the wallpapers in /var/tmp/ instead of /tmp/, assuming that we want to keep these images in temporary but persistent folder.
- Check if we are using a metered connection or not, to avoid consuming data while using limited or expensive connection.
- Concatenate the wallpaper download and change steps, if wget response isn’t positive, no need to go further.
The improvements I’m still trying to add:
- Copy the new wallpaper only if it is 100% downloaded, to avoid the case of corrupted file due to an interruption e.g. power loss.
- Add a timeout to limit the waiting time for wget.
Now the v2 script procedure.
sudo nano unsplash_v2.sh
Copy paste the code:
#!/bin/bash if [[ $(nmcli -t -f GENERAL.METERED dev show wlanx) = 'GENERAL.METERED:no (guessed)' ]]; then wget -O /var/tmp/wallpaper.jpg https://source.unsplash.com/1600x900/?nature,water && gsettings set org.gnome.desktop.background picture-uri file:///var/tmp/wallpaper.jpg fi
There are two important parameters to change based on your machine.
- wlanx: This is the name of your internet interface.
- GENERAL.METERED:no (guessed): This status of metered property.
To find 1), use the command :
To find 2), run the following command:
nmcli -t -f GENERAL.METERED dev show wlanx (replace wlanx with your interface)
Save the script: Ctrl+X, then Y, then Enter.
Make it executable:
sudo chmod +x unsplash_v2.sh
The cron step is the same as for the first version of the script above.