Skip to content

Instantly share code, notes, and snippets.

@SamanSh999
Forked from Reza-Rg/changebg.sh
Created January 18, 2017 10:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SamanSh999/3ee8cad2859a900b7e36e1cff4204005 to your computer and use it in GitHub Desktop.
Save SamanSh999/3ee8cad2859a900b7e36e1cff4204005 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment