Hyper Expert /Blog
About
Archives

Shell script to download Bing’s daily image as a wallpaper for Mac

I previously wrote about downloading Bing’s daily image and set it as your wallpaper using PowerShell for Windows. Or, Python for Mac. This time around, I will show you how to do a Shell script to achieve the same thing. Also, set the downloaded image as wallpaper immediately.

I have already made the following script an app using Mac Automator. It basically creates a new directory for you, if one does not exist, under your “Pictures” directory called “Bing Wallpaper” and download Bing’s daily image there as “wallpaper.jpg”. Then, it will set the downloaded image as your wallpaper – Also works on multi-monitor.

You can save the app in your Applications folder or anywhere you want.
Download app

Or, if you want to edit the script, download the source below and make your own app.
Download Script

#!/usr/bin/env bash
#Market options: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ
#Resolution options: 1366×768, 1920×1080, 1920×1200
Market="en-US"
Resolution="1920x1080"
Directory="$HOME/Pictures/Bing Wallpaper/"
FileName="wallpaper.jpg"

while ! ping -c 1 bing.com > /dev/null 2>&1; do 
	echo -e "\x1B[93m Waiting for internet connectivity to continue... \x1b[0m" 
	sleep 10; 
done; 

mkdir -pv "$Directory"

URL=($(curl -s 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt='"$Market" | sed -e 's/^.*"urlbase":"\([^"]*\)".*$/\1/'))
ImageURL="http://www.bing.com"/$URL"_"$Resolution".jpg"

if [ -f "$Directory/$FileName" ]; then
	fileDate=($(stat -f "%Sm" -t "%Y%m%d" "$Directory"/$FileName))
	todayDate=($(date +"%Y%m%d"))
	if [ $todayDate = $fileDate ]; then
		echo -e "\x1B[32m You already have today's Bing image \x1b[0m"
	else
		echo -e "\x1B[32m Downloading Bing image to: $Directory \x1b[0m"
		curl -so "$Directory/$FileName" "$ImageURL"
	fi
else
	echo -e "\x1B[32m Downloading Bing image to: $Directory \x1b[0m"
	curl -so "$Directory/$FileName" "$ImageURL"
fi
while [ ! -f "$Directory/$FileName" ] ; do 
	echo -e "\x1B[93m Waiting for Bing image to finish downloading... \x1b[0m" 
	sleep 10; 
done;
osascript -e "tell application \"System Events\" to set picture of every desktop to \"/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png\"" 
osascript -e "tell application \"System Events\" to set picture of every desktop to \"$Directory/$FileName\""

The last 2 lines change your wallpaper twice. First, the script changes your wallpaper to a single color from the built-in OS X color library. Then, it changes your wallpaper again to the downloaded Bing image. It happens so quick you will barely notice the change. The reason for that is, when you already have a Bing image set as your wallpaper and you download a new image to overwrite it and set it as your new wallpaper, OS X does not apply the change because it can’t find the old wallpaper file anymore. This way, we tell the OS to set the wallpaper to a different always existing file, then back to our newly downloaded wallpaper file to avoid any hang ups.

Simple download only script

If you want a very simple way to just download the latest Bing image, the following script will do the job.

#!/usr/bin/env bash
#Market options: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ
#Resolution options: 1366×768, 1920×1080, 1920×1200
Market="en-US"
Resolution="1920x1080"
Directory="$HOME/Pictures/Bing Wallpaper/"
FileName="wallpaper.jpg"

mkdir -pv "$Directory"

URL=($(curl -s 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt='"$Market" | sed -e 's/^.*"urlbase":"\([^"]*\)".*$/\1/'))
ImageURL="http://www.bing.com"/$URL"_"$Resolution".jpg"
echo "Downloading Bing image to: $Directory"
curl -so "$Directory"/$FileName "$ImageURL"

Adjust the first 4 lines (after the comments) to your preferences and the script will download the latest Bing image for you.

The above two scripts use “sed” to filter out the image URL from Bing’s page. There is another “cleaner” way to accomplish the same thing by including Python instead like the script below:

#!/usr/bin/env bash
#Market options: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ
#Resolution options: 1366×768, 1920×1080, 1920×1200
Market="en-US"
Resolution="1920x1080"
Directory="$HOME/Pictures/Bing Wallpaper/"
FileName="wallpaper.jpg"

mkdir -pv "$Directory"

URL=($(curl -s 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt='"$Market" | python -c "import json,sys;obj=json.load(sys.stdin);print obj['images'][0]['urlbase'];"))
ImageURL="http://www.bing.com"/$URL"_"$Resolution".jpg"
echo "Downloading Bing image to: $Directory"
curl -so "$Directory"/$FileName "$ImageURL"

The only differences between the first script posted above and the two simple ones is that I added some logic to the first script. The first script checks if there is internet connectivity before continuing. It also checks if you already have the latest image before downloading a new one. And finally, it waits for the image to finish downloading before trying to set it as your wallpaper.

You pick whichever one you like to use. They all accomplish the same thing.

If you want to know how to make the script an app with automator, I have already posted a how-to with screenshots over here: Python script to download Bing’s daily image for Mac

///////

Hi, my name is Ali. I am a Systems Engineer. I live in the beautiful Evergreen state. I have a Bachelor degree of science in Information Technology with a handful of accompanying I.T. certificates. I also have a degree in Computer Networking. I am an Apple Certified Technician, Microsoft MCITP and a Linux expert.

Would you like to share your thoughts?