7

I wanted to make a bash script that takes a random picture from a folder and sets it as the desktop background. I figured out the algorithm (it's not hard) but I can't find a command that would change the background picture if you give it the path to the picture.

I found gconftool for bash 2.~ and gsettings for bash 3.~, but i have GNU bash, version 4.3.42(1)-release on Ubuntu 16.04 LTS and these don't work.

Zanna
  • 70,465
  • What command did you use ? you can set a bg image with several tools from feh to nitorgen - – Panther Jun 06 '16 at 16:01
  • gconftool and gsettings aren't dependent on the version of bash, and nobody uses anything older than bash 4 on any current Ubuntu system. – muru Jun 06 '16 at 16:02

2 Answers2

11

Assuming you're using standard Ubuntu 16.04 with Unity, you can use the following command to set the wallpaper:

gsettings set org.gnome.desktop.background picture-uri "file:///home/username/path/to/image.jpg"

You should also look at Variety wallpaper changer - it does the same thing as what you want to do with a shell script, with a lot of features and options.

Jonas Czech
  • 4,017
1

I've done it like this:

targetDir="/home/username/Photos"


function get_next_photo() {
    # Returns a random file form targetdir
    files=( "$targetDir"/* )
    echo "${files[RANDOM % ${#files[@]}]}"
}

function set_background() {
    # Takes an absolute file path as argument. Need * for spaces in path
    bg="$*"
    echo "Setting background to $bg"
    gsettings set org.gnome.desktop.background picture-uri "file://$bg"
}


background=$(get_next_photo)
echo "Next background is $background"
set_background $background

Then add a quick launcher for your script to the dock, and you can change your wallpaper in one click.

MathKid
  • 131