Change dynamic wallpaper directory every season

The basic question is how to do something at the start of Spring, Summer, Fall and Winter. For this I would create a bash script that runs on boot, rather than clogging up cron with entries.

I've approached this answer using the OP's question "How do I develop a script?". So I've deviated from usual method of simply posting a bash script and enhanced the answer with:

  • References are included within the code. They link to Stack Exchange answers for solving specific problems. For example: How to copy files, How to get day of year, etc.
  • A section on "Testing" is provided as it is something we all need to do
  • A section on "Enhancements" is provided because software is usually developed in versions where each is incrementally better than the previous version.

When do seasons start?

From the Farmer's Almanac:

Seasons of 2018

 Season     Astronomical Start                     Meteorological Start
 ======     =====================================  =====================
 SPRING     Tuesday, March 20, 12:15 P.M. EDT      Thursday, March 1 
 SUMMER     Thursday, June 21, 6:07 A.M. EDT       Friday, June 1
 FALL       Saturday, September 22, 9:54 P.M. EDT  Saturday, September 1
 WINTER     Friday, December 21, 5:23 P.M. EST     Saturday, December 1

Convert season start date to Day of Year

For our bash script to work we need to know what day of the year each seasons start.

$ echo $(date --date="March 20" '+%j')
079
$ echo $(date --date="June 21" '+%j')
172
$ echo $(date --date="Sep 22" '+%j')
265
$ echo $(date --date="Dec 21" '+%j')
355
# Reference: https://unix.stackexchange.com/questions/352176/take-input-arguments-and-pass-them-to-date

Create bash script: season.sh

Open the terminal using: Ctrl+Alt+T

Create the directory if it doesn't exist: mkdir -p ~/bin

Edit the script using: gedit ~/bin/season.sh

  • Note: Lubuntu user's need to use leafpad instead of gedit

Copy and paste the following lines into gedit:

#!/bin/bash
# NAME: season.sh
# PATH: ~/bin
# DATE: December 15, 2018

# NOTE: Written for: https://askubuntu.com/questions/1100934/change-dynamic-wallpaper-directory-every-season/1102084#1102084

# User defined variables, change to suit your needs
# Our directory names, lines indented for cosmetic reasons only
SlideShowDir="~/Season Slide Show"
   SpringDir="~/Pictures/Spring Slide Show"
   SummerDir="~/Pictures/Summer Slide Show"
     FallDir="~/Pictures/Fall Slide Show"
   WinterDir="~/Pictures/Winter Slide Show"

CheckTripWire () {
    # Our last season is in "~/Season Slide Show/CurrentSeason"
    LastSeasonFilename="$SlideShowDir"/CurrentSeason
    LastSeason=$(cat "$LastSeasonFilename")

    [[ "$LastSeason" == "$Season" ]] && return 0 # Season still the same

    # We now know our season has changed.

    rm -f "$SlideShowDir"/{*,.*}           # Erase all files in target
    # Reference: https://askubuntu.com/questions/60228/how-to-remove-all-files-from-a-directory

    echo "$Season" > "$LastSeasonFilename" # Record new season in target

    # Copy new slide show based on season
    if (( "$Season" == SPRING)) ; then
        cp -R "$SpringDir"/. "$SlideShowDir"/
        # Reference: https://stackoverflow.com/questions/3643848/copy-files-from-one-directory-into-an-existing-directory
    elif (( "$Season" == SUMMER)) ; then
        cp -R "$SummerDir"/. "$SlideShowDir"/
    elif (( "$Season" == FALL)) ; then
        cp -R "$FallDir"/. "$SlideShowDir"/
    else
        cp -R "$WinterDir"/. "$SlideShowDir"/
    fi

} # End of CheckTripWire () function.

# Start of Mainline

DOY=$(date '+%j')                     # DOY = Current Day of Year
# Reference: https://stackoverflow.com/questions/10112453/how-to-get-day-of-the-year-in-shell

if ((DOY>=079 && DOY<172)) ; then
    Season="SPRING"                   # Spring has sprung!
    # Reference: https://stackoverflow.com/questions/12614011/using-case-for-a-range-of-numbers-in-bash
elif ((DOY>=172 && DOY<265)) ; then
    Season="SUMMER"                   # Hit the beach!
elif ((DOY>=265 && DOY<355)) ; then
    Season="FALL"                     # Rake those leaves!
else
    Season="WINTER"                   # Shovel the snow!
fi

# Current season establish, now see if we tripped the wire
CheckTripWire

exit 0 # Command not necessary but good habit to signify no Abend.

Save the file in gedit. Now mark it as executable using:

chmod a+x ~/bin/season.sh

Next we need to add it to startup applications. Reference: How do I start applications automatically on login?

Note: You probably already have your slide show setup in startup applications. You will want to use season.sh BEFORE your regular slide show as it deletes and copies files which would crash the slide show program if it started first.


Testing

You will want to test season.sh script when you create it and not wait a year to see if it works properly or not. Reference: https://serverfault.com/questions/138325/faking-the-date-for-a-specific-shell-session


Enhancements

After initially developing a script it is common to enhance it Days, Weeks, Months or even Years later. This section discusses some enhancements you might want to make to session.sh down the road.

Compress files to save disk space

Consider keeping the off-season images compressed in TAR (Tape Archive) format to save on disk space. Then replace the cp (Copy) command with the tar command to un-compress the files. Reference: https://www.rootusers.com/23-tar-command-examples-for-linux/:

For example, we would change:

cp -R "$SpringDir"/. "$SlideShowDir"/

To:

tar -xf "$SpringDir"archive.tar -C "$SlideShowDir"/

... and so on for the other seasons.

Setup variables for season start

Using variables for season start days makes it easier to modify the script and makes the code easier to read (aka code readability).

Consider setting up Variables for start of season:

SpringStart=079
SummerStart=179
FallStart=265
WinterStart=355

Define the variables at the top of the script to make them easier to spot and change. You might want to do this for leap years. You might want to change to "Meteorological" season starts instead of "Astronomical" start dates.

Then change these lines:

if ((DOY>=079 && DOY<172)) ; then
elif ((DOY>=172 && DOY<265)) ; then
elif ((DOY>=265 && DOY<355)) ; then

To this:

if ((DOY>="$SpringStart" && DOY<"$SummerStart")) ; then
elif ((DOY>="$SummerStart" && DOY<"$FallStart")) ; then
elif ((DOY>="$FallStart" && DOY<"$WinterStart")) ; then

NOTE: I was hoping to finish this answer in an hour but it's been two hours and I have to do some Saturday Shopping. To Be Continued...


Perhaps this is an easier way:

  1. Create a symlink from ~/images/mybackgrounds to ~/images/spring:

    ln -s ~/images/spring ~/images/mybackgrounds
    
  2. Use one of these methods to display a background slideshow using images from ~/images/mybackgrounds.

  3. Set up crontab entries to change the symlink on particular days. Create a file called ~/mycrontab with these contents:

    # min  hr     day     mon  dow
    0      9      21      3    *     ln -sf ~/images/spring ~/images/mybackgrounds
    0      9      21      6    *     ln -sf ~/images/summer ~/images/mybackgrounds
    0      9      21      9    *     ln -sf ~/images/fall ~/images/mybackgrounds
    0      9      21      12   *     ln -sf ~/images/winter ~/images/mybackgrounds
    

    Run

    crontab ~/mycrontab
    

    to register the crontab entries. On March 21 at 9AM, crond will run the command

    ln -sf ~/images/spring ~/images/mybackgrounds
    

thus linking ~/images/mybackgrounds to ~/images/spring. On Jun 21 at 9AM, crond will change the symlink so that ~/images/mybackgrounds points to ~/images/summer. The slideshow program is configured to select a file from ~/images/mybackgrounds. The path to ~/images/mybackgrounds stays the same, but now all the contents are different because the symlink points to a different location. The crontab entries for Sep 21 and Dec 21 pull the same trick.