feiqihang

背景

  本人一直非常喜欢Bing(必应)的壁纸,之前写过Java程序在Windows系统中自动下载并切换桌面壁纸。现在尝试在Mac OS X下实现,心想既然有shell这么好用的脚本语言,何乐不为啊。一起享受养眼的桌面吧。

实现

  直接上代码,其中osascript命令是Mac OS专有的。

#!/bin/sh
#提取壁纸图片URL(支持重定向)
url=$(expr "$(curl -L -e \'; auto\' https://www.bing.com/?mkt=zh-CN |grep g_img=)" : ".*g_img={url:\"\(.*\)\"};.*")
#去除url中的斜杠“\”
url="http://www.bing.com${url//\\/}"
#替换&符号
url=${url/\u0026/&}
echo $url
#提取图片名称
filename=$(expr "$url" : ".*id=\(.*\)&rf=.*")
#本地图片地址-当前用户下缺省图片目录
localpath="/Users/$USER/Pictures/$filename"
#下载图片至本地
curl -o $localpath $url
#调用Finder应用切换桌面壁纸
osascript -e "tell application \"Finder\" to set desktop picture to POSIX file \"$localpath\""

 

  看,Bing首页很漂亮吧。 

分类:

技术点:

相关文章:

  • 2021-09-14
  • 2021-12-09
  • 2020-04-11
  • 2021-05-31
  • 2021-06-25
  • 2021-12-09
  • 2021-06-02
  • 2021-12-04
猜你喜欢
  • 2022-12-23
  • 2021-12-01
  • 2021-12-29
  • 2022-12-23
  • 2021-10-21
  • 2021-11-05
  • 2021-08-21
相关资源
相似解决方案