Внимание

Этот блог переехал по адресу yktoo.com/ru/

Attention

This blog has moved to yktoo.com/en/

July 20, 2011

Скрипт для смены обоев Gnome/Unity ~ Gnome/Unity random wallpaper script

Внимание: этот скрипт не работает в GNOME 3 и Ubuntu 11.10; для этих систем нужно использовать новую версию скрипта.

Note: this script will not work in GNOME 3 and Ubuntu 11.10; for these systems you have to use the new version.

Этот bash-скрипт выбирает случайный файл изображения (.jpg, .jpeg или .png) из заданного каталога и устанавливает его в качестве обоев рабочего стола.

В данной задаче есть небольшая хитрость. Когда скрипт запускается из cron, переменная DBUS_SESSION_BUS_ADDRESS, необходимая gconftool-2, не установлена. Для её инициализации добавлен специальный фрагмент кода.

Код протестирован во всех версиях Ubuntu вплоть до 11.04 Natty Narwhal. Он работает как с Gnome, так и с Unity.

Всё, что нужно настроить в нижеприведённом скрипте, это путь к каталогу с изображениями dir_wallpapers.
This bash script picks up a random image file (.jpg, .jpeg or .png) from the predefined directory and sets it as wallpaper.

One of the tricky things was to make it work when started from cron. The problem was with the DBUS_SESSION_BUS_ADDRESS variable (required by gconftool-2) not set in that case, so there's an extra piece of code that takes care of this variable.

The code has been tested with all Ubuntu versions up to 11.04 Natty Narwhal. It works with both Gnome and Unity.

The only thing to setup in the below script is the path to image files in dir_wallpapers.
#!/bin/bash
#================================================================================
# Description
#   Script to set a random wallpaper in Gnome/Unity
# Synopsis
#   rotate-wallpaper
# Author
#   Dmitry Kann, http://yktoo.com/
# License
#   Public domain
#================================================================================
# Setup vars
dir_wallpapers="$HOME/Pictures/"  # Path to wallpaper directory

# Exports DBUS variable which is not set when called from cron session
export_variables() {
  user=$(whoami)
  pid=$(pgrep -u $user gnome-panel)
  for dbusenv in $pid; do
    DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//')
    data="DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
    eval "export $data"
  done
}

# Fill array with picture files (replace spaces with colons to fool the array)
files=( $(find "$dir_wallpapers" -type f \( -iname '*.jpeg' -o -iname '*.jpg' -o -iname '*.png' \) | sed s/' '/':'/g) )
# Get number of members in the array
N=${#files[@]}
# Randomize N
((N=RANDOM%N))
# Get the normal file name (change colons back to spaces)
file=${files[$N]//:/ }
# Change desktop wallpaper
export_variables
gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$file"