REFRESH YOUR MAC DESKTOP WITH BING HOMEPAGE

Everyone would agree that Bing Homepage Wallpaper are cool and refreshing daily. If you are on Mac you would be little jealous with Windows 8 desktop Wallpaper, which are refreshed on daily basis. Well, after looking around, I got the solution which refreshes your Mac desktop with Bing Daily Homepage.

1. Create a shell script file, bing-wallpaper.sh (you can rename it whatever you want):

#!/usr/bin/env bash

PICTURE_DIR="$HOME/Pictures/Bing-Wallpapers"

mkdir -p $PICTURE_DIR

# Picks
# urls=( $(curl -s http://www.bing.com | \
#     grep -Eo "url:'.*?'" | \
#     sed -e "s/url:'\([^']*\)'.*/http:\/\/bing.com\1/" | \
#     sed -e "s/\\\//g") )

urls=( $(curl -s http://www.bing.com | \
    grep -Eo "g_img={url: \".*?\"" | \
	sed -e "s/g_img={url: \"//g" | \
	sed -e "s/\"//g" | \
	sed -e "s/\\\u0026/\&/g" ) )

if [ "$urls" = "" ]
then	
	urls=( $(curl -s http://www.bing.com | \
    	grep -Eo "g_img={url: \".*?\"" | \
		sed -e "s/g_img={url: \"\([^\"]*\)\".*/http:\/\/bing.com\1/" | \
		sed -e "s/\\\//g" | \
		sed -e "s/\\\u0026/\&/g" ) )
fi

if [ "$urls" != "" ]
then
	
	if [[ $urls = //* ]]
	then
		urls="http:$urls"
	elif [[ $urls != http* ]]
	then
		urls="http://www.bing.com$urls"
	fi

	rm -Rf $PICTURE_DIR/*.*

#	Use this for today's Bing Wallpaper	
	filename=$(echo $urls|sed -e "s/.*\/\(.*\)/\1/")
	# echo $urls
	# exit 0
	curl -Lo "$PICTURE_DIR/$filename" $urls
		
#	Use this for today's and next day's Bing Wallpaper 
#		
#	for p in ${urls[@]}; do
#    	filename=$(echo $p|sed -e "s/.*\/\(.*\)/\1/")
#    	if [ ! -f $PICTURE_DIR/$filename ]; then
#        	echo "Downloading: $filename ..."
#        	curl -Lo "$PICTURE_DIR/$filename" $p
#    	else
#        	echo "Skipping: $filename ..."
#    	fi
#	done

	killall Dock
	exit 0
fi

exit 99

2. Provide execute rights on this file

chmod +x /Applications/Custom\ Apps/bing-wallpaper.sh

3. Create launchd scheduled job (com.yourname.bing-wallpaper.plist file) to run it every morning 7 AM and execute our shell script (create in Step #1)

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>com.yourname.bing-wallpaper</string>
   <key>ProgramArguments</key>
   <array>
      <string>/bin/bash</string>
      <string>/Applications/Custom Apps/bing-wallpaper.sh</string>
   </array>
   <key>LowPriorityIO</key>
   <true/>
   <key>Nice</key>
   <integer>1</integer>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <false/>
    </dict>
    <key>RunAtLoad</key>
    <true/>
    <key>StartCalendarInterval</key>
    <dict>
      <key>Hour</key>
      <integer>7</integer>
      <key>Minute</key>
      <integer>0</integer>
    </dict>  
</dict>
</plist>

4. Copy this file to $HOME/Library/LaunchAgents folder

cp /folder/location/of/com.yourname.bing-wallpaper.plist $HOME/Library/LaunchAgents

5. Generally launchd load these jobs on login but you can load it manually

launchctl load $HOME/Library/LaunchAgents/com.yourname.bing-wallpaper.plist

If required, you can disable this job using following command –

launchctl unload $HOME/Library/LaunchAgents/com.yourname.bing-wallpaper.plist

You can find the status of job using the following command –

launchctl list | grep com.yourname

3 thoughts on “REFRESH YOUR MAC DESKTOP WITH BING HOMEPAGE”

  1. Update:

    Change in bing-wallpaper.sh file, to set global version of image:

    urls=( $(curl -s http://www.bing.com | \
    grep -Eo “g_img={url: \”.*?\”” | \
    sed -e “s/g_img={url: \”\([^\”]*\)\”.*/http:\/\/bing.com\1/” | \
    sed -e “s/\\\//g”) )

Leave a Reply