gsettings set org.gnome.desktop.background not working

12,845

Solution 1

I think I found the issue: Even if I go to the file, right click, and say "Set as Wallpaper...", nothing happens. So I postulated that it's the fact the filename isn't changing each time; Linux has some kind of cost-saving I'm-not-actually-going-to-refresh-because-that's-the-same-image feature. To force the system to recognize it's a new picture each time, vary the filename like this:

#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"
RAND=$RANDOM

rm -f ${PICS}/*.jpg
rm -f ${PICS}/photo-of-the-day

# download photo-of-the-day page
wget http://photography.nationalgeographic.com/photography/photo-of-the-day -O ${PICS}/photo-of-the-day

# parse the url out from the file
url=`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '"' -f 2`

# download the photo
wget http:$url -O ${PICS}/wall${RAND}.jpg

# set the desktop background
URI="file://${PICS}/wall${RAND}.jpg"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"

Solution 2

You can either quote the full URI variable as cmks have shown, or ensure that file:// and wall.jpg are quoted, like so:

URI="file:///"${PICS}"/wall.jpg"

Here's a small improvement of your script. Variables are used to shorten the command line. File is saved to /tmp, which is deleted each time system is restarted, so you don't have to manually clear cache. AWK is used to improve parsing and decrease piping. wget directly writes to AWK to avoid saving extra files

#!/bin/bash

# set variables to shorten lines
FILE="/tmp/photo_of_the_day"
PAGE="http://photography.nationalgeographic.com/photography/photo-of-the-day"
SEARCH="images.nationalgeographic.com.*cache.*990x742.jpg"

# get image URI directly
IMAGE=$(wget "$PAGE" -O - -o /dev/null  | awk -F'"' -v regex="$SEARCH" '$0~ regex {print $2}')

# download the photo
wget http:$IMAGE -O "$FILE"

# set the desktop background
URI="file:///$FILE"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"

Solution 3

try it this way:

#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"

rm -f "${PICS}/wall.jpg"
rm -f "${PICS}/photo-of-the-day"

# download photo-of-the-day page
wget "http://photography.nationalgeographic.com/photography/photo-of-the-day" -O "${PICS}/photo-of-the-day"

# parse the url out from the file
url="`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '\"' -f 2`"

# download the photo
wget "http:$url" -O "${PICS}/wall.jpg"

# set the desktop background
# only two slashes here, because the PICS var already has a leading slash
URI="file://${PICS}/wall.jpg"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"
Share:
12,845

Related videos on Youtube

Pavel Komarov
Author by

Pavel Komarov

Updated on September 18, 2022

Comments

  • Pavel Komarov
    Pavel Komarov over 1 year

    I am trying to download a daily picture and set it as my background image with:

    #!/bin/bash
    # clear cache
    PICS="/home/pvlkmrv/Pictures"
    
    rm -f ${PICS}/wall.jpg
    rm -f ${PICS}/photo-of-the-day
    
    # download photo-of-the-day page
    wget http://photography.nationalgeographic.com/photography/photo-of-the-day -O ${PICS}/photo-of-the-day
    
    # parse the url out from the file
    url=`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '"' -f 2`
    
    # download the photo
    wget http:$url -O ${PICS}/wall.jpg
    
    # set the desktop background
    URI=file:///${PICS}/wall.jpg
    echo ${URI}
    gsettings set org.gnome.desktop.background picture-options 'centered'
    gsettings set org.gnome.desktop.background picture-uri ${URI}
    

    The image downloads just as expected, but the background is not actually set. Strangely, it works if I modify URI to include more or fewer forward-slashes, but it does so only once. I end up having to modify the script in what should be a meaningless way every time in order to make this section work.

    What could be causing this?

    • muru
      muru about 8 years
      Since you're always using the same filename, you don't need to run the gsettings command again each run. It should detect a changed file and automatically update the displayed background. I suggest commenting out everything after wget http:$url -O ${PICS}/wall.jpg.
    • Pavel Komarov
      Pavel Komarov about 8 years
      This does not work; I have to tell the system to reload the image. You are likely correct, but it would take a shutdown or log out to make the program reload the image from the disk.
  • Pavel Komarov
    Pavel Komarov about 8 years
    Yes, just adding quotes around the URI and ${URI} seems to fix it.
  • Pavel Komarov
    Pavel Komarov about 8 years
    No! Now it's not working again, and the only change I've made is the quotes.