- 下载图片放到任意文件夹,如:/home/<yourname>/图片。
- 在任意位置创建一个script.run文件,内容为(600指每600秒切换壁纸):
dir="/home/<yourname>/图片" files=`ls -u ${dir} | grep -v contest` last_file='empty' echo '<background>' echo ' <starttime>' echo ' <year>2019</year>' echo ' <month>3</month>' echo ' <day>5</day>' echo ' <hour>00</hour>' echo ' <minute>00</minute>' echo ' <second>00</second>' echo ' </starttime>' for current_file in $files do if [ "$last_file" = "empty" ]; then last_file=$current_file first_file=$last_file echo ' <static>' echo ' <duration>600.0</duration>' echo " <file>${dir}/$last_file</file>" echo ' </static>' else echo ' <transition>' echo ' <duration>3.0</duration>' echo " <from>${dir}/$last_file</from>" echo " <to>${dir}/$current_file</to>" echo ' </transition>' echo ' <static>' echo ' <duration>600.0</duration>' echo " <file>${dir}/$current_file</file>" echo ' </static>' last_file=$current_file fi done echo ' <transition>' echo ' <duration>3.0</duration>' echo " <from>${dir}/$last_file</from>" echo " <to>${dir}/$first_file</to>" echo ' </transition>' echo '</background>'
- 运行脚本生成mywallpapers.xml:sh script.sh > mywallpapers.xml。
- 把mywallpapers.xml移动到/usr/share/backgrounds/contest。
- 向/usr/share/gnome-background-properties/xenial-wallpapers.xml添加内容:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE wallpapers SYSTEM "gnome-wp-list.dtd"> <wallpapers> <wallpaper deleted="false"> <name>Ubuntu 16.04 Community Wallpapers</name> <filename>/usr/share/backgrounds/contest/xenial.xml</filename> <options>zoom</options> </wallpaper> <!--添加开始--> <wallpaper deleted="false"> <name>My Wallpapers</name> <filename>/usr/share/backgrounds/contest/mywallpapers.xml</filename> <options>zoom</options> </wallpaper> <!--添加结束--> ...
- 到系统设置里设置壁纸。
- 以后使用新图片做壁纸需要重新执行<3>、<4>步。
用脚本生成mywallpapers.xml后,壁纸按照固定的顺序切换,添加新图片时还要重新生成。为了灵活,下面使用程序切换。
- 图片路径不含中文:
#命令行执行 import os import random str1 = "export GIO_EXTRA_MODULES=/usr/lib/x86_64-linux-gnu/gio/modules/ && " str2 = "gsettings set org.gnome.desktop.background picture-uri " str3 = "/home/<yourname>/a/" imgs = os.listdir(str3) aimg = str3 + str(random.choice(imgs))[0:] cmda = str1 + str2 + "\"file:" + aimg +"\"" os.system(cmda) #crontab执行 import os import random pre1 = "PID=$(pgrep gnome-session) && " pre2 = "export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) && " str1 = "gsettings set org.gnome.desktop.background picture-uri " str2 = "/home/<yourname>/a/" imgs = os.listdir(str2) aimg = str2 + str(random.choice(imgs))[0:] cmda = pre1 + pre2 + str1 + "\"file:" + aimg +"\"" os.system(cmda)
- 图片路径含中文:
#命令行执行 #encoding=utf-8 import os import random str1 = "export GIO_EXTRA_MODULES=/usr/lib/x86_64-linux-gnu/gio/modules/ && " str2 = "gsettings set org.gnome.desktop.background picture-uri " str3 = unicode("/home/<yourname>/图片/","utf-8") imgs = os.listdir(str3) aimg = str3 + str(random.choice(imgs))[0:] cmda = str1 + str2 + "\"file:" + aimg +"\"" os.system(cmda.encode('utf-8')) #crontab执行 #encoding=utf-8 import os import random pre1 = "PID=$(pgrep gnome-session) && " pre2 = "export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) && " str1 = "gsettings set org.gnome.desktop.background picture-uri " str2 = unicode("/home/<yourname>/图片/","utf-8") imgs = os.listdir(str2) aimg = str2 + str(random.choice(imgs))[0:] cmda = pre1 + pre2 + str1 + "\"file:" + aimg +"\"" os.system(cmda.encode('utf-8'))
- 注意只要“crontab执行”部分。假设上面创建的文件为/home/<yourname>/wallpaper.py。执行命令:crontab -e,追加:*/5 * * * * python /home/<yourname>/wallpaper.py,ctrl + X保存,Y确认,Enter关闭。执行命令crontab -l查看。其中,5指每隔5分钟换壁纸,选择填充。
- 用于中文路径并且通知提示的python文件:
#encoding=utf-8 import os import random pre1 = "PID=$(pgrep gnome-session) && " pre2 = "export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) && " str1 = "gsettings set org.gnome.desktop.background picture-uri " str2 = unicode("/home/<yourname>/图片/","utf-8") imgs = os.listdir(str2) aimg = str2 + str(random.choice(imgs))[0:] cmda = pre1 + pre2 + str1 + "\"file:" + aimg +"\"" os.system(cmda.encode('utf-8')) mess = "notify-send -i /home/<yourname>/文档/caffe.png \"现在时间\" \"$(date +%m月%d日\' \'%p%I:%M)\"" os.system(pre1+pre2+mess)
[参考1][crontab定时任务]