GaoWhen高H温

Mac 自动获取 sentense.me 图片做为桌面背景

某日扫到这篇文章 自动获取 sentense.me 图片做为桌面背景。其实之前是介绍过一个挺不错的定时替换桌面的软件 desktopia的。不过这个软件是如此优秀,以至于得到了FGBFXW的认证。结果你懂的。

顶着被 @adamsxu 鉴定为闲得蛋疼的危险,开始折腾 Mac 下的使用。shell 脚本中获取图片的方法直接可用,就是后面设置为桌面的方法 Mac 还是不同的。

然后想到了 AppleScript,折腾了下搞定。不过当时用的 do script 方法,每次都要跳出一个 Terminal 窗口,比较不爽。

然后遭 @adamsxu 鄙视,说回去给我写一个。

第二天参照他的方法换为 do shell,结合之前的设置为壁纸方法。然后再添加到 crontab 中,设置为每 15 分钟更换一次,收工。

确保系统装有 wget、feh

Mac 下安装 wget 的话,需要有 C 编译器,我是从同事那里拷来的 XCode。
首先去 官网下载源程序,解压。然后 terminal 进入 wget 目录。

  	cd ~/wget
  	./configure
	make
	sudo make install

期间看 terminal 的提示操作就好了。

用到的脚本如下。getwallpaper.sh 和 wallpaper.scpt 在文件夹 wallpapers 中,位于 home 路径下。用得时候要修改下相应的路径。

* getwallpaper.sh

	#!/bin/sh
	/usr/local/bin/wget http://sentense.me/  -O - -q |\
	sed -n 's/.*"\([^"]*14400x900[^"]*\.png\)".*/http:\/\/sentense.me\1/p'|\
	/usr/local/bin/wget -i - -P wallpapers -nc -q

* wallpaper.scpt

	do shell script "/Users/gaowhen/wallpapers/./getwallpaper.sh"
	tell application "Finder"
		set theList to files of folder "wallpapers" of folder "gaowhen" of folder "Users" of startup disk
		set theNum to random number from 1 to (count theList) with seed (time of (current date))
		set desktop picture to item theNum of theList
	end tell

然后加入系统的定时任务中。terminal 中

	crontab -e

如果系统中已有 crontab 文件则打开,没有则创建一个新文件,并用 vi 打开。在其中输入

	*/15 * * * * osascript ~/wallpapers/wallpaper.scpt

保存退出(:wq),一样要注意路径。

crontab 语法

選項與參數:
-u  :只有 root 才能進行這個任務,亦即幫其他使用者建立/移除 crontab 工作排程;
-e  :編輯 crontab 的工作內容
-l  :查閱 crontab 的工作內容
-r  :移除所有的 crontab 的工作內容,若僅要移除一項,請用 -e 去編輯。

範例一:用 dmtsai 的身份在每天的 12:00 發信給自己
[dmtsai@www ~]$ crontab -e
# 此時會進入 vi 的編輯畫面讓您編輯工作!注意到,每項工作都是一行。
0   12  *  *  * mail dmtsai -s "at 12:00" < /home/dmtsai/.bashrc
#分 時 日 月 週 |<==============指令串========================>|

详情可以参考

如果不想折腾,或者嫌弃 sentense.me 的壁纸过于单调的话,还可以尝试下 desklickr。免费软件,自动从 Flickr
获取图片并设为桌面的,也挺不错。

3 Responses to Mac 自动获取 sentense.me 图片做为桌面背景

  1. cyril says:

    听说你们搞定了我就没掺和了, 原来你们还要两个脚本的阿…
    我这个似乎清爽一点, 执行后获取最新的一张图片作为墙纸

    #!/bin/sh
    . /users/cyril/.profile
    wget http://sentense.me/feed -O – -q |\
    sed -n ‘s/.*”\([^"]*1280×800[^"]*\.png\)”.*/http:\/\/sentense.me\1/p’|head -n1|\
    wget -i – -nc 2>&1|\
    sed -n ‘s/.*`\([^`]*1280×800[^`]*\.png\).*/\1/p’|head -n1
    wallpaper=`pwd`/`ls -lrt *.png|tail -n1|awk ‘{print $9}’`
    osascript <<EOF
    tell application "Finder"
    set desktop picture to POSIX file "$wallpaper"
    end tell
    EOF

  2. 糖伴西红柿 says:

    只使用最新的貌似不爽,还是随机使用有变化点,哈哈。

  3. Shawphy says:

    看来我也有望利用这玩意写出获取最新的Chromium了

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>