Making a Simple Front-End to the Wget Command Using Korn Shell
In the previous post we made a simple Hello World script using the Common Desktop Environment's Korn shell (dtksh
). This time we're going to make a front-end to the ubiquitous wget
command.
You probably know what wget
is, tl;dr it's a program for retrieving files from the internet. It's present on most Linux and BSD systems.
As the front-end is going to be very simple, we'll need just 6 controls on the main window. A URL entry field, an output path field, two labels to describe them, a button and a checkbox to download into an existing file (the -c
switch).
First we need to tell the shell to use dtksh
:
#!/usr/dt/bin/dtksh
. /usr/dt/lib/dtksh/DtFuncs.dtsh
...and we need to write the functions which will be trigerred by the buttons:
startDownload() {
XmTextFieldGetString URL $URLFIELD
XmTextFieldGetString OUTPATH $OUTFIELD
CMD="wget $URL -O $OUTFIELD"
# append "-c" when "continue downloading" is checked
if XmToggleButtonGetState $DOCONTINUE; then
CMD=$CMD" -c"
fi
reportStatus
}
reportStatus() {
# the following line opens a terminal window and runs the command
/usr/dt/bin/dtterm -title "Retrieve file" -e $CMD
DtkshDisplayInformationDialog "Download a file" \
"Downloading from
$URL
done."
}
The first function (startDownload
) gets the URL and output path and constructs the full command. The if
statement appends -c
to the command when the "continue downloading" checkbox is checked.
The second opens a dtterm
(CDE's terminal emulator) and runs the command. It shows an info window when the download is completed.
We now have to define the UI. As mentioned, we need a window, a control pane in it, two labels and two fields, a toggle button (checkbox) and a button to start the download.
XtInitialize TOPLEVEL wgetcde Dtksh $0
XtSetValues $TOPLEVEL title:"Download a file"
XtCreateManagedWidget BBOARD bboard XmBulletinBoard $TOPLEVEL \
resizePolicy:RESIZE_NONE height:170 width:400
XtCreateManagedWidget LABEL1 label1 XmLabel $BBOARD \
labelString:"File URL:"
XtCreateManagedWidget URLFIELD urlfield XmTextField $BBOARD \
width:380 x:10 y:35
XtCreateManagedWidget LABEL2 label2 XmLabel $BBOARD \
labelString:"Output path (include the filename)" \
x:10 y:70
XtCreateManagedWidget OUTFIELD outfield XmTextField $BBOARD \
width:380 x:10 y:95
XtCreateManagedWidget DOCONTINUE docontinue XmToggleButton $BBOARD \
labelString:"Continue downloading an existing file" \
x:10 y:130
XtCreateManagedWidget BUTTON startbutton XmPushButton $BBOARD \
labelString:"START" \
height:27 width:100 x:290 y:130
We can also put some placeholder values in the text fields to aid the user:
XmTextFieldSetString $OUTFIELD "~/"
XmTextFieldSetString $URLFIELD "https://"
...and we have to bind our startDownload
function to the button:
XtAddCallback $BUTTON activateCallback startDownload
All that's left is displaying the window:
XtRealizeWidget $TOPLEVEL
XtMainLoop
If you try to run the script now, you should get something similar to this:
Here's a quick list of ideas to improve it:
- redirect the output to another window instead of using
dtterm
- display an error / warning popup on errors
- support more download options
- close the main window automatically when the download finishes.
Here's the full script:
#!/usr/dt/bin/dtksh
. /usr/dt/lib/dtksh/DtFuncs.dtsh
startDownload() {
XmTextFieldGetString URL $URLFIELD
XmTextFieldGetString OUTPATH $OUTFIELD
CMD="wget $URL -O $OUTFIELD"
# append "-c" when "continue downloading" is checked
if XmToggleButtonGetState $DOCONTINUE; then
CMD=$CMD" -c"
fi
reportStatus
}
reportStatus() {
# the following line opens a terminal window and runs the command
/usr/dt/bin/dtterm -title "Retrieve file" -e $CMD
DtkshDisplayInformationDialog "Download a file" \
"Downloading from
$URL
done."
}
####################################################################
XtInitialize TOPLEVEL wgetcde Dtksh $0
XtSetValues $TOPLEVEL title:"Download a file"
XtCreateManagedWidget BBOARD bboard XmBulletinBoard $TOPLEVEL \
resizePolicy:RESIZE_NONE height:170 width:400
XtCreateManagedWidget LABEL1 label1 XmLabel $BBOARD \
labelString:"File URL:"
XtCreateManagedWidget URLFIELD urlfield XmTextField $BBOARD \
width:380 x:10 y:35
XtCreateManagedWidget LABEL2 label2 XmLabel $BBOARD \
labelString:"Output path (include the filename)" \
x:10 y:70
XtCreateManagedWidget OUTFIELD outfield XmTextField $BBOARD \
width:380 x:10 y:95
XtCreateManagedWidget DOCONTINUE docontinue XmToggleButton $BBOARD \
labelString:"Continue downloading an existing file" \
x:10 y:130
XtCreateManagedWidget BUTTON startbutton XmPushButton $BBOARD \
labelString:"START" \
height:27 width:100 x:290 y:130
####################################################################
XmTextFieldSetString $OUTFIELD "~/"
XmTextFieldSetString $URLFIELD "http://"
XtAddCallback $BUTTON activateCallback startDownload
####################################################################
XtRealizeWidget $TOPLEVEL
XtMainLoop