The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 1,357     Votes:  4     ✅ Solution
Tags: scripts   desktop-background  
Link: 🔍 See Original Answer on Ask Ubuntu ⧉ 🔗

URL: https://askubuntu.com/q/1102084
Title: Change dynamic wallpaper directory every season
ID: /2018/12/15/Change-dynamic-wallpaper-directory-every-season
Created: December 15, 2018    Edited:  January 6, 2022
Upload: September 15, 2024    Layout:  post
TOC: false    Navigation:  true    Copy to clipboard:  false


Skip

Introduction

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:


Top ToS Skip

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

Top ToS Skip

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: <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>T</kbd>

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`:

``` bash

<a id="hdr4"></a>
<div class="hdr-bar">  <a href="#">Top</a>  <a href="#hdr3">ToS</a>  <a href="#hdr5">Skip</a></div>

# !/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.


<a id="hdr5"></a>
<div class="hdr-bar">  <a href="#">Top</a>  <a href="#hdr4">ToS</a>  <a href="#hdr6">Skip</a></div>

# 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


<a id="hdr6"></a>
<div class="hdr-bar">  <a href="#">Top</a>  <a href="#hdr5">ToS</a>  <a href="#hdr7">Skip</a></div>

# 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: 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 uncompress the files. Reference: 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

Top ToS
⇧ CPU frequency too high even with 'powersave' governor What does spool mean for printing?  ⇩