Skip to content

Instantly share code, notes, and snippets.

@Reza-Rg
Last active April 6, 2022 02:35
Show Gist options
  • Save Reza-Rg/ffabff4baaef6e4eaefa3543767195e2 to your computer and use it in GitHub Desktop.
Save Reza-Rg/ffabff4baaef6e4eaefa3543767195e2 to your computer and use it in GitHub Desktop.
A shell script to set Bing Background as wallpaper automatically on both OSX and Linux (requires wget & jq)
#!/usr/bin/env bash
if [ "$(uname)" = "Darwin" ]; then
jq=/usr/local/bin/jq
wget=/usr/local/bin/wget
image_folder_path="/Users/$USER/Documents/Images/Bing/"
mkdir -p $image_folder_path
elif [ "$(expr substr $(uname -s) 1 5)" = "Linux" ]; then
jq=/usr/bin/jq
wget=/usr/bin/wget
image_folder_path="/home/$USER/Documents/Images/Bing/"
mkdir -p $image_folder_path
fi
#Get Bing Image JSON
url='http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'
json=$(curl $url)
#Parse Json to get image_address and hash using jq
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_copyright=$(echo "$json" | $jq '.images[0].copyright' | sed -e 's/^"//' -e 's/"$//')
#Image Path
image_file_address=$image_folder_path$image_name
#Check image existance
if [ -e "$image_file_address" ]; then
echo "File exists, Cancel progress."
exit
else
echo "File does not exist, Download it."
fi
#Download image
$wget -O $image_file_address $image_address
#set image as background
if [ "$(uname)" = "Darwin" ]; then
#Mac OS X
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$image_file_address'";
killall Dock;
osascript -e 'display notification "'"$image_copyright"'" with title "New background :)" sound name "Ping"'
elif [ "$(expr substr $(uname -s) 1 5)" = "Linux" ]; then
#GNU/Linux
gsettings set org.gnome.desktop.background picture-uri "file://$image_file_address"
notify-send "New background :)" "$image_copyright"
fi
@Reza-Rg
Copy link
Author

Reza-Rg commented Jun 9, 2016

Add it to crontab and run it every hour, to update your background every day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment