Sunday, February 28, 2021

A simple weather notification for the XFCE panel

[Update: New & Improved version available.]

This is a very minimalistic weather applet for the XFCE panel using data from OpenWeather via ansiweather, a terminal shell script.  The applet uses the XFCE Genmon plugin to run a simple script. XFCE does have a weather plugin for the panel, but this is a)much simpler and b)an excuse to play with writing scripts.

A bit chilly this morning, as you can see. (The orange icon, in case you wondered, is my previous adventure in scripting, an update notification for the XFCE panel.)

The tooltip shows a five day forecast. Clicking on the temperature in the panel can refresh the data, open a terminal to view the source data in a terminal, or go to the OpenWeather web page for your town where you can see an eight day forecast.

To use the applet:

1. install ansiweather

2. find your location at OpenWeather and replace <city,country code, eg London,UK> and the city code ******* in the URL as appropriate. Also find the right number to replace the * in  genmon-*, if you want a click to refresh the applet. The method is described in a comment in the script.

3. remove the # before the click command you want.

4. copy the script into a .sh file and make it executable.

5. add an instance of Genmon to the panel, enter the path to the .sh file and a refresh time in seconds, and save.

#!/bin/bash
temp=$(ansiweather -l <city,country code, eg London,UK> | awk '{print $6$7}')
forecast=$(ansiweather -l <city,country code, eg London,UK> -s true -a false -F | awk '{print $1 "\n\n" $4,$7,$8,$9 "\n" $11,$14,$15,$16 "\n" $18,$21,$22,$23 "\n" $25,$28,$29,$30 "\n" $32,$35,$36,$37 "\n" $38,$41,$42,$43}')

PANEL="<txt>$temp</txt>"
PANEL+="<txtclick>firefox --new-tab --url https://openweathermap.org/city/*******</txtclick>"
#replace ******* with the code number for your city
#PANEL+="<txtclick>xfce4-panel --plugin-event=genmon-*:refresh:bool:true</txtclick>"
#You will need to obtain the number in genmon-* by going to panel>preferences>items and hovering over the instance of Genmon
#PANEL+="<txtclick>xterm -hold -e  ansiweather -l <city,country code, eg London,UK> -s true -f 7</txtclick>"

TOOLTIP="<tool>"
TOOLTIP+="<span weight='bold' font='sans regular'>Forecast</span>\n\n"
TOOLTIP+="<span font= 'monospace regular'>$forecast</span>\n"
#TOOLTIP+="<span weight='bold' font='sans regular'>Click to update</span>"
TOOLTIP+="<span weight='bold' font='sans regular'>OpenWeather</span>"
#Change to suit the click action you choose
TOOLTIP+="</tool>"

echo -e "${PANEL}"
echo -e "${TOOLTIP}"

[Update: Here is an alternative script with a nicer column format:

NB the temperature column will accommodate six characters, so "-5/-10" would be OK, but "-15/-10" wouldn't. Change %6s (a six character string) to %7s if you live somewhere that never gets above minus double digits in the winter. If you live somewhere temperate that never gets into double-digit cold, %5s  will be OK and look better. Screenshot below is %6s.

#!/bin/bash
city=$(ansiweather -l london,uk | awk '{print $4}')
temp=$(ansiweather -l london,uk | awk '{print $6$7}')
forecast=$(ansiweather -l london,uk -s true -a false -F | awk '{printf "%s %6s%s %s\n%s %6s%s %s\n%s %6s%s %s\n%s %6s%s %s\n%s %6s%s %s\n", $4,$7,$8,$9,$11,$14,$15,$16,$18,$21,$22,$23,$25,$28,$29,$30,$32,$35,$36,$37}')

PANEL="<txt>$temp</txt>"
PANEL+="<txtclick>firefox --new-tab --url https://openweathermap.org/city/*******</txtclick>"
#PANEL+="<txtclick>xfce4-panel --plugin-event=genmon-*:refresh:bool:true</txtclick>"
#PANEL+="<txtclick>xterm -hold -e  ansiweather -l london,uk -s true -f 7</txtclick>"

TOOLTIP="<tool>"
TOOLTIP+="<span weight='bold' font='sans regular'>Forecast</span>\n"
TOOLTIP+="<span font= 'monospace regular'>$city</span>\n"
TOOLTIP+="<span font= 'monospace regular'>$forecast</span>\n"
#TOOLTIP+="<span weight='bold' font='sans regular'>Click to update</span>"
TOOLTIP+="<span weight='bold' font='sans regular'>OpenWeather</span>"
TOOLTIP+="</tool>"

echo -e "${PANEL}"
echo -e "${TOOLTIP}"

Replace london,uk and * as appropriate.]


[Edit: removed superfluous variable entries from script.]

No comments:

Post a Comment