Download new image everyday and change desktop background automatically

Recently I am using Windows 10 machine in office. One nice feature I love of win 10 is daily lock screen background change. Whenever I go to office, I see a nice new background picture in lock screen. Basically it downloads a new nice image everyday and set it as lock screen background.

I wanted to create similar functionality in my own home machine. National geographic publishes a nice photo every day (http://www.nationalgeographic.com/photography/photo-of-the-day). I need to create a script which will run everyday and download the photo.

After a little effort, I managed to write a simple shell script to download the photo.

currdate=$(date +"%Y%m%d")
img_file="/Users/shuvankar/Desktop/Shuvankar/PhotoGrabber/$currdate.jpg"
#echo $img_file
#echo $currdate
if [ -f "$img_file" ]
then
	echo "File already exists"
else
	img="$(curl https://www.nationalgeographic.com/photography/photo-of-the-day/ -s | grep -o "og:image[^\]*" | grep -o "https://[a-zA-Z0-9./?=_-]*")"
	if [ -n "$img" ]
	then
		echo "Downloading $img"
		curl -o $img_file $img
		echo "Wallpaper downloaded successfully and saved as $img_file"
	else
		echo "No Wallpaper today"
	fi
fi

The logic is pretty simple.

  • Using curl, download the source code of the national geographic photo of the day webpage.
  • search the exact file name of the image using grep.
  • Assuming each day one image will be there. So make the image file name as ddmmyyyy.jpg
  • If the image not already downloaded, download the image using curl.

Full updated source code can be downloaded from https://github.com/sonu041/PhotoGrabber

My script to download the image is ready. Now I need to run the script everyday. I am not sure that in which hour of the day I will open the computer, so I run the script every hour. I used crontab for this.
In shell execute “crontab -e” to edit the crontab.
Schedule it for every hour

00 */1 * * * sh /Users/shuvankar/Desktop/Shuvankar/PhotoGrabber/ngphoto.sh

If you are using window, you can use windows schedular to schedule the script to run in every hour.

I automatically downloaded new image every day. Now there are several ways to automatically set the image from a folder. I wanted to change the background picture in every 15 min. As my requirementI used the simple default feature of mac to set desktop background from my downloaded folder.

Go to System Preference -> Desktop & Screen Saver -> Click on + and Select the folder -> Tick Change Picture in every hour -> Tick Random Order -> close.

You can do the same for screensaver as well.

Youtube video explained in Bengali.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.