So, I know there is already some programs over the internet that does this trick. But I imagine that this must be a very simple program to make.
Can I make it with C and/or shell script?
I want to make a function that does something like bool setDesktopBG(ImagePath path)
what are the main calls I must make in order to do it? Which packets should I access to do it? I never coded nothing related to the OS like this so I don't know where exactly to begin?
Also, is this question more suitable to Stack Overflow? If it's the case, I'll delete it and post there. Thanks in advance.
Best Answer
What changes the wallpaper
In case of Ubuntu's default behavior when it comes to backgrounds, that's set through
gsettings
. In a nut shell, it's a form of database for settings to various apps. The simple command is this:, where
"$FILE"
is the shell script variable that holds full path to image in the formfile:///path/to/image.png
That's somewhat different from other desktop environments. Think of Ubuntu's Unity and Gnome as having a layer above typical X11 server ( i.e. the layer above GUI environment ). In such environments as
blackbox
oropenbox
you'd need to call an external program. For instance ,feh --bg-scale /path/to/image
I assume you want to deal with default environment - Ubuntu's Unity.
Making a shell function
Here's example of what I use in my
.bashrc
file.The function can be called like so
This function has two important parts. First, we can either have full path or partial path to the file - that's what
readlink -f "$1"
takes care of - it returns full path to image. Then we turn that into a string by combining output ofreadlink -f "$1"
andfile://
. Remember, Unix path is always starting with root folder,/
, so we need only leading two slashes there. Result would befile:///home/user/WALLPAPERS-FOLDER/cool-background.png
string and stored inFILE
variable.Finally we call
gsettings
withFILE
as argument.This function can be part of any script and called with any partial or full path as parameter
Putting together a script
Here's a script I sketched out in about 15 minutes that will make a list of images , and then select random image out of that list, and set background to that image. The script is also placed in my github , sergrep repository repository and will be developed just a little bit more to include extra functionality, but for the most part , the code version here does the big job.
In order to automate this script , refer to How do i make a program autostart every time i log in ?