DEV Community

Cover image for Simple GUI on Linux Using Zenity and Node.js
Fernando Raposo da Camara Silva
Fernando Raposo da Camara Silva

Posted on

Simple GUI on Linux Using Zenity and Node.js

It is incredible the amount of "secret" features on Linux that are not well known until today.
I came across a tool called Zenity the other day. It is related to sending UI notifications on Linux.
Zenity is an open-source tool for displaying simple GUI in shell scripts. It makes scripts more user-friendly by displaying GTK+ dialogs. It is tipically used on shell scripting to improve its usability.

My goal is to do the following:

  1. Use Node.js to access financial data related to Brazilian Treasury Bonds (Tesouro Direto in Brazil);
  2. Create a mechanism to sistematically check instant treasury rates;
  3. Use Zenity to pop-up a notification to user;

Acessing Tesouro Direto Data

Tesouro Direto Rates can be found here. In addition, if you press F12 and check Network Tab, you'll see that the following json is received by the browser: https://www.tesourodireto.com.br/json/br/com/b3/tesourodireto/service/api/treasurybondsinfo.json
We can see that it contains all the current treasury rates up to date.
So, there's not much to do other than to use Node.js to make GET commands and capture that json file as shown bellow:

const axios = require('axios');
const { exec } = require("child_process");
const https = require('https');



const instance = axios.create({
  httpsAgent: new https.Agent({
    rejectUnauthorized: false
  })
});

instance.get('https://www.tesourodireto.com.br/json/br/com/b3/tesourodireto/service/api/treasurybondsinfo.json')
.then(x => {...

Enter fullscreen mode Exit fullscreen mode

Note: That rejectUnauthorized: false is important to be set, otherwise you will get a certificate error.

Task Scheduler

A cron job is a Linux command used for scheduling tasks to be executed sometime in the future. This feature is exactly what is needed to check the desired rates every hour. So, we will use a cron job to call our Node script to get the treasury bond rates every hour.
To create your Cron Job go to:
/var/spool/cron
And access user's cron tab by typing:
crontab -e
The README presents orientation about how to schedule commands using crontab by adding some expressionfollowed by the command that needs to be executed.
For instance, if you want your program to run every minute from 10AM to 17PM every weekday, write the following:
* 10-17 * * 1-5 <command>
In order to ease this task, there is a site called crontab.guru that helps us to construct the expression.
Note: To schedule cron to run your script EVERY HOUR of EVERY WEEKDAY from 10AM to 17PM, the expression is: 0 10-17 * * 1-5

  • Remember to add the full path of Node and script to make the cron tab job to work properly.

Notifications Using Zenity

Zenity is a tool that enables the generation of dialogs from terminal commands. Notifications look like this:
image
You can generate them by typing on Terminal:
zenity --notification\
--window-icon="info" \
--text="There are system updates necessary!"

There's a good chance that Zenity is alredy installed on your machine (In Ubuntu it is). If you do not have it, please type:
sudo apt-get update -y
next, type:
sudo apt-get install -y zenity
To conclude our goals we need a way to make Node run shell commands and process their I/O. This is achieved by using exec library as shown bellow:

  exec('zenity --notification --window-icon="info" --text="Taxa Atual TD 2035: IPCA+"' + taxa, (error, stdout, stderr) => {
    if (error) {
      console.log(`error: ${error.message}`);
      return;
    }
    if (stderr) {
      console.log(`stderr: ${stderr}`);
      return;
    }
  });
Enter fullscreen mode Exit fullscreen mode

The entire Github project is available here.

Fonts:
https://help.gnome.org/users/zenity/stable/index.html.en
https://www.howtoforge.com/how-to-display-gui-dialogs-in-bash-script-using-zenity/
https://crontab.guru/

Top comments (0)