2

I have a nested directory structure with travel pictures under ~/Pictures/Shotwell-Import/YYYY/MM/DD/.

I would like my desktop background to be randomly picked from those pictures.

Unfortunately, it seems that Cinnamon expects a flat directory, where the pictures are at the top level.

Any idea how to circumvent this limitation?
Thanks!

0

You can use a cron job as a workaround.

Instructions with an example directory structure. Modify as needed.

Assuming the nested directory structure is at /home/USERNAME/Pictures/Shotwell-Import

Write a shell script

Create a script file at /home/USERNAME/Pictures/set-random-image.sh:

#!/bin/bash

# Change to directory containing this script.
# See http://stackoverflow.com/a/3355423/246724
cd "$(dirname "$0")"

# Set the pictures directory
PICDIR="/home/USERNAME/Pictures/Shotwell-Import"

# Randomly pick one of the pictures.
# See http://www.webupd8.org/2009/11/3-lines-script-to-automatically-change.html
PICFILE=$(find $PICDIR -iregex '.*\.\(jpeg\|jpg\|png\)' | shuf -n1)

# Prevent a "dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY"
# Omit the last letter of "cinnamon-session" for the character limit in pgrep.
PID=$(pgrep -u $LOGNAME cinnamon-sessio)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

# Set Cinnamon background image.
# Other desktop environments need different command.
gsettings set org.cinnamon.desktop.background picture-uri "file://$PICFILE"

Try the script / set file permissions

Configure file permissions so you can execute this script. Then execute the script once.

cd /home/USERNAME/Pictures
# Copy an example picture to mybkg.jpg
./set-random-image.sh
# Permission problems?
chmod u+rwx set-random-image.sh
# Now it should work!
./set-random-image.sh
# Do it again a few times, and see the background change.
# If this does not work, this tutorial will be useless to you.
./set-random-image.sh
./set-random-image.sh

Configure a background image

Now set this as your background image. You could probably do this with commandline, but I think it is more transparent to do it in the UI.

Open the Settings > Backgrounds dialog.

In the "Settings" tab, disable "Play backgrounds as a slideshow". We don't need the slideshow, cause we are going to create a cron job.

In the "Images" tab, configure /home/USERNAME/Pictures/mybkg.jpg as the background image. You will need the "+" icon in the bottom left to add the folder /home/USERNAME/Pictures, and then select the image within the folder.

Configure a cron job

Type crontab -e. Add the following line to change it every minute:

* * * * * /home/USERNAME/Pictures/set-random-image.sh

Wait a minute and see if the background changes.

Note: Cinnamon setting vs file copy

In a previous version of this answer, the script would copy an image to a default location, instead of changing the cinnamon setting. The advantage was that this would work equally well for other desktop environments. The disadvantage is unnecessary disk writing every minute, which may be bad for an SSD. Paranoia won in the end, so I changed this answer.

  • Nah, great. Apparently the gsettings set does not work from cron. It says "dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY" – donquixote Jun 23 '16 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.