Ubuntu动态壁纸

  1. 下载图片放到任意文件夹,如:/home/<yourname>/图片。
  2. 在任意位置创建一个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>'

     

  3. 运行脚本生成mywallpapers.xml:sh script.sh > mywallpapers.xml。
  4. 把mywallpapers.xml移动到/usr/share/backgrounds/contest。
  5. 向/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>
     <!--添加结束-->
    ...

     

  6. 到系统设置里设置壁纸。
  7. 以后使用新图片做壁纸需要重新执行<3>、<4>步。

[参考1][参考2]


用脚本生成mywallpapers.xml后,壁纸按照固定的顺序切换,添加新图片时还要重新生成。为了灵活,下面使用程序切换。

  1. 图片路径不含中文:
    #命令行执行
    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)

     

  2. 图片路径含中文:
    #命令行执行
    #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'))

     

  3. 注意只要“crontab执行”部分。假设上面创建的文件为/home/<yourname>/wallpaper.py。执行命令:crontab -e,追加:*/5 * * * * python /home/<yourname>/wallpaper.py,ctrl + X保存,Y确认,Enter关闭。执行命令crontab -l查看。其中,5指每隔5分钟换壁纸,选择填充。
  4. 用于中文路径并且通知提示的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定时任务]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值