这是在下小博客上的文章,原文见 Mac 自动下载切换壁纸。分享到这里供大家参考。
之前我还介绍过用脚本去下载国家地理的每日一图,今天就完善整理成一个小教程,让 Mac 自动去下载壁纸到指定文件夹,简单设置一下让 Mac 自动切换壁纸,给既定的生活添点变化。
国家地理和必应壁纸下载脚本
首先,选择一个文件夹,比如说 /Users/franklin/Pictures/Wallpapers/
,下面就指 Wallpapers
文件夹啦。在里面新建两个脚本:
-
ng.sh
:#!/bin/bash # change directory to where the script resides BASEDIR=$(dirname $0) cd $BASEDIR ############################################################################ # get data data=$(curl -s https://www.nationalgeographic.com/photography/photo-of-the-day/) if [ -n "$data" ] then # get img url imgurl=$(grep 'property="og:image"' <<< "$data") imgurl="$(cut -d"\"" -f4 <<< $imgurl)" # get image name from canonical link and date name=$(grep 'rel="canonical"' <<< "$data") name="$(cut -d"\"" -f4 <<< $name)" name="$(cut -d"/" -f8 <<< $name)" date=$(grep 'meta property="gsa_publish_date"' <<< "$data") date="$(cut -d"\"" -f4 <<< $date)" file="$date-$name.jpg" if [ -f "$file" ] then echo "$(date) - $file already exists" else curl "$imgurl" > $file echo "$(date) - image saved as $file" fi else echo "$(date) - connection failed" fi
-
bing.sh
:#!/bin/bash # change directory to where the script resides. BASEDIR=$(dirname $0) cd $BASEDIR bing="http://www.bing.com" # the idx parameter determines where to start from. 0 is the current day, xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-WW" # Valid options: "_1024x768" "_1280x720" "_1366x768" "_1920x1200" "_1920x1080" picRes="_1920x1080" # The file extension for the Bing pic picExt=".jpg" # the XML data retrieved from xmlURL, form the fully qualified # URL for the pic of the day, and store it in $picURL data=$(curl -s $xmlURL) if [ -n "$data" ] then picURL=$(cut -d '>' -f13 <<< "$data") picURL=$(cut -d '<' -f 1 <<< "$picURL") picURL=$bing$picURL$picRes$picExt date=$(cut -d '>' -f5 <<< "$data") date=$(cut -d '<' -f1 <<< "$date") name=$(cut -d '>' -f15 <<< "$data") name=$(cut -d '<' -f 1 <<< "$name") name=$(cut -d '(' -f 1 <<< "$name") len=${#name} file="$date - ${name:0:len-1}$picExt" if [ -f "$file" ] then echo "$(date) - '$file' already downloaded" else curl "$picURL" > "$file" echo "$(date) - image saved as $file" fi else echo "$(date) - connection failed" fi
这两个脚本分别负责下载国家地理和必应每天的壁纸,下载的壁纸文件存放在这个脚本所处的文件夹里面,也就是 Wallpapers
文件夹里。文件是按“日期-图片名”形式存储的,也方便管理。
这两个脚本是硬编的,大佬们见笑啦?。
再新建另一个脚本,就叫 wallpapers.sh
吧,用来调用上面这两个脚本:
#!/bin/bash
path=$0
BASEDIR=$(dirname "$path")
cd "$BASEDIR"
./bing.sh
./ng.sh
exit
今后如果要添加其他的壁纸源,照猫画虎添加进来就行。
然后,需要给这三个脚本赋予可执行的权限:
chmod u+x /Users/franklin/Pictures/Wallpapers/ng.sh
chmod u+x /Users/franklin/Pictures/Wallpapers/bing.sh
chmod u+x /Users/franklin/Pictures/Wallpapers/wallpapers.sh
launchctl 定时运行壁纸下载脚本
在 ~/Library/LaunchAgents/
里新建一个 plist 文件,例如 com.frankindev.wallpapers.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- give a unique Label -->
<key>Label</key>
<string>com.frankindev.wallpapers.plist</string>
<!-- path to the script -->
<key>ProgramArguments</key>
<array>
<string>/Users/franklin/Pictures/Wallpapers/wallpapers.sh</string>
</array>
<!-- set the interval time in seconds -->
<key>KeepAlive</key>
<true/>
<key>StartInterval</key>
<integer>7200</integer>
<!-- std output -->
<key>StandardOutPath</key>
<string>/Users/franklin/Pictures/Wallpapers/wallpapers.log</string>
<!-- std err output -->
<key>StandardErrorPath</key>
<string>/Users/franklin/Pictures/Wallpapers/wallpapers.err</string>
</dict>
</plist>
按实际情况更改里面的 Label
、文件路径和运行间隔。
这里设定的是每两个小时运行一次下载脚本。在上面的具体脚本里面其实设置了检查文件是否已经下载,这么做也就是图个安心,以防网络不稳定当天的壁纸没 get 到。
建好 plist 文件后,需要使用 Mac 的 launchctl
添加到系统的启动进程里:
launchctl load -w ~/Library/LaunchAgents/com.frankindev.wallpapers.plist
Okay,这样系统就会悄悄滴每两个小时去运行一次壁纸下载脚本了。如果想了解更多关于 launchctl
的资料,这边请。
需要注意,如需修改 plist 文件中的内容,需要先 unload
再 load
才会生效:
launchctl unload -w ~/Library/LaunchAgents/com.frankindev.wallpapers.plist
launchctl load -w ~/Library/LaunchAgents/com.frankindev.wallpapers.plist
使用 Linux 的同学当然也可以用 crontab 来替换 launchctl。
Mac 自定义壁纸设置
壁纸到手了,就只需在 Mac 系统设置里添加壁纸文件夹,以及简单的个性化设置一下。如下图左下角那个 +
添加自定义文件夹:
上图里面只有两张壁纸可供选择,以后会越来越多滴。
如有不满意的壁纸今后去文件夹里删除掉就好了,其他地方下载的壁纸也可以丢到这个文件夹里统一管理。
我其实是将这个 Wallpapers
文件夹放在 OneDrive 里面进行同步的,当使用同一个账户切换到 Windows 下时我照样可以使用这些壁纸设置桌面。当然,上面那些脚本文件路径需要相应调整一下。
以上,有什么建议、意见留言讨论哈!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。