Set Bing wallpaper for macOS random timer

Original link: https://blog.kelu.org/tech/2023/05/04/change-background-with-bing-wallpaper-in-macos-by-script.html

This is a project on github, very simple and practical, use the command line to download bing’s wallpaper and replace it with Mac’s wallpaper. Although it is not elegant, it is better than simplicity, record it. Source: https://github.com/xiqishow/bing_wallpaper

 #!/usr/bin/env bash #创建壁纸目录echo $ if [[ $1 = "-n" ]] then osascript -e "display notification \"开始更换壁纸\" with title \"定时换壁纸\" " fi mkdir -p ~/bing-wallpapers/ cd ~/bing-wallpapers/ #请求一个随机数(bing只能返回0到7) index_seed = " $( jot -r 1 0 7 ) " #请求bing服务获得最新一张壁纸(下载UHD壁纸) rurl_esult = " $( curl --location --request GET 'http://www.bing.com/HPImageArchive.aspx?idx=' $index_seed '&n=5&format=js' --header 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' | grep -o '"url":"[^"]*"' | sed -e 's/"url":"/https:\/\/cn.bing.com/' | sed -e 's/"//' | sed -e 's/1920x1080/UHD/g' ) " #下载壁纸rm -f wallpapler * .jpg #随机文件名,否则无法更换壁纸file_seed = " $( date +%s ) " curl $rurl_esult -o wallpapler_ $file_seed .jpg > /dev/null #设置壁纸路径localpath = "/Users/ $USER /bing-wallpapers/wallpapler_ $file_seed .jpg" #设置壁纸# osascript -e "tell application \"Finder\" to set desktop picture to POSIX file \"$localpath\"" osascript -e "tell application \" System Events \" to set picture of (reference to every desktop) to \" $localpath \" " #提示壁纸设置成功if [[ $1 = "-n" ]] then osascript -e "display notification \"壁纸更换成功\" with title \"定时换壁纸\" " fi

Save as a file (assuming w.sh), chmod +x w.sh ,

Run the following script (set as a scheduled task, once every 5 minutes):

 #!/usr/bin/env bash cFolder = " $( pwd ) " echo "*/5 * * * * $cFolder /w.sh > /dev/null 2>&1" | crontab

References

This article is transferred from: https://blog.kelu.org/tech/2023/05/04/change-background-with-bing-wallpaper-in-macos-by-script.html
This site is only for collection, and the copyright belongs to the original author.