每次登录时都可以自动更换桌面壁纸吗

每次登录时都可以自动更换桌面壁纸吗

我已经学会了如何通过终端更改背景,但我能让终端每次登录时都更改为不同的背景吗?

答案1

介绍

下面的脚本将目录作为参数(最好只包含图像),从其内容中选择随机项,并将该项目设置为壁纸。它旨在在用户登录时启动,尽管也可以单独使用。

设置和使用

首先,脚本必须存储在系统上的某个位置。最好将其放在您的目录中。如果您的主目录中~/bin没有目录,则只需创建一个。bin

接下来,确保脚本具有可执行权限。您可以使用任何一种方式,chmod +x ~/bin/set_random_wallpaper.py也可以通过右键单击文件并检查Allow executing file as program“属性”菜单的“权限”选项卡来以 GUI 方式执行此操作。

该脚本需要目录作为参数。最好提供完整路径。例如:

set_random_wallpaper.py /home/JohnDoe/Pictures/wallpapers/ 

如果您通过命令行执行此操作,则可以提供相对路径,Pictures/wallpapers/但是为了将其设置为在登录时自动运行,请使用完整路径。

要使文件在每次登录时运行,请打开启动应用程序并单击“添加”按钮。使用命令和文件夹的完整路径:

/home/JohnDoe/bin/set_random_wallpaper.py /home/JohnDoe/Pictures/wallpapers/ 

就是这样 !

脚本源

该脚本也可在GitHub

#!/usr/bin/env python3
"""
Author: Serg Kolo , <[email protected]>
Date: December, 21,2016
Purpose: Sets random wallpaper from a given directory
Written for: https://askubuntu.com/q/851705/295286
"""
from gi.repository import Gio
import os,sys,random

def gsettings_set(schema, path, key, value):
    """Set value of gsettings schema"""
    if path is None:
        gsettings = Gio.Settings.new(schema)
    else:
        gsettings = Gio.Settings.new_with_path(schema, path)
    if isinstance(value, list):
        return gsettings.set_strv(key, value)
    if isinstance(value, int):
        return gsettings.set_int(key, value)
    if isinstance(value,str): 
        return gsettings.set_string(key,value)

def error_and_exit(message):
    sys.stderr.write(message + "\n")
    sys.exit(1)

def select_random_uri(dir_path):

    selection = random.choice(os.listdir(dir_path))
    selection_path = os.path.join(dir_path,selection)
    while not os.path.isfile(selection_path):
        selection = random.choice(os.listdir(dir_path))
        selection_path = os.path.join(dir_path,selection)

    selection_uri = Gio.File.new_for_path(selection_path).get_uri()
    return selection_uri

def main():
    """ Program entry point"""
    if len(sys.argv) != 2:
       error_and_exit('>>> Script requires path to a directory as single argument')
    if not os.path.isdir(sys.argv[1]):
       error_and_exit('>>> Argument is not a directory')   
    img_dir = os.path.abspath(sys.argv[1])
    uri = select_random_uri(img_dir)
    try:
        gsettings_set('org.gnome.desktop.background',None,'picture-uri',uri)
    except Exception as ex:
       error_and_exit(ex.repr()) 

if __name__ == '__main__': main()

技术细节和操作理论

脚本本身的工作方式非常简单,但我用自己的一些函数对其进行了补充。主函数检查是否有参数,以及该参数是否是目录 - 否则退出。如果一切正常,我们继续获取目录的绝对路径,并将其提供给set_random_uri()函数。

您可能知道也可能不知道,壁纸设置在dconf数据库中,可以使用gsettings命令访问和更改。简单的命令行方式是

gsettings set org.gnome.desktop.background picture-uri file:///home/JohnDoe/Pictures/cool_wallpaper.jpg

部分file://...是文件的 URI(本质上是文件的编码路径,如果您的系统使用英语以外的语言环境,则非常有用)。首先,我们需要选择随机文件并获取其 URI。这很简单 - 我们用random.choice()从列表中选择并os.listdir()获取目录中的项目列表。如果我们的随机选择恰好是一个目录而不是一个文件怎么办?好吧,这就是 while 循环的作用select_random_uri。一旦我们对选择感到满意,我们就会得到它的

好吧,从那里开始,这几乎与gsettings set命令中发生的事情相同,但我使用自定义gsettings_set()函数,避免调用外部命令,并且它对其他项目很有用,例如启动器列表指示器

就这样!祝您编码愉快,并尽享 Ubuntu 的乐趣!

其他资源

相关内容