North Idaho Linux Users Group

Categories

Downloads

Archives

Zenity - a way to GUI-ness for command line users

By Rod Anderson

Zenity is a way to wrap a GUI around a shell or other command line script.

It is installed as part of Mint and probably most other standard distributions.

The man page sucks a bit, assuming that you know all the common GUI definers and features. It isn't all that complete, leaving out a lot of the more powerful features.

Here are several sites I've found very helpful.

I found these using Google with 'zenity forms examples' as the search criteria.

There is also a fork and enhancement called Yad but I am more than happy with Zenity (so far) so I didn't try to install Yad. Also it isn't part of the standard packages in Linux Mint 13.

Zenity isn't a magical mind reading application. You will need some (or a lot of) shell scripting knowledge to accomplish anything complicated. But what better way to leave that notepad in your pocket and simply click/double-click an icon and away you go.

So here we go with a few Zenity wrapped scripts I use.

Whatsmyip

We know we can use one of the web sites and get this information but it isn't as cool as just a click on icon.

#!/bin/bash

_IP=$(wget -qO- http://ipecho.net/plain)
zenity --info --­title="Current IP address" text="$_IP"

jDate

  

I like to use a modified version of a Juliene date (YYYdoy) to label files or assign transaction IDs.

#! /bin/bash

szDate=$(zenity --calendar --text="Pick a day" --title="Juliene Date" )
jzDate=$(date -­d "$szDate" "+%Y%j")
zenity --info --title="Date $szDate" --text="faux Juliene is: \n$jzDate"

mkPass

Sometimes you want a non-guessable password but may need to replicate/recover it later.

When I was creating a shared hosting setup for PostgreSQL I used a shell script that this is based on.

It allowed me to “recover(?)” the password for an account.

#!/bin/bash
zValues=$(zenity --forms --title="Password Generator" \
--separator="," \
--text="Enter a seed value" \
--add-entry="Seed value" \
--text="Start Point" \
--add-entry="Start point" \
--add-entry="Password length")

zSeed=$(echo $zValues | cut -d \, -f 1)
zStart=$(echo $zValues | cut -d \, -f 2)
zLen=$(echo $zValues | cut -d \, -f 3)
zStop=$(($zStart + $zLen - 1))

[ $zStop -ge 32 ] && zStart=1
[ $zStop -ge 32 ] && zLen=1

# zStart=($zStop >= 32) ? 1 : $zStart
# zLen=($zStop >= 32 ? 8: $zLen)
zStop=$(($zStart + $zLen - 1))

zPass=$(echo ${zSeed} | md5sum | md5sum | cut -b ${zStart}-${zStop})
zenity --info --title="Generated password" --text="$zPass"

#zenity --info --title="Generated password" --text="$zSeed\n$zStart\n$zLen\n$zStop"

# for p in $( echo -n $1 | cut -d '@' -f 1- --output-delimiter=' ' ) ; do echo $p ; done

Envelop

Yup I left the 'e' off.

I found a Perl script that printed USPS address and barcode information on envelopes. It uses/requires a 'env' file with sender and recipient. Since I use a couple different envelope sizes and have several places I need to send snail-mail to I wanted a way to select one 'env' file and the printer I wanted to print to. There is an example of an 'env' file after the script listing.

(Next on the list is a way to build the 'env' file using Zenity.)

#!/bin/bash
pushd ~/envelopes > /dev/null 2>&1

declare -A ENVELOPES
ENVELOPES[No_6-3_4]="# 6-3/4"
ENVELOPES[No_10]="# 10"

fileList=$(for e in $(ls -1 *.env); do echo "FALSE" $(basename $e .env); done)

env=$(zenity --list --height="350" --width=400 --text "Select Envelope to print"
--radiolist --column "Pick" --column "Envelope file" $fileList )

envType=${env##*.}

if [ $envType == $env ]; then
envType='No_10'
fi

prt=$(zenity --list --height=300 --width=300 --text "Select a printer" --radiolist
--column "Pick" --column "Printer" FALSE OfficeLaser FALSE EpsonColor )

if [ -z $prt ]; then
zenity --info --text="No Printer selected. Envelope Print cancelled"
exit
fi

doit=$(zenity --question --title="Print Envelope $ans" --text="Make sure a $
{ENVELOPES[$envType]} envelope is $prt")

case $? in
0)
envelope "${env}.env" | lpr -P${prt}
;;
*)
zenity --info --text="Envelope Print cancelled"
;;
esac

popd > /dev/null 2>&1

'env' File Example

There are a lot of options that can go in the file but I've found the defaults and this simple one works fine.

[to]
<Helvetica 14>
Idaho Miltary Division
Public Safety Communications
700 S Stratford Dr
Meridian, ID 83642­6206

[from]
<Helvetica 12>
Kootenai Amateur Radio Society
PO Box 1765
Hayden, ID 83835­1765