前兩天看了一篇文章《自己寫指令碼自動更換案頭》,覺得用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 = -1next_index = current_index + 1if next_index >= len(avail_wallpapers): next_index = 0# 設定下一張新壁紙os.system('gsettings set org.gnome.desktop.background picture-uri \'' + avail_wallpapers[next_index] + '\'')