Purpose: to provide guidelines, tips, techniques, etc. on writing Tcl/Tk/etc. scripts that wrap around an existing command line oriented application.

By wrap, I mean that the resulting script provides some additional ease of use or functionality over the original program, and that the user typically has minimal knowledge that the original application even exists.

Common examples are the creation of a GUI interface for a command-line application [L1 ], and adding Tcl/Tk to a C application (as well as the cases which fit both those description).


Many people take advantage of Expect and Tk in writing GUI applications which interact with a character oriented application.

Expect is quite useful when you are wanting to interact with an application that is written to interact with users directly via the terminal. One neat example that the Expect book discusses is wrapping a command like ftp, for instance, and then providing your own hooks for adding new commands - say to gzip or ungzip files on your machine, or to define macros to perform common sets of ftp actions, etc. While this is indeed way cool, CL counsels that, in the twenty-first century, use of vfs or tcllib's ftp client library is a considerably more elegant, robust, portable, and generally maintainable alternative. LV offers, as an alternative view, that he would much prefer to use an existing ftp client and write a _small_ amount of add on code than to write an entire ftp client ;) ...

Tcl and Tk work just fine for writing application that take additional arguments or use the GUI to gather values to then be supplied to the wrapped command via its command line.

The logical place for most newcomers to start would be "How to Use C and Tcl Together" [L2 ]; the inconvenience there is the larger one of reading back-issues of the C/C++ Users Journal.


[Lots more pointers: Fortran pages, wrap references, RE series, ...]


EKB 27 March 2005

I wrap a Tcl/Tk GUI around a command-line program frequently, using the exec function rather than embedding Tcl/Tk in my C/C++ program. This is one of my main reasons for using Tcl/Tk, because it is so easy to do in this language. Also, since I write software for Windows, it has the added advantage of making me feel like a rebel - it's not the way Windows programs are normally developed.

Here are a few quick tips for using exec:

If you want to return control immediately to the calling program, use '&'

 tk_messageBox -message "Before loading"
 exec Notepad.exe
 tk_messageBox -message "After loading"

will pop up "After loading" only after you close Notepad, while

 tk_messageBox -message "Before loading"
 exec Notepad.exe &
 tk_messageBox -message "After loading"

will pop up "After loading" immediately after opening Notepad.

Always wrap in a catch command - this will capture anything sent to stderr by the program

 if [catch {exec MyProg.exe my command line} errors] {
   # Clean up here...
 }

Make sure you're in the proper directory for running the program, or provide a full path

 set currdir [pwd]
 cd "C:/some dir/to run/program/in"
 exec MyProg.exe
 cd $currdir

Category Discussion