33.8. Assorted Tips

33.8.1. Ideas for more powerful scripts

33.8.2. Widgets

It would be nice to be able to invoke X-Windows widgets from a shell script. There happen to exist several packages that purport to do so, namely Xscript, Xmenu, and widtools. The first two of these no longer seem to be maintained. Fortunately, it is still possible to obtain widtools here.

Caution

The widtools (widget tools) package requires the XForms library to be installed. Additionally, the Makefile needs some judicious editing before the package will build on a typical Linux system. Finally, three of the six widgets offered do not work (and, in fact, segfault).

The dialog family of tools offers a method of calling "dialog" widgets from a shell script. The original dialog utility works in a text console, but its successors, gdialog, Xdialog, and kdialog use X-Windows-based widget sets.


Example 33-20. Widgets invoked from a shell script

   1 #!/bin/bash
   2 # dialog.sh: Using 'gdialog' widgets.
   3 
   4 # Must have 'gdialog' installed on your system to run this script.
   5 # Or, you can replace all instance of 'gdialog' below with 'kdialog' ...
   6 # Version 1.1 (corrected 04/05/05)
   7 
   8 # This script was inspired by the following article.
   9 #     "Scripting for X Productivity," by Marco Fioretti,
  10 #      LINUX JOURNAL, Issue 113, September 2003, pp. 86-9.
  11 # Thank you, all you good people at LJ.
  12 
  13 
  14 # Input error in dialog box.
  15 E_INPUT=65
  16 # Dimensions of display, input widgets.
  17 HEIGHT=50
  18 WIDTH=60
  19 
  20 # Output file name (constructed out of script name).
  21 OUTFILE=$0.output
  22 
  23 # Display this script in a text widget.
  24 gdialog --title "Displaying: $0" --textbox $0 $HEIGHT $WIDTH
  25 
  26 
  27 
  28 # Now, we'll try saving input in a file.
  29 echo -n "VARIABLE=" > $OUTFILE
  30 gdialog --title "User Input" --inputbox "Enter variable, please:" \
  31 $HEIGHT $WIDTH 2>> $OUTFILE
  32 
  33 
  34 if [ "$?" -eq 0 ]
  35 # It's good practice to check exit status.
  36 then
  37   echo "Executed \"dialog box\" without errors."
  38 else
  39   echo "Error(s) in \"dialog box\" execution."
  40         # Or, clicked on "Cancel", instead of "OK" button.
  41   rm $OUTFILE
  42   exit $E_INPUT
  43 fi
  44 
  45 
  46 
  47 # Now, we'll retrieve and display the saved variable.
  48 . $OUTFILE   # 'Source' the saved file.
  49 echo "The variable input in the \"input box\" was: "$VARIABLE""
  50 
  51 
  52 rm $OUTFILE  # Clean up by removing the temp file.
  53              # Some applications may need to retain this file.
  54 
  55 exit $?
  56 
  57 # Exercise: Rewrite this script using the 'zenity' widget set.

The xmessage command is a simple method of popping up a message/query window. For example:
   1 xmessage Fatal error in script! -button exit

The latest entry in the widget sweepstakes is zenity. This utility pops up GTK+ dialog widgets-and-windows, and it works very nicely within a script.
   1 get_info ()
   2 {
   3   zenity --entry       #  Pops up query window . . .
   4                        #+ and prints user entry to stdout.
   5 
   6                        #  Also try the --calendar and --scale options.
   7 }
   8 
   9 answer=$( get_info )   #  Capture stdout in $answer variable.
  10 
  11 echo "User entered: "$answer""

For other methods of scripting with widgets, try Tk or wish (Tcl derivatives), PerlTk (Perl with Tk extensions), tksh (ksh with Tk extensions), XForms4Perl (Perl with XForms extensions), Gtk-Perl (Perl with Gtk extensions), or PyQt (Python with Qt extensions).