GUI to Change Background Arctica Greeter (Login Screen) on Ubuntu-MATE 22.04

A GUI to change the login-screen background.
( bash script, using zenity )

#!/bin/bash

mybg="/usr/share/backgrounds/mybackground"
schemadir="/usr/share/glib-2.0/schemas"
configfile="$schemadir/30_ubuntu-mate.gschema.override"

tmpfile="$XDG_RUNTIME_DIR/wallpaper$$"
trap "rm $tmpfile &>/dev/null" INT TERM EXIT

if ! grep "background='$mybg'" "$configfile" &>/dev/null
then
	pkexec sed -i "s|background=.*|background='$mybg'|g" "$configfile"
	pkexec glib-compile-schemas "$schemadir/."
fi

eom "$mybg" &

sleep 1
cp "$(zenity --title LoginBackgroundSelector --file-selection )" "$tmpfile" || exit
pkexec cp "$tmpfile" "$mybg"

eom "$mybg"

It works perfectly but I am not completely satisfied with my use of 'pkexec' here. Any suggestions are welcome.

Ah zenity... I haven't run zenity a loooong time.

You can use sudo and zenity to ask for password.

zenity --password | sudo -S "command"

-S read from stdin

In a script something like this:

if [[ "$EUID" = 0 ]]; then
    echo "already root"
else
    sudo -k # make sure to ask for password on next sudo
    if zenity --password | sudo -S true; then
        echo "correct password"
    else
        echo "wrong password"
        exit 1
    fi
fi
# Do your sudo stuff here. Password will not be asked again due to caching.
sudo sed -i "s|background=.*|background='$mybg'|g" "$configfile"
sudo glib-compile-schemas "$schemadir/."

sudo -k # make sure to ask for password on next sudo

Also have a look at this in the manual

man sudo
.......
-E, --preserve-env
Indicates to the security policy that the user wishes to pre‐
serve their existing environment variables. The security
policy may return an error if the user does not have permis‐
sion to preserve the environment.

1 Like

Marvellous! Thanks ! Oh yes, that was exactly what I needed, you're awesome :smiley:

EDIT: improved code

#!/bin/bash

exec &>/dev/null
source "$HOME/.config/user-dirs.dirs"

text1="Login Background Changer"
text2="Select Background for Login"
mybg="/usr/share/backgrounds/mybackground"
schemadir="/usr/share/glib-2.0/schemas"
configfile="$schemadir/30_ubuntu-mate.gschema.override"

tmpfile="$XDG_RUNTIME_DIR/delete_me_$$"
cleanup()
{
	rm $tmpfile
	sudo -k
}
trap "cleanup" INT TERM EXIT

zenity --title="$text1" --password | sudo -S true || exit

if ! grep "background='$mybg'" "$configfile"
then
	sudo sed -i "s|background=.*|background='$mybg'|g" "$configfile"
	sudo glib-compile-schemas "$schemadir/."
fi

eom "$mybg" &

sleep 1
cp "$(zenity --title="$text2" --file-selection --filename="$XDG_PICTURES_DIR/*")" "$tmpfile" || exit
sudo cp "$tmpfile" "$mybg"

eom "$mybg"

P.S.:
b.t.w. are you the same misko that composed my favourite laptopbackground ?
(i've gratefully been using this for years now) :slight_smile:

1 Like

Oh, I'm famous now, ha, ha.
Such a good surprise today, I'm so glad you like the background.
My sister also has it on laptop for years.
Other people told me the kids usually ask why is the fox sleeping. :slightly_smiling_face:

1 Like

Oh, before I forget:
At anyone following this thread, the best solution (much better than my hack) is to revert from arctica-greeter to the old tried en tested (and working) slick-greeter.