前两天看了一篇文章《自己写脚本自动更换桌面》,觉得用Python实现起来应该更容易理解,于是就有了下面的dynamic-wallpaper.py脚本。
dynamic-wallpaper.py脚本的完整内容:
#!/usr/bin/python # -*- coding: utf-8 -*- import os, os.path, fnmatch, commands # 壁纸图片所在的目录 wallpaper_path = '/usr/share/backgrounds' # 搜索所有可用的壁纸 avail_wallpapers = [] for file in os.listdir(wallpaper_path): if not os.path.isfile(wallpaper_path + '/' + file): continue if not fnmatch.fnmatch(file, '*.jpg') and \ not fnmatch.fnmatch(file, '*.png'): continue avail_wallpapers.append('file://' + wallpaper_path + '/' + file) if len(avail_wallpapers) == 0: amonest@amonest-virtual-machine:~/python$ cat dynamic_wallpaper.py #!/usr/bin/python # -*- coding: utf-8 -*- import os, os.path, fnmatch, commands # 壁纸图片所在的目录 wallpaper_path = '/usr/share/backgrounds' # 搜索所有可用的壁纸 avail_wallpapers = [] for file in os.listdir(wallpaper_path): if not os.path.isfile(wallpaper_path + '/' + file): continue if not fnmatch.fnmatch(file, '*.jpg') and \ not fnmatch.fnmatch(file, '*.png'): continue avail_wallpapers.append('file://' + wallpaper_path + '/' + file) if len(avail_wallpapers) == 0: exit # 对所有可用的壁纸排序 avail_wallpapers.sort() # 获取当前使用的壁纸 current_wallpaper = commands.getoutput('gsettings get org.gnome.desktop.background picture-uri').strip('\'') # 计算下一张壁纸索引 try: current_index = avail_wallpapers.index(current_wallpaper) except: current_index = -1 next_index = current_index + 1 if next_index >= len(avail_wallpapers): next_index = 0 # 设置下一张新壁纸 os.system('gsettings set org.gnome.desktop.background picture-uri \'' + avail_wallpapers[next_index] + '\'')