Skip to content

Instantly share code, notes, and snippets.

@nkmathew
Last active August 29, 2015 14:21
Show Gist options
  • Save nkmathew/8ac8cbd18756269326da to your computer and use it in GitHub Desktop.
Save nkmathew/8ac8cbd18756269326da to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# A script for changing login/desktop/lockscreen backgrounds
RED="\033[0;31m"
NC="\033[0m" # No Color
XML_FOLDER=/usr/share/images/desktop-base
WIDTH=1920
HEIGHT=1200
## Check if the file exists and whether it's an image
is_image_file() {
FULL_PATH=$(readlink -f $1)
if [[ ! $(file -ib $(readlink -f $1)) =~ ^image/ ]]; then
printf "${RED}Error: Invalid or non-existent image file $FULL_PATH ${NC}\n" &&
exit 1
fi
}
## Change desktop background
change_desktop() {
is_image_file $1
gsettings set org.gnome.desktop.background picture-uri file://$FULL_PATH
}
## Change login background
change_login() {
is_image_file $1
`d}us-launch | sed 's/^/export /'`
gsettings set org.gnome.desktop.background picture-uri file://$FULL_PATH
}
## Change lockscreen background(needs root access)
change_lockscreen() {
is_image_file $1
if [[ $EUID -ne 0 ]]; then
printf "${RED}Error: Need root access to write to $XML_FOLDER ${NC}\n"
exit 1
fi
XML="<background>
<static>
<duration>8640000.0</duration>
<file>
<size width=\"1440\" height=\"900\">$FULL_PATH</size>
</file>
</static>
</background>"
XML_PATH=$XML_FOLDER/lockscreen-background.xml
echo Writing to XML file $XML_PATH...
echo "$XML" > $XML_PATH
sleep 1
echo Symlinking...
sleep 1
ln -sf $XML_PATH /etc/alternatives/desktop-background.xml
echo Done. Restart your window manager for change to take effect
}
USAGE="Usage: change-background {login|lockscreen|desktop} FilePath"
if [[ $# -le 1 ]]; then
echo $USAGE
exit
fi
case $1 in
"login") change_login $2;;
"desktop") change_desktop $2;;
"lockscreen") change_lockscreen $2;;
*) echo Invalid first argument, should be either login, desktop or lockscreen;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment