Jacobs "Blog"

Programmatically create/set Ubuntu Mate Desktop Background

I recently obtained about 500 Pokemon wallpapers that I wanted to put on rotate and Ubuntu Mate sadly doesn't give you an easy way to create a slideshow for a wallpaper.

Some search terms for this: Programmatically create and set Ubuntu Mate desktop wallpaper background using a bash script

Now that's out of the way, the script is relatively simple; just save it somewhere, make it executable, and pass your options when you execute it.

#!/bin/bash

NAME=$1
DUR=$2
DIR=$3
BGDIR="/usr/share/backgrounds"
FIRST=`ls $DIR/*.jpg | head -n1`

mkdir -p $BGDIR/$NAME
BGDIR="$BGDIR/$NAME"

>$BGDIR/background-$NAME.xml
cat >> $BGDIR/background-$NAME.xml << EOF
<background>
  <starttime>
    <year>2009</year>
    <month>08</month>
    <day>04</day>
    <hour>00</hour>
    <minute>00</minute>
    <second>00</second>
  </starttime>
  <static>
    <duration>$DUR</duration>
    <file>$FIRST</file>
  </static>
EOF

OLD=$FIRST
for thing in `ls $DIR/*.jpg`
do
    NEW="$thing"
    cat >> $BGDIR/background-$NAME.xml << EOF
  <transition>
    <duration>$DUR</duration>
    <from>$OLD</from>
    <to>$NEW</to>
  </transition>
EOF
    OLD="$thing"
done

cat >> $BGDIR/background-$NAME.xml << EOF
  <transition>
    <duration>$DUR</duration>
    <from>$OLD</from>
    <to>$FIRST</to>
  </transition>
</background>
EOF

gsettings set org.mate.background picture-filename "$BGDIR/background-$NAME.xml"
gsettings set org.mate.background picture-options zoom

You chmod +x script then ./script

As it stands, it will only find your jpegs in that directory but outside of that, it'll create an appropriately named directory in the correct place, then use gsettings to set it. If the gsettings doesnt work - like for me - then you can just open the "Change Desktop Background", browse to the location created by the script and open the xml file like you would an image.

I'm writing this in a rush because I need to get back to work. Any queries; ping me.

Credit where it's due; I think this artist created the wallpaper I used as an image. I'm finding it hard to confirm though: http://pldh.net/gallery/the493


Ammendment

I've had to update the script because I realized that it was constantly transitioning. You need to set the transition from the old image to the new image, with a static tag inbetween each transition in order for it to sit on that image for a while.

The above script works well if you're transitioning between relatively similar images, where you might want them overlapping for a while over time (you could transition between colors for instance).

The script below will switch between each slide, and you can control the duration each slide is used for and the duration of the transition. Same process as before, however the new syntax is ./script <name> <slide duration> <transition duration> <path to files>.

#!/bin/bash

NAME=$1 ; SDUR=$2 ; TDUR=$3 ; DIR=$4 ; BGDIR="/usr/share/backgrounds" ; FIRST=`ls $DIR/*.jpg | head -n1`

mkdir -p $BGDIR/$NAME
BGDIR="$BGDIR/$NAME"

>$BGDIR/background-$NAME.xml
cat >> $BGDIR/background-$NAME.xml << EOF
<background>
  <starttime>
    <year>2009</year>
    <month>08</month>
    <day>04</day>
    <hour>00</hour>
    <minute>00</minute>
    <second>00</second>
  </starttime>
  <static>
    <duration>$SDUR</duration>
    <file>$FIRST</file>
  </static>
EOF

OLD=$FIRST
for thing in `ls $DIR/*.jpg`
do
    NEW="$thing"
    cat >> $BGDIR/background-$NAME.xml << EOF
  <transition>
    <duration>$TDUR</duration>
    <from>$OLD</from>
    <to>$NEW</to>
  </transition>
  <static>
    <duration>$SDUR</duration>
    <file>$NEW</file>
  </static>
EOF
    OLD="$thing"
done

cat >> $BGDIR/background-$NAME.xml << EOF
  <transition>
    <duration>$TDUR</duration>
    <from>$OLD</from>
    <to>$FIRST</to>
  </transition>
</background>
EOF

gsettings set org.mate.background picture-filename "$BGDIR/background-$NAME.xml"
gsettings set org.mate.background picture-options zoom