10

您能否编写最简单的 shell 脚本,以定期(例如 1 分钟)更改桌面壁纸(在 Ubuntu 中)。

壁纸将保存在特定目录中(例如 $HOME/wallpapers)。我只需要基本功能。

$HOME/wallpapers
1)从2 中选择随机壁纸)将其设置为桌面上的壁纸
3)设置 cron 以每分钟运行一次脚本(不是问题的一部分)。

4

6 回答 6

13
#!/bin/bash
wallpaperdir='$HOME/wallpaper'

files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`

gconftool-2 -t str --set /desktop/gnome/background/picture_filename "$randompic"

保存此脚本并使用命令“crontab -e”编辑您的脚本(它会启动一个编辑器,您将此行放在文件末尾):

*/1     *     *     *     *         /bin/bash /path/to/script.sh

编辑:我假设你正在使用 gnome。如果不是,您需要编辑最后一行,因为我的示例使用 Gnome Conftool。;)

要更改 XFCE 中的背景,您应该使用 gconftool-2 将行更改为:

echo -e “# xfce backdrop list\n$randompic”>$HOME/.config/xfce4/desktop/backdrops.list    
killall -USR1 xfdesktop
于 2011-04-05T11:29:16.227 回答
4

我知道这个答案有点晚了,但因为它可以帮助某些人,所以我发布了它。

从 septi 的代码加上一些修改,这是我的解决方案:

#!/bin/bash
wallpaperdir="$HOME/wallpaper"

files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`

echo -e "# xfce backdrop list\n$randompic">$HOME/.config/xfce4/desktop/backdrop.list
xfdesktop --reload

必须用双引号替换单引号,以便计算机正确解释 $HOME 部分。此外,您要编辑的文件是背景列表,而不是背景列表。最后,我发现在这种情况下使用 killall 有点过分,因为您可以简单地重新加载 xfdesktop。

我已经在我的计算机(Linux Mint Debian 版)上对其进行了测试,它似乎运行良好。

希望能帮助到你。=)

编辑:我忘了提到你必须在你的命令之前添加 DISPLAY=:0.0,在 crontab 中。这给了

*/1 * * * * DISPLAY=:0.0 wallpaper.sh
于 2012-08-20T21:23:52.367 回答
3

这只是我在这件事上的做法。我并不声称它是理想的。

WALLS_PATH=/path/to/images
cd $WALLS_PATH

while [ 1 ]; do
    for NEW_WALL in "$WALLS_PATH"/*; do
        gsettings set org.gnome.desktop.background picture-uri "file://${NEW_WALL}"
        sleep 1800
    done
done
于 2012-12-26T16:03:04.380 回答
2

在较新的 Ubuntu 中试试这个:(gsettings set org.gnome.desktop.background picture-uri file:///path/to/img.jpg来自这里的提示)

于 2012-04-05T21:25:30.400 回答
2

For gnome3 you need to use gsettings instead of gconftool.

But if you're going to execute the script throught cron it will not work.

I've tried a lot of .sh scripts but no one works for me.

At the end, i fixed it using this python script that loads a random wallpaper from a folder:

#!/usr/bin/env python
#coding: utf8 

import os,random
setup = "/path_to_folder/" + random.choice(os.listdir("/path_to_folder/"))
os.system("DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri  'file://%s'" %(setup))

Hope it helps for someone with my same problem!

于 2014-05-28T21:44:14.110 回答
1

This worked for me in Gnome:

#!/bin/bash

DIR="/home/user/Pictures/wallpapers"
PIC=$(find $DIR -type f -maxdepth 1 | shuf -n1)
gsettings set org.gnome.desktop.background picture-uri "file://$PIC"
于 2014-08-12T06:57:19.233 回答