Skip to content

Instantly share code, notes, and snippets.

@ahgood
Forked from SamanSh999/changebg.sh
Last active July 14, 2022 18:45
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 ahgood/f31ff0e7d6c673140fa8dbe8f2bb1e63 to your computer and use it in GitHub Desktop.
Save ahgood/f31ff0e7d6c673140fa8dbe8f2bb1e63 to your computer and use it in GitHub Desktop.
A shell script to set Bing Background as wallpaper automatically on macOS(2022) (requires wget & jq)
#!/usr/bin/env bash
#
# Credit: https://gist.github.com/SamanSh999/3ee8cad2859a900b7e36e1cff4204005
#
# Instruction(macOS only):
#
# 1. Save this script to your home folder
# 2. Grant permission to script: chmod +x ~/bing-daily-wallpaper.sh
# 3. Install homebrew if you don't have it: https://brew.sh/
# 4. Install jq: brew install jq
# 5. Install wget: brew install wget
# 6. Setup cron job: crontab -e
# Example, run every 15 mins: */15 * * * * (cd $HOME && ./bing-daily-wallpaper.sh)
# 7. Grant full disk access right to cron: Add file /usr/sbin/cron to System Preferences -> Security and Privacy -> Full Disk Access
jq=/usr/local/bin/jq
wget=/usr/local/bin/wget
# Create folder if folder does not exist
image_folder_path="/Users/$USER/Bing_Daily_Wallpaper/"
mkdir -p $image_folder_path
date=$(date '+%Y-%m-%d-')
# Exit if today's wallpaper already downloaded
if ls "${image_folder_path}${date}"* 1> /dev/null 2>&1; then
exit
fi
# Parse Bing daily wallpaper JSON file and prepare local file name
url='http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'
json=$(curl $url)
image_address="http://www.bing.com$(echo $json | $jq '.images[0].url' | sed -e 's/^"//' -e 's/"$//')"
image_name=$(echo "$json" | $jq '.images[0].hsh' | sed -e 's/^"//' -e 's/"$//')
image_name="${date}${image_name}.jpg"
image_file_address=$image_folder_path$image_name
# There is a case that the image updated yesterday but not been updated today
# So add this check before downloading image.
if ls "${image_folder_path}"*"${image_name}"* 1> /dev/null 2>&1; then
exit
fi
# Remove all existing downloaded image before downloading the new image
rm "${image_folder_path}"*
# Download wallpaper image file
$wget -O $image_file_address $image_address
# Update desktop picture
osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"${image_file_address}\" as POSIX file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment