Add GTK GUI frontends to a command line program in minutes
Posted: Wed 12 Aug 2009, 04:04
Here is a little template I put together adding GTK GUI frontends to command line programs.
Just use the individual parts (as many times as needed - just copy/paste and increment the numbers or comment out unneeded sections) to collect the required data in the GUI and pass it to the command line.
Just use the individual parts (as many times as needed - just copy/paste and increment the numbers or comment out unneeded sections) to collect the required data in the GUI and pass it to the command line.
Code: Select all
#! /bin/bash
export CHOOSER="
<window title=\"YOURPROGRAMNAME \"icon-name=\"gtk-cdrom\">
<vbox>
<frame Select your file>
<hbox>
<entry editable=\"true\" accept=\"filename\">
<variable>FILE1</variable>
</entry>
<button>
<input file stock=\"gtk-file\"></input>
<variable>FILE_BROWSE_FILENAME</variable>
<visible>enabled</visible>
<action type=\"fileselect\">FILE1</action>
</button>
</hbox>
</frame>
<frame Select a directory>
<hbox>
<entry editable=\"true\" accept=\"directory\">
<variable>DIR1</variable>
</entry>
<button>
<input file stock=\"gtk-open\"></input>
<variable>FILE_BROWSE_DIRECTORY</variable>
<action type=\"fileselect\">DIR1</action>
</button>
</hbox>
</frame>
<frame Entry box for user definable strings>
<hbox>
<entry editable=\"true\">
<variable>WORD1</variable>
<default> default text here </default>
</entry>
</hbox>
</frame>
<frame choose any or all:>
<hbox>
<checkbox>
<label>Check the box if you want to ...</label>
<variable>CBOX1</variable>
<default>false</default>
</checkbox>
</hbox>
<hbox>
<checkbox>
<label>Check the box if you want to ...</label>
<variable>CBOX2</variable>
<default>true</default>
</checkbox>
</hbox>
</frame>
<frame choose one of:>
<combobox>
<variable>COMBO1</variable>
<item>item 1</item>
<item>item 2</item>
</combobox>
</frame>
<frame choose one of:>
<combobox>
<variable>COMBO2</variable>
<item>item 1</item>
<item>item 2</item>
</combobox>
</frame>
<hbox>
<button>
<input file icon=\"gtk-ok\"></input>
<label>OK</label>
<action type=\"exit\">EXIT_OK</action>
</button>
<button>
<input file icon=\"gtk-quit\"></input>
<label>QUIT</label>
<action type=\"exit\">EXIT_NOW</action>
</button>
</hbox>
</vbox>
</window>
"
CHOOSER2==`gtkdialog3 --program=CHOOSER --center`
if [ "`echo $CHOOSER2 | grep EXIT_NOW`" != "" ]; then
exit 0
fi
#Get all of the variables
FILE1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep FILE1 | sed 's/FILE1//'`"
#This is another file example if you need to prepend the flag -f ... it could be any string
#FILE1=" -f ""`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep FILE1 | sed 's/FILE1//'`"
WORD1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep WORD1 | sed 's/WORD1//'`"
DIR1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep DIR1 | sed 's/DIR1//'`"
CBOX1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep CBOX1 | sed 's/CBOX1//'`"
CBOX2="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep CBOX2 | sed 's/CBOX2//'`"
COMBO1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep COMBO1 | sed 's/COMBO1//'`"
COMBO2="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep COMBO2 | sed 's/COMBO2//'`"
if [ $CBOX1 = "true" ];then
CBOX1=" -X " #where -X is the parameter you wish to pass when this checkbox is checked
else
CBOX1="" #setting it back to "null" allows us to pass all parameters without doing more "if thens"
fi
if [ $CBOX2 = "true" ];then
CBOX2=" -X " #where -Y is the parameter you wish to pass when this checkbox is checked
else
CBOX2="" #setting it back to "null" allows us to pass all parameters without doing more "if thens"
fi
#you can add any number of any of these variables
#to debug you can add echo $VARNAME for each variable and run the script from rxvt
#The following is an imaginary example to open an 80x4 terminal and execute program_name
#with cbox flags (if set) using file1 (presumably as input of type combo1)
# and output to a file of type combo2 named word1 in dir1
#rxvt +sb -bg orange -geometry 80x4 -e program_name $CBOX1... $FILE1 $COMBO1 $DIR1/$WORD1 $COMBO2
gxmessage -wrap -name YourProgram "This is a message to tell the user that the program is finished. You can use the variable names outside of the quotes like this: CBOX1 = "$CBOX1", CBOX2 = "$CBOX2", DIR1 = "$DIR1", FILE1 = "$FILE1", WORD1 = "$WORD1", COMBO1 = "$COMBO1", COMBO2 = "$COMBO2" Make sure you put the output in user understandable terms after you have finished the debugging process"
unset CHOOSER