pgprompt

This script lets you pop up simple windows prompting the user to select something from a list, select a file, type in something, or click a button. It does NOT require you to pre-design a gui in XML. You simply pass command line parameters, for example the following snippet would show a window like in the first screenshot below:
export MAILCLIENT="sylpheed-claws"
pgprompt-gtk2 tell MESSAGE="You have mail" BUTTONS="Exit/Summary/Start $MAILCLIENT" \
	      BUTTONSEP="/" DEFAULTBUTTON="Exit" CANCELBUTTON="Exit" 

It tries to apply intuitive behaviours - for example if a CANCELBUTTON is defined, then you can hit Escape and it will act as if you clicked the specified button. Ditto for DEFAULTBUTTON if you hit Return.

For convenience here I've just included the help that the script prints out - it should give you a fair idea of what you can do. Also, take a look at the example command lines and the screenshots below. For other examples of using it you can also look at my quake3 scripts.

In the case of the file dialogs that it shows - it shows the old simple style of file dialog with keyboard completion that works (ie not the crappy GNOMEified rubbish dialogs).

Downloads

Software downloads are released under the GNU GPL. All downloads and related files are released "as is" and their use is entirely at your own risk.

The gtk1 version is used by the gtk2 version to allow selection of standard X11 / GTK fonts (ie the gtk1font mode).

Screenshots

Here are some screenshots of it in action. The Gtk1 version is still useful for example for picking a Gtk1 or X11 style font.





The following pic is pgprompt-gtk1 showing the old style font dialog.


Usage

pgprompt[pgprompt-gtk2] version 1.9
usage: pgprompt [version|help] [MODE] [PARAMETER=value]...
valid MODEs are:
   tell ask buttons list combo text field file filebox tail font gtk1font x11font gtk2font pangofont slider
valid PARAMETERs are:
   MESSAGE BUTTONS BUTTONNUM LISTNUM CANCEL CANCELBUTTON OKBUTTON OKPREFIX
   IMMEDIATE TITLE GTK2FONT LIST REVERSELIST SELECTED LISTSELECTION CLOSEVALUE
   WIDTH HEIGHT CENTERED BUTTONDIR BUTTONSEP LISTSEP COMMAND COMMANDSEP
   CURSORPOS SAVETEXT MONOSPACED FONT X Y PAD DEFAULTBUTTON TEXTREADONLY
   MULTILINE INTERP TEEFILE SLIDER_RANGE SHOWVALUE NOBUTTONS NODECORATIONS
   SLIDER_ACTION KEY_ACTIONS
specify -h on the command line for more detailed help


  The first and only argument specifies the type of prompt and is one of:
    tell      shows a message with an OK button
    ask       shows a message with OK and cancel buttons
    buttons   specifies that a list of buttons is to be shown
    list      specifies that a list should be shown with a list of items
    combo     specifies that a list of items is to be shown in a drop-down box
    text      specifies that a text box should be shown
    field     specifies that single line field should be shown
    file      shows a file selection dialog
    filebox   shows a text entry field and Browse button for entering a file path
    tail      reads standard input and shows it in a text field
    font      shows a font selection dialog
    gtk1font  shows a GTK1 (X11) font selection dialog
    x11font   shows an X11 font selection dialog
    gtk2font  shows a GTK2 (pango) font selection dialog
    pangofont shows a Pango font selection dialog
    slider    shows a slider for the range from SLIDER_RANGE
    
The general idea is that a window is shown prompting the user with buttons,
a list or drop down box, or a text entry field. When the user finishes, their
selection is printed to standard output, unless the COMMAND option is set, in
which case the specified command is run with the users selection appended.

  other details are specified by the PARAMETER=value pairs where the parameters
  are one of:
    MESSAGE       if set, specifies a label that will be shown with that message. Supports interpolation.
    BUTTONS       specifies the names (text) of buttons to be shown. Supports interpolation. If any button is just '%', a separator will be used instead - use '%%' if you actually want a button with '%' on it.
    BUTTONNUM     [Boolean] if set, if the user selects a button other than the cancel button, its  number is returned instead of its name.
    LISTNUM       [Boolean] if set, when the user selects a list, its number is returned  instead of its text.
    CANCEL        sets the value will be returned if the user presses the cancel button or closes the window
    CANCELBUTTON  specifies which button should act as the cancel button
    OKBUTTON      if set, it specifies which button is used as the OK button it can be a very simple pattern like A|B for example if you have two buttons A and B which should both behave like OK buttons
    OKPREFIX      [Boolean] if set, then if an ok button is pressed, the output will be be preceded by the name of the button. The default is for OK buttons to produce just the output (eg contents of text field), but for other buttons to produce just their names.
    IMMEDIATE     [Boolean] if set, then clicking on a list item will have the same effect as selecting it and then clicking the OK button
    TITLE         sets the title for the window
    GTK2FONT      [Boolean] tells the font mode to output a font in gtk2 format
    LIST          specifies items for list or combo mode. See LISTSEP. Supports interpolation
    REVERSELIST   [Boolean] if set, the list items will be shown in reverse order
    SELECTED      if set, it pre-selects a combo or list item or items, or sets the text or file name for the text,field, and file modes.
    LISTSELECTION controls how selection behaviour in the list widget - one of: multiple,single,or browse. single means zero or one item can be selected. browse is like single except there is at least one item selected. multiple means zero or more items can be selected.
    CLOSEVALUE    sets the value to use if the window is just closed by the user, without them using a button (ie closing it with their window manager controls). The default is to do whatever the cancel value is.
    WIDTH         if HEIGHT is also set, specifies the minimum window width in pixels.
    HEIGHT        if WIDTH is also set, specifies the minimum window height in pixels.
    CENTERED      [Boolean] or "mouse" if set (default=yes), the window will be automatically centered
    BUTTONDIR     if set to h or v, it changes the direction buttons are laid out.
    BUTTONSEP     specifies what character is used to separate button names in the BUTTONS list. See the examples below.
    LISTSEP       (default=newline) specifies what character is used to separate items in the LIST and SELECTED lists. See the examples below.
    COMMAND       specifies a command to run afterwards with the results appended to the command line
    COMMANDSEP    (default=" "), specifies a different character to split the COMMAND option with.
    CURSORPOS     if set, and the mode is "text" this parameter specifies the cursor position within the text field - in characters from the start of the text.
    SAVETEXT      [Boolean] if set, and and the mode is "text", then no matter what the result (ie whatever button the user presses), the text from the text field will be saved to the file specified by this parameter. If you specify this, and the user clicks on your OK button, the OK button will just return OK (or whatever you specified for OKVALUE). If you do not specify this, then the OK button prints the text from the text box.
    MONOSPACED    [Boolean] if set (default=no),the font "fixed" will be used for the text box. To specify a different monospaced font, use the FONT parameter
    FONT          sets the font to use for the text box.
    X             if CENTERED is false, X and Y will position the window
    Y             if CENTERED is false, X and Y will position the window
    PAD           [int] the number of pixels of padding between widgets
    DEFAULTBUTTON specifies which button should be the default (used if the user hits enter)
    TEXTREADONLY  specifies that the text field should be made read-only
    MULTILINE     [Boolean] causes \n within the MESSAGE parameter to be converted to newlines
    INTERP        [Boolean] if set to false, interpolation of parameter values. Can still be overridden for a particular parameter by prefixing it with %
    TEEFILE       if set, and the mode is tail, then any input read will also be written to the file specified by this parameter
    SLIDER_RANGE  [lower,upper,step,page,digits] sets the parameters of the slider - digits is the number of digits to show 
    SHOWVALUE     [Boolean] if set to false, the slider will not show its value. 
    NOBUTTONS     [Boolean] dont show the buttons
    NODECORATIONS [Boolean] ask the window manager not to show window decorations (borders etc)
    SLIDER_ACTION a command to run each time the value of the slider changes. The value will be substituted for the code %v, and the output of the command will be shown in the gui
    KEY_ACTIONS   [key=>cmd] set some shell commands to run for particular keys: eg Tab=>echo hit tab,Escape=>echo hit escape;

  Note about 'boolean' parameters:
    If a parameter is a boolean parameter, it can be set or unset in various ways:
      - an empty value or any of the values: 0 n no f false off
        specifies a false value for the parameter.
      - any of the values: y yes t true on
        specifies a true value for the parameter.
      - the parameter by itself is equivalent to PARAMETER=y (ie an true value)
      - if the paramater is prefixed by NOT it inverts the sense of how the
        value is interpreted, for example NOTCENTERED or NOTCENTERED=yes is
        equivalent to CENTERED=no

  Note about 'supports interpolation':
    Unless interpolation is disabled by setting the INTERP boolean paramter
    to false, or unless the parameter name is prefixed with a '%', then
    if a parameter says it "supports -" above, the following rules apply:
       - if the value specified is "-", standard input will be read for a 
              list of button names, one per line.
       - if the value specified ends in in a pipe "|", it is taken as a 
         command to generate the item value. The command is run and it's 
         output becomes the value of the parameter.
    For example, %MESSAGE=- will read the MESSAGE parameter from standard input.
    Only ONE option may specify read standard input using -, but
    multiple ones can use the pipe form.

  ********************************************************
  here are some examples:
  ********************************************************
  Note these examples have been spread over multiple lines for readability.

  pop up a message 300 pixels wide in the center of the screen:
    pgprompt tell MESSAGE="You have new mail." TITLE="New Mail" WIDTH=300 
    

  ask the user for an item from a list, shown in a drop-down box:
    pgprompt list MESSAGE="pick a value:" LIST="a b c d" 

  a simple yes/no dialog:
    pgprompt ask MESSAGE="click OK to continue:" TITLE="continue?" 

  the same dialog, but with custom buttons, and set so that cancelling prints
  an empty line instead of Cancel, and the window is made 300 pixels wide:
    pgprompt ask WIDTH=300 MESSAGE="Do you wish to  continue:" \
    BUTTONS="Continue Cancel" CANCELBUTTON="Cancel" CANCEL="" TITLE="continue?"
      
  this is a more complex example - it shows a list of choices as well as 
  a few buttons. One of the buttons is specified as an OK button, which means
  that clicking on it produces the value selected from the list. Another is
  specified as a cancel button, which produces the CANCEL value. Others will
  produce the text of the button name. 
  This example also shows how to specify items with spaces in them by 
  specifying an alternate separator(/), and also selects two items:
    pgprompt list LISTSELECTION="multiple"  BUTTONS="Select Random None" \
    LIST="anchovies/salami/olives/hot chilli sauce/chicken" \
    MESSAGE="pick an extra topping:" TITLE="Add topping?" OKBUTTON="Select" \
    CANCELBUTTON="None" SELECTED="salami/chicken" LISTSEP="/" 

  A simple vertical row of buttons:
    pgprompt buttons BUTTONS="button 1/button 2/button 3/button 4" \
    BUTTONSEP="/" BUTTONDIR="v" MESSAGE="Pick a button:"
    

  Display a choice of quake 3 mods:
    /bin/ls -d /usr/local/games/quake3/*/ | \
    egrep -vi '/(tools|help|docs|demos|demoq3)/$' | xargs -n 1 basename | \
    pgprompt list %LIST="-" WIDTH=200 HEIGHT=300 MESSAGE="Select a mod:" \
    TITLE="Quake III Arena" SELECTED="uie"

  or you could run this to show you the subjects of new mail messages:
    pgprompt list BUTTONS="OK" WIDTH=500 MESSAGE="You have new mail." 
    LIST="grep '^Subject: ' /var/spool/mail/$USER | cut -c 10- |" 


pgprompt version 1.9
usage: pgprompt [version|help|list|buttons|text|file|filebox|tell|ask] 
specify -h on the command line for more detailed help

  The first and only argument specifies the type of prompt and is one of:
    list: specifies that a list should be shown with a list of items.
    combo: specifies that a list of items is to be shown in a drop-down box.
    buttons: specifies that a list of buttons is to be shown.
    tell: shows a message with an OK button.
    ask: shows a message with OK and cancel buttons.
    field: specifies that single line field should be shown.
    text: specifies that a text box should be shown.
    file: shows a file selection dialog
    filebox: shows a text entry field for entering a file path, with a 
             'Browse ...' button which shows a file selection dialog.
    
The general idea is that a window is shown prompting the user with buttons,
a list or drop down box, or a text entry field. When the user finishes, their
selection is printed to standard output, unless the COMMAND option is set, in
which case the specified command is run with the users selection appended.

  other details are specified by the parameter=value pairs where the parameters
  are one of:
    LIST,BUTTONS,
    MESSAGE,TITLE,
    CANCEL,SELECTED,
    CANCELBUTTON,OKBUTTON,
    WIDTH,HEIGHT,CENTERED,
    LISTSELECTION,BUTTONDIR,
    BUTTONSEP,LISTSEP,COMMAND,COMMANDSEP,TEXTREADONLY,CURSORPOS
    BUTTONNUM,LISTNUM,SAVETEXT

  if MESSAGE is set, a label will be shown with that message
  if BUTTONS is set, it specifies the names (text) of buttons to be shown
          if it is specified just as "-", standard input will be read for a 
            list of button names, one per line.
          if it ends in |, it is taken as a command to generate button names,
          one per line. see the examples below.
          if any button is just '%', a separator will be used instead - use
          '%%' if you actually want a button with '%' on it.
  if BUTTONNUM is set, if the user selects a button other than the cancel 
     button, it's  number is returned instead of it's name.
  if LISTNUM is set, if the user selects a list item, it's number is returned 
     instead of it's text.
  if CANCEL is set, then that value will be returned if the user presses 
     the cancel button or closes the window
  if CANCELBUTTON is set, it specifies which button to as the cancel button
  if OKBUTTON is set, it specifies which button is used as the OK button
  if TITLE is set, it specifies the the window title
  if LIST is set, it specifies items for list or combo mode
          if it is specified just as "-", standard input will be read for a 
            list of items, one item per line.
          if it ends in |, it is taken as a command to generate a list of 
            items, one per line. see the examples below.
  if SELECTED is set, it pre-selects a combo or list item or items, or sets
     the text or file name for the text,field, and file modes.
  if LISTSELECTION is one of multiple,single,or browse
      single means zero or one item can be selected.
      browse is like single except there is at least one item selected.
      multiple means zero or more items can be selected.
  if CLOSEVALUE is set, it sets the value to use if the window is just closed.
      The default is to do whatever the cancel value is.
  if both WIDTH and HEIGHT are set, they specify the minimum window size.
  if NOTCENTERED is set, the window will be not be automatically centered
  if BUTTONDIR is set to h or v, it changes the direction buttons are laid out.
  if BUTTONSEP is set it specifies what character is used to separate button 
     names in the BUTTONS list. See the examples below.
  if LISTSEP is set, it specifies what character is used to separate items in
     the LIST and SELECTED lists. See the examples below.
  if COMMAND is specified, it specifies a command to run with the 
     results appended
  if COMMANDSEP is specified, it specifies a different character to split 
     the COMMAND option with (the default is ' ').
  if CURSORPOS is set, and the mode is 'text' it specifies the cursor position 
     within the text field - in characters from the start of the text.
  if SAVETEXT is set, and the mode is 'text', then no matter what the result,
     ie whatever button the user presses, the text from the text field will be
     saved to the file specified by this parameter. If you specify this, and
     the user clicks on your OK button, the OK button will still just return OK.
     If you do not specify this, then the OK button prints the text from the 
     text box.

  Note, for parameters that just need to be set or not, like NOTCENTERED,
  you cant just specify NOTCENTERED or NOTCENTERED=  on the command line - you 
  need to use NOTCENTERED=1
  

  ********************************************************
  here are some examples:
  ********************************************************
  Note these examples have been spread over multiple lines for readability.

  pop up a message 300 pixels wide in the center of the screen:
    pgprompt tell MESSAGE="You have new mail." TITLE="New Mail" WIDTH=300 
    

  ask the user for an item from a list, shown in a drop-down box:
    pgprompt list MESSAGE="pick a value:" LIST="a b c d" 

  a simple yes/no dialog:
    pgprompt ask MESSAGE="click OK to continue:" TITLE="continue?" 

  the same dialog, but with custom buttons, and set so that cancelling prints
  an empty line instead of Cancel, and the window is made 300 pixels wide:
    pgprompt ask WIDTH=300 MESSAGE="Do you wish to  continue:" \\
    BUTTONS="Continue Cancel" CANCELBUTTON="Cancel" CANCEL="" TITLE="continue?"
      
  this is a more complex example - it shows a list of choices as well as 
  a few buttons. One of the buttons is specified as an OK button, which means
  that clicking on it produces the value selected from the list. Another is
  specified as a cancel button, which produces the CANCEL value. Others will
  produce the text of the button name. 
  This example also shows how to specify items with spaces in them by 
  specifying an alternate separator(/), and also selects two items:
    pgprompt list LISTSELECTION="multiple"  BUTTONS="Select Random None" \\
    LIST="anchovies/salami/olives/hot chilli sauce/chicken" \\
    MESSAGE="pick an extra topping:" TITLE="Add topping?" OKBUTTON="Select" \\
    CANCELBUTTON="None" SELECTED="salami/chicken" LISTSEP="/" 

  A simple vertical row of buttons:
    pgprompt buttons BUTTONS="button 1/button 2/button 3/button 4" \\
    BUTTONSEP="/" BUTTONDIR="v" MESSAGE="Pick a button:"
    

  Display a choice of quake 3 mods:
    /bin/ls -d /usr/local/games/quake3/*/ | \\
    egrep -vi '/(tools|help|docs|demos|demoq3)/\$' | xargs -n 1 basename | \\
    pgprompt list LIST="-" WIDTH=200 HEIGHT=300 MESSAGE="Select a mod:" \\
    TITLE="Quake III Arena" SELECTED="uie"

  or you could run this to show you the subjects of new mail messages:
    pgprompt list BUTTONS="OK" WIDTH=500 MESSAGE="You have new mail." 
    LIST="grep '^Subject: ' /var/spool/mail/\$USER | cut -c 10- |" 

Changelog

  • v1.9:
    - fixed 'filebox' to work with Gtk2 (window->position(-mouse) doesnt work any more) - everything else is working now.
  • v1.8:
    - made Gtk2 version - some small bits still aren't working
  • v1.7:
    - added the tail option
    - improved the short usage (printed if the program is run with no args)
    - improved the parsing of parameters so now any boolean parameter can be set or unset in multiple ways, eg NOTCENTERED CENTERED=n etc
    - moved most of the descriptions of parameters out of the usage string for easier maintenance
  • v1.6:
    - made it so that hitting escape does the cancel action (if one was specified) and hitting return does the ok action (if editing only a single line of text ) - also added option OKPREFIX to make the OK buttons also precede the output with the button name
  • v1.5:
    - Added the MONOSPACED and FONT options
  • v1.4:
    - Added the ability to put separators among the buttons using '%'
    - FFixed bug when list is is empty and LISTSELECTION is browse