Last active
April 6, 2022 02:35
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
Add it to crontab and run it every hour, to update your background every day.