Ok this is my first true post, in dev community, so I gonna try do it the best that I can, so let's give context to this, in my past time how a GnuLinux user I liked to test a lot of distros (Yeah I was a distro hopper) one of all these was LXLE, this is a distro very cool and something that I like a lot of it was that it has a script by default that change the desktop background image in every boot up of your system, so cool yeah, but due to the destiny I had to use another distro, but I wanted have a same function to change the background image of my distro in every boot up, so due that I made my own script to do this task, anyway so much text and nothing of code, so let's do it.
⚠️ This script only works in the xfce4 desktop environment (This because xfce is which control the config of the background image and we will use binaries that are part of xfce to set the background)
Ok this is very very easy, first we need to use the binary xfconf-query the function of this binary is help us to set configurations in the desktop environment without use the GUI(Graphic user interface), so the first is type
xfconf-query -l
This for list the channels, ok let's explain what the hell are the channels, channel is how is name to every sub menu of settings that can be configure with this binary, and why instead of start to write the script we start typing commands??? So simple because we don't know which is the correct setting to modify to set the background😉👍✌️, so let's continue for prepare our script, let's see the last command with its respective output:
lex@lexSys:~$ xfconf-query -l
Channels:
parole
thunar-volman
xfprint
ristretto
xfce4-power-manager
xfce4-notifyd
xfce4-appfinder
thunar
xsettings
xfce4-panel
keyboards
xfce4-session
xfce4-settings-manager
xfwm4
xfce4-keyboard-shortcuts
xfce4-desktop
displays
The output print the list of channels, so for logic due we need to modify desktop settings the channel that have the necessaries options to modify the background desktop is xfce4-desktop
so the next is list the settings that we can modify in this channel, this is do it with the next command xfconf-query -c xfce4-desktop -lv
, ok let's explain the arguments of this command the -c
is for set the channel(xfce4-desktop
), -l
to list the settings in this channel, but why my command instead of have -l
it has -lv
??? Ok because the argument -v
is for show a verbose output which if I use it in this case, the command will gonna show me the list of settings in the channel xfce4-desktop
with its respective values😉👍✌️, also the command could be of this order xfconf-query -lvc xfce4-desktop
, so here is the command with its respective output:
lex@lexSys:~$ xfconf-query -lvc xfce4-desktop
/backdrop/screen0/monitor0/image-path /home/lex/Descargas/QwOrRDy.jpg
/backdrop/screen0/monitor0/image-show true
/backdrop/screen0/monitor0/image-style 5
/backdrop/screen0/monitor0/workspace0/color-style 3
/backdrop/screen0/monitor0/workspace0/image-style 5
/backdrop/screen0/monitor0/workspace0/last-image /home/lex/Descargas/QwOrRDy.jpg
/backdrop/screen0/monitor1/image-path /usr/share/xfce4/backdrops/xubuntu-wallpaper.png
/backdrop/screen0/monitor1/image-show true
/backdrop/screen0/monitor1/image-style 5
/backdrop/screen0/monitor1/workspace0/color-style 0
/backdrop/screen0/monitor1/workspace0/image-style 5
/backdrop/screen0/monitor1/workspace0/last-image /usr/share/xfce4/backdrops/xubuntu-wallpaper.png
/desktop-icons/file-icons/show-filesystem true
/desktop-icons/file-icons/show-home true
/desktop-icons/file-icons/show-removable true
/desktop-icons/file-icons/show-trash true
/desktop-icons/icon-size 48
/desktop-icons/show-thumbnails true
/desktop-icons/style 2
/desktop-icons/tooltip-size 64.000000
/desktop-menu/show-icons true
/last/window-height 546
/last/window-width 666
So here we can see that the setting that set the background image is /backdrop/screen0/monitor0/last-image
so for set any setting we need to use the command -s
, so the full command should be of this way xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s pathOfOurImageToSet
, ok finally we have our principal command now is time of do the Bash Script
So the next is make a folder where we gonna put all the images which we want that our script takes a image randomly to set it like background, the best is make a folder inside of the folder Images or Pictures so let's do it the folder:
mkdir /Images/backgroundImages
Right now comes the most cool the first is set a variable to get the total amount of files in our folder of background images this gonna be do it with this command:
fileNumber=$(ls -1 $HOME/Images/backgroundImages|wc -l)
let's explain this piped commands, first ls -1 $HOME/Images/backgroundImages
this ls
command list the files in our folder using the argument -1
to set that ls list one file per line, then we pipe the output of this to wc -l
this for the wc return us the total amount of lines that is translated how our total number of images files in our folder, we need this because we gonna use the shell variable $RANDOM
let's see how it works:
RANDOM
Each time this parameter is referenced,random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.
Ok how says above this shell variable takes a random number between 0 and 32767 but we can limit this range of this way, we need to use the syntax of bash to do arithmetic operations $(())
let's to see a example, if I want get a random number between 0 and 1000 we should type this in our shell:
echo $(( $RANDOM % 1000 ))
So maybe you already don't understand where this is going(Yeah maybe my ideas are very crazies and I can't explain in a simple way sorry) but let's to explain😉👍✌️ it, we need the total amount of files in our folder of images because that number will help us to set the number limit to get a random number, that due we gonna use the result of get that random number to choose a image from our folder of background images, ok let's to see the code to explain this better😉.
So let's to see my script😉👍✌️ and then explain how works every thing in this mess:
#!/usr/bin/env bash
folderPath="/home/lex/Imágenes/"
let fileNumber=$(ls -1 $folderPath|wc -l)
getFileNumber(){
let randomNumber=$(( $RANDOM % $fileNumber ))
}
getFileNumber
until [[ $randomNumber -ne 0 ]]; do
getFileNumber
done
imageToSet=$(ls -1 $folderPath|sed -n ${randomNumber}p)
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s $folderPath$imageToSet
First let's to explain how works this script, ok in the first line we have the shebang this line of code tell to our program loader which interpreter to use, then we have a variable named $folderPath
this variable gonna save us folder path where is our folder with the images, then we have let fileNumber=$(ls -1 $folderPath|wc -l)
this variable gonna save the total amount of files in our folder of images and we use let
to declare that the value this variable is a number int, then we have a function getFileNumber
this function get a random number based in our amount image files in our folder backgroundImages this for use this function in a loop until
that it works how filter, let's see this...the RANDOM shell variable returns a number between 0 and 32767, and the problem here is that we have a 0, and if we want to get a random number based in our the amount of image files, we can obtain a number 0 and it isn't in our really number of files, because that I putted the filter with the loop until this loop gonna execute the function getFileNumber
until the result wasn't 0, in simple words if the result of our function getFileNumber
is 0 the function will be execute until it don't be 0, at for last we use sed with the argument -n
this argument let us print number of lines or even search strings in text and if it match sed print the lines that have that string(Yeah like a grep
but with sed, very cool😁😁😁) with the random value of our variable randomNumber
to print the number line with name of that image file, this for concatenate in our last argument the variables $folderPath
and $imageToSet
in this command xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s $folderPath$imageToSet
for set our desktop background image, and that is all, very easy, the next is configure this script for be run on the start up of xfce4, this is very easy we have two ways we can use our GUI(Grafic User Interface), for this we press Ctrl+Esc for open our menu of applications, then we search Session and Startup select it, will be open the window of Session and Startup then we select Application Autostart and then we click it in Add then only we put the name our app that could be ChangeBackground and in Command we put the path of our script😉👍✌️. The another way is create a .desktop file in ~/.config/autostart
ok lets explain first is create the file with its respective name, it should be ChangeBackground.desktop
and then we add in its content this:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=ChangeBackground
Comment=Script for change the background.
Exec=/home/lex/changeBackground.sh
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false
Here the important is put the path of our script in the section Exec=, and for last our script must have execute permissions we can give it with this command:
sudo chmod u+x changeBackground.sh
and that is all, I hope that this post could be helpful, so use GnuLinux(Linux), Peace and Love, Never give up, happy coding and programming....👍✌️
Top comments (2)
Nice write up! I didn't know about xfconf-query ;-)
Cool, very useful to manage settings of xfce with your shell...😁✌️👍