Skip to content

Instantly share code, notes, and snippets.

@ytomino
Last active October 8, 2019 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytomino/f1630902b637e6d4a715ac48cf3b9a12 to your computer and use it in GitHub Desktop.
Save ytomino/f1630902b637e6d4a715ac48cf3b9a12 to your computer and use it in GitHub Desktop.
Use "bing Wallpaper" on macOS
#!/bin/bash
# Thanks to https://ubuntuforums.org/showthread.php?t=2074098
[[ -z "$HOME" ]] && exit 1
[[ -z "$TMPDIR" ]] && exit 1
# $bing is needed to form the fully qualified URL for
# the Bing pic of the day
bing="www.bing.com"
# $xmlURL is needed to get the xml data from which
# the relative URL for the Bing pic of the day is extracted
#
# The mkt parameter determines which Bing market you would like to
# obtain your images from.
# Valid values are: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ, en-CA.
#
# The idx parameter determines where to start from. 0 is the current day,
# 1 the previous day, etc.
country="ja-JP"
xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=$country"
# $saveDir is used to set the location where Bing pics of the day
# are stored. $HOME holds the path of the current user's home directory
saveDir="$HOME/Downloads/Pictures/bing"
[[ -d "$saveDir" ]] || exit 1
tempFile="$TMPDIR/bing.jpg"
# The desired Bing picture resolution to download
# Valid options: "_1024x768" "_1280x720" "_1366x768" "_1920x1200" "_1920x1080"
# "_2560x1600"?
screenSize=$( system_profiler SPDisplaysDataType | grep Resolution | sed -E 's/^.* ([0-9]+) x ([0-9]+) .*$/\1x\2/' )
desiredPicRes="$screenSize"
# The file extension for the Bing pic
picExt=".jpg"
# Extract the relative URL of the Bing pic of the day from
# the XML data retrieved from xmlURL, form the fully qualified
# URL for the pic of the day, and store it in $picURL
# Form the URL for the desired pic resolution
desiredPicURL=$bing$(echo $(curl -s $xmlURL) | grep -E -o "<urlBase>(.*)</urlBase>" | cut -d ">" -f 2 | cut -d "<" -f 1)_$desiredPicRes$picExt
# Attempt to download the desired image resolution. If it doesn't
# exist then download the default image resolution
function test_size {
eval $( stat -s "$1" )
[[ $st_size -gt 4096 ]]
}
okURL=
curl -s -o "$tempFile" "$desiredPicURL" && test_size "$tempFile" && okURL="$desiredPicURL"
if [[ -z "$okURL" ]] ; then
# Form the URL for the default pic resolution
defaultPicURL=$bing$(echo $(curl -s $xmlURL) | grep -E -o "<url>(.*)</url>" | cut -d ">" -f 2 | cut -d "<" -f 1)
curl -s -o "$tempFile" "$defaultPicURL" && okURL="$defaultPicURL"
if [[ -z "$okURL" ]] ; then
echo "ERROR!!" > /dev/stderr
exit 1
fi
fi
if [[ "$saveDir/bing1.jpg" -nt "$saveDir/bing2.jpg" ]]; then
saveFile="$saveDir/bing2.jpg"
else
saveFile="$saveDir/bing1.jpg"
fi
mv "$tempFile" "$saveFile"
echo "$okURL"
echo "$saveFile"
osascript -e "tell application \"Finder\" to set desktop picture to POSIX file \"$saveFile\""
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>jp.halfmoon.panathenaia.bing-daily-wallpaper</string>
<key>Program</key>
<string>/Users/USERNAME/bin/bing-daily-wallpaper</string>
<key>KeepAlive</key>
<false/>
<key>StartInterval</key>
<integer>14400</integer>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment