Tuesday, August 9, 2011

Changing the Desktop, Login and Lock Screen Backgrounds in Gnome using a Simple GUI

We are happy to present you with a script capable of invoking a simple GUI which lets you easily change your Desktop, Login Screen and Lock Screen backgrounds, the last two of which are not that easily changeable. The script is solely our own developed one. It was my idea initially and Krytarik developed a very nice GUI around it and also improved the script in many aspects.

This post might seem to be lengthy at the first sight but actually all you need to do is copy&paste from here to your Terminal/Text Editor. It shouldn't take more than 5 minutes to implement and once implemented, hopefully it would serve you nice and also it would save you some time when you get fed up with your backgrounds and want to change them again.


First of all, let's take a look at what you get with this script:
  • A simple file navigation dialog for selecting your desired image.
  • Options to change any one or all three of Desktop, Login Screen and Lock Screen backgrounds.
  • Options to scale your image according to your resolution.
Note: This script doesn't work with Oneiric 11.10 and onwards, for multiple reasons. We are working on adapting it, so please check back later! (11/06/2011)

Creating the Script

1. Go to a Terminal and run:

gksudo gedit /usr/local/bin/background-image

2. In the text file that opens up, paste this text:

#!/bin/bash
#
# GUI to change the Desktop, Login Screen and Lock Screen Backgrounds in Gnome
#
# Depends on zenity
#
# Authors: Sikander Hayat Khan (idea) / Krytarik (GUI)
#

zenity --title="Choose Background Image" --info --text="Choose your desired background image in the following dialog."
IMAGE=$(zenity --title="Choose Background Image" --file-selection)

if [[ `file -i "$IMAGE"` == *image* ]]; then
    SCALE=$(zenity --title="Choose Scaling Option" --height=270 --list --text="Choose a scaling option for your background image:" --radiolist --hide-column=2 --column="" --column="" --column="" FALSE wallpaper Tile FALSE zoom Zoom FALSE centered Center TRUE scaled Scale FALSE stretched Stretch FALSE spanned Span)

if [ -z "$SCALE" ]; then
    zenity --title="Information" --info --text="You didn't choose a scaling option, the default, 'Scale', will be set."
    SCALE="scaled"
fi

APPLY=$(zenity --title="Apply to what?" --height=200 --list --text="To which backgrounds shall the image be applied?" --checklist --multiple --column="" --column="" TRUE Desktop TRUE 'Login Screen' TRUE 'Lock Screen')

if [ -n "$APPLY" ]; then

    if [[ $APPLY == *Desktop* ]]; then
        gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$IMAGE"
        gconftool-2 --type string --set /desktop/gnome/background/picture_options $SCALE
    fi

    if [[ $APPLY == *Login\ Screen* ]]; then
        gksudo "gconftool-2 --config-source=xml::/var/lib/gdm/.gconf --type string --set /desktop/gnome/background/picture_filename '$IMAGE'"
        gksudo "gconftool-2 --config-source=xml::/var/lib/gdm/.gconf --type string --set /desktop/gnome/background/picture_options $SCALE"
        gksudo "chmod 644 '$IMAGE'"
    fi

    if [[ $APPLY == *Lock\ Screen* ]]; then
        gksudo "gconftool-2 --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults --type string --set /desktop/gnome/background/picture_filename '$IMAGE'"
        gksudo "gconftool-2 --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults --type string --set /desktop/gnome/background/picture_options $SCALE"
        gksudo "chmod 644 '$IMAGE'"
    fi

    zenity --title="Confirmation" --info --text="$IMAGE has been set as your $APPLY.\n\nNote: You need to relogin for the Lock Screen Background to be applied."

else
    zenity --title="Failed!" --error --text="You didn't choose any option, nothing has been changed."
fi

else
    zenity --title="Failed!" --error --text="You didn't choose a valid image, nothing has been changed."
fi

3. Save and close this file.

4. Now make the script file executable by running:

sudo chmod +x /usr/local/bin/background-image

Creating the .desktop file for the Launcher

1. Go to a Terminal and run:

gksudo gedit /usr/share/applications/background-image.desktop

2. In the file that opens up, paste this text:

[Desktop Entry]
Name=Background Image
Comment=GUI to change the Desktop, Login Screen and Lock Screen Backgrounds in Gnome
Exec=background-image
Icon=background
Terminal=false
Type=Application
Categories=GNOME;GTK;Settings;DesktopSettings;
OnlyShowIn=GNOME;Unity;

3. Save and close this file.

4. Update the menu item cache (otherwise done by dpkg during a usual installation/upgrade):

sudo rm /usr/share/applications/desktop.*.cache
sudo sh -c "/usr/share/gnome-menus/update-gnome-menus-cache /usr/share/applications/ > /usr/share/applications/desktop.${LANG}.cache"

If you want to change additional aspects of the Login Screen, please see here:

http://www.tuxgarage.com/2011/05/customize-gdm-plymouth-grub2.html

Usage

Now you can find 'Background Image' under System > Preferences menu in classic Gnome or by searching the Dash in Unity.

Furthermore, the screenshots below are self-explanatory.






Related Posts:

Appearance , GDM