2

What file controls the image displayed on the desktop background. And how might i go about editing it so that i can change the background by pressing a key on my keyboard (is a way to do that already?). I have Ubuntu 16.04.

  • 2
    As a reputation 1 user, please consider marking any one of the below answers that you find as the most satisfactory solution to your question by clicking the grey checkmark below up and down arrows of each post to mark the one as accepted. You can also leave the posts unmarked if you find that none of the posts provide a satisfactory solution. – Sergiy Kolodyazhnyy Dec 09 '16 at 07:39

2 Answers2

4

Simple way to do so is via gsettings command paired together with zenity --file-selection which allows you to choose a file. There are more advanced ways to do it via python and python-gi API, but a simple and short shell script is sufficient.

Usage

Save this script in your ~/bin directory ( if you don't have the bin folder in your home folder, then create one). Next open System Settings -> Keyboard -> Shortcuts -> Custom and click the + button to create new shortcut. Make sure you give full path to the script as command, like /home/john_doe/bin/change_background.sh and make sure to assign a key to this shortcut by clicking on the right field

Script source

Please ensure that the script is made executable with chmod +x ~/bin/change_background.sh command in terminal or via righ-click -> Properties menu in the GUI file manager

#!/bin/bash

file_path=$(zenity --file-selection)

if [ "x$file_path" != "x"  ];
then
    gsettings set org.gnome.desktop.background picture-uri file://"$file_path"
fi

Additional info

You've asked:

What file controls the image displayed on the desktop background

Technically speaking, there is no single file specifically for background settings. There is dconf database for user settings ( which is typically stored in your ~/.config/dconf/user file ). This is binary data , so you can't simply open it in text editor and write the name of file you want to be displayed as your background. What you can do is use either dconf or gsettings command. dconf operates on that database directly, while gsettings does a few "sanity checks" for the data you put in, so it's is very common to see gsettings being used although in my experience there is no advantage from using one over the other, and in fact I personally prefer using dconf. In either case, these two tools are what I would consider are power-user tools that do the same thing.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
1

You can use the gsettings set org.gnome.desktop.background picture-uri command. Put the file:///[location of image] as the parameter.

For example:

 $ gsettings set org.gnome.desktop.background picture-uri "file:///$HOME/Pictures/myimage.png"

You can also use the gsettings get to get the current background image. This will show you the current background image.

 $ gsettings get org.gnome.desktop.background picture-uri

will show:

 file:///home/users/l/j/ljames/Pictures/backgrounds/myimage.png

Update:

The system stores the background image in a binary file located at:

$HOME/.config/dconf/user

A script to show the current background would have this content:

#!/bin/bash

current_background=$(gsettings get org.gnome.desktop.background picture-uri)) echo $current_background

When you run the script it'll show you the current pathname of the current background.

You can store your file anywhere and put the pathname in a location where your script will read the location into a variable and use that.

In this example use a text editor to put the file's pathname in a file called background.

Create the script and save it:

$ gedit ~/bin/setbackground.sh
$ chmod +x ~/bin/setbackground.sh

This is the content for the script:

#!/bin/bash

background=$(cat ~/background) gsettings set org.gnome.desktop.background picture-uri "file:///$background"

Edit the file to contain the background image to use.

$ gedit $HOME/background

Put the path name /home/yourID/Pictures/myimage.png and save the file.

Run and test the script from the commandline:

$ ~/bin/setbackground.sh

(note: after relogging in you can just type setbackgrund.sh without the path to run the script from the commandline).

Set the HotKey

Now you can use the Ubuntu's keyboard settings to set the hotkey. Go to:

System Settings -> Keyboard -> Shortcuts -> Custom Shortcuts -> (click) + ->
(Type in Name and Command) -> (click) Apply

Place this in the Custom Shortcut window:

Name:       Set Background
Command:    /home/userID/setbackground.sh

For the keyboard shortcut you can put:

shift+ctrl+b

Now anytime you want to change your background, jut put the pathname in your $HOME/background file, then hit your keyboard hotkey to change it to that.

L. D. James
  • 25,036