3

为 MacOS 随机定时设定 Bing 壁纸

 1 year ago
source link: https://blog.kelu.org/tech/2023/05/04/change-background-with-bing-wallpaper-in-macos-by-script.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

为 MacOS 随机定时设定 Bing 壁纸

2023-05-04     tech mac
mac.jpg

这是一个 github 上的项目,挺简单实用的,用命令行下载 bing 的壁纸替换为 Mac 的壁纸。虽然不够优雅但胜在简单,记录过来。出处: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

保存为一个文件(假设为 w.sh),chmod +x w.sh

运行如下脚本(设置为定时任务,每5分钟1 次):

#!/usr/bin/env bash

cFolder="$(pwd)"
echo "*/5 * * * * $cFolder/w.sh  > /dev/null 2>&1" | crontab

如果本文对你有帮助,欢迎投食

alipay.gif
qpay.gif
圣杯の寄るべに従い。この意、この理に従うならば応えよ!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK