1

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.

fabionr
  • 13
  • Fairly easy question. It uses gsettings. I actually have a shell function in use already. What's your goal specifically ? Just to change wallpaper through code ? – Sergiy Kolodyazhnyy Mar 10 '16 at 22:59
  • 1
    Yes, then I'll make some kind of cron to call it periodically – fabionr Mar 10 '16 at 23:00
  • I'll be posting answer soon anyway, buy i really would like to know your specific goals here. This question is probably a duplicate, so unless you make it specific, it will be closed. That's my opinion though – Sergiy Kolodyazhnyy Mar 10 '16 at 23:01
  • 1
    Well, I want to make a program, in this case in shell as you suggested and then I want to set the OS to call it periodically like sh/bash changeDesktop pathOfFile – fabionr Mar 10 '16 at 23:04
  • OK. Working on an answer. cron probably isn't the easiest method, but i will throw some other suggestions there. – Sergiy Kolodyazhnyy Mar 10 '16 at 23:08
  • Can you please add the specific requirements you've mentioned in the comments to your question ? It's easier for people to see that way – Sergiy Kolodyazhnyy Mar 10 '16 at 23:27

1 Answers1

2

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:

gsettings set org.gnome.desktop.background picture-uri "$FILE" 

, where "$FILE" is the shell script variable that holds full path to image in the form file:///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 or openbox 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.

function chbackground {
    FILE="'file://$(readlink -f "$1" )'" 
    echo changing to "$FILE" 
    gsettings set org.gnome.desktop.background picture-uri "$FILE" 
}

The function can be called like so

chbackground WALLPAPERS-FOLDER/cool-background.png

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 of readlink -f "$1" and file://. Remember, Unix path is always starting with root folder, /, so we need only leading two slashes there. Result would be file:///home/user/WALLPAPERS-FOLDER/cool-background.png string and stored in FILE variable.

Finally we call gsettings with FILE 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 ?

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: 1047481448@qq.com 
# Date: March 10th 2016 
# Purpose: Set random wallpaper
#      To make it start automatically, add it as one of 
#          the Startup Applications in Ubuntu's Unity 
#          or Gnome 
# 
# Written for: https://askubuntu.com/q/744464/295286
# Tested on: Ubuntu 14.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.

ARGV0="$0"
ARGC=$#
change_background()
{
    FILE="'file://$(readlink -f "$1" )'" 
    echo changing to "$FILE" 
    gsettings set org.gnome.desktop.background picture-uri "$FILE"
}

make_list()
{
  # -max-depth may be optional , if you want to 
  # traverse subdirectories,too
  find "$1" -maxdepth 1 -type f > "$2"
}

main()
{
  # Variables
  local DISPLAY=:0 # ensure this is set
  local DIR="$1"
  local LIST="/tmp/wallpaper.list"
  local TIME="$2"
  # cat $LIST # for debugging only
  make_list "$DIR" "$LIST"
  while true 
  do
     change_background $( shuf $LIST | head -n 1   )
     sleep "$TIME"      
  done
}

main "$@"
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • @fabionr Well, glad I could help. Um, keep in mind , cron runs as root and doesn't have access to GUI unless you give it DISPLAY=:0 so that might not be the best solution. I will be adding more stuff to this answer, so check it out later :) Happy coding ! – Sergiy Kolodyazhnyy Mar 10 '16 at 23:34
  • Hey, thanks again for the script but it is not working yet. I just copied the script you gave me and i'm calling it to change at each 10seconds (just to test). I'm having this output:

    fabio@fabio-PC:~/Desktop$ sudo bash chbk.bashrc /home/fabio/Pictures/wallpapers/ 10 changing to 'file:///home/fabio/Pictures/wallpapers/Wallpaper-60E.jpg' changing to 'file:///home/fabio/Pictures/wallpapers/HD-Wallpapers-89_rElkAPy.jpg' etc... It is saying that is changing the background but it stills the same...

    – fabionr Mar 13 '16 at 02:00
  • Well, you are calling the script with sudo . You don't need that. gsettings schemas are set per user. If you are running that as root, you are changing gsettings for root, but you want to change it for your account – Sergiy Kolodyazhnyy Mar 13 '16 at 02:32
  • You're welcome :) Check out my other scripts on git. Also keep checking this answer time to time, i plan on editing it and improving – Sergiy Kolodyazhnyy Mar 13 '16 at 02:36