Tclkit: A Tiny Full Featured Scripting Language


Tcl is a full featured script programming language with a small footprint. It is available on many platforms. Tcl’s syntax looks a bit odd if you are coming from a C-like language. But in reality, it is pretty simple … Tcl is a small language and you’ll learn it fast.

Tk is a cross-platform GUI system that has been built for Tcl. Together, they form Tcl/Tk. Tk is used with many other languages (Perl, Python, Ruby, …) too.

Tclkit and Tclkitsh

Tclkit is Tcl and Tk and several libraries altogether put into one single executable. No installation is needed.

For Windows, there is also available tclkitsh.exe which contains only the command line version of Tcl and libraries as one single executable. It is among the first ten things I put onto a new computer.

Why I Use Tclkit

  • Deployment nearly can’t be easier. You just have to copy the single tclkit or tclkitsh executable and your script file. I’ve used this method with my zip speed test program. You can download tclkitsh.exe from there.

  • It is tiny. Tclkitsh.exe 8.5.9 is only 740 kB. Tclkit.exe,
    which contains the full Tk GUI is only 1.3 MB

  • Development with tclkit is fast. No compile-run cycles.
  • The GUI system is easy to handle. You don’t even need any visual
    tool to create your GUI.
  • The windows cmd shell language is just terrible. That beast
    cannot even be called a language. If you don’t want to dive
    into Pwershell, Tclkit is a simple and perfect
    replacement for the windows cmd shell.
  • Compared to Perl and Python, it is easier to learn,
    read and understand.
  • It is perfect for doing file operations in your build process.
  • It is perfect for controlling other executables,
    e.g. in your build process. In fact, Tcl has been built
    with the main target to be a language to control other executables.

Example 1: Check If Drive Is Available

I do my backups onto an external USB drive. The backup program is controlled via the Windows Task Scheduler. For security reasons, this drive is usually not connected. This means, before the backup program can do its work, I have to manually connect the drive.

I’ve written a small Tcl/Tk program to check if the drive is available and to inform me that I should connect it if it’s not.

This is the Tcl/Tk Script. With graphical interface and bells.

proc drives {} { foreach drive [list a b c d e f g h i j k l m n o p q r s t u v x y z] { if {[catch {file stat ${drive}: dummy}] == 0} { lappend drives $drive } } return $drives } proc checkDrive {} { if { [lsearch [drives] z] != -1 } exit else { bell; bell } } set a "The daily backup is about to be done. " append a "Please connect the backup drive, make it " append a "available as network drive Z: and click Ok." checkDrive # Create a label or message called .m message .m -textvariable a -width 250 # Create a button called .hello # strange: the width of the button is in another dimension # than that of the message. button .hello -text "Ok" -command { checkDrive } -default active -width 15 # This binds the return key to the pressing of the .hello button bind . <Return> {.hello invoke} # Pack the message and the button together. pack .m .hello -padx 5 -pady 5 # Show the main window wm deiconify .

When this tiny script is run, it checks if drive Z: is connected and if not, it shows the following message box. When the Ok button is hit, it checks again.


Example 2: With SVN, Show All Changed Files

I am working on a system with svn as version control since some days. I have not yet found a fine graphical svn client, aside of TkCVS. (Yes, TkCVS handles svn quite well, despite its name.)

One of my first problems was to find out which files are changed locally in several unconnected checked out directories. A task for which tclkitsh is suited very well.

proc isEmptyDirectory { path } { set isED false if { [file isdirectory $path] } { set content [glob -nocomplain $path/*] if { [llength $content] == 0 } { set isED true } } return $isED } proc handleEntry { dir entry } { set path [file join $dir [string range $entry 8 end]] if { ! [regexp -nocase {( bin|/bin| obj|/obj|.user|.suo)$} $entry] && ! [isEmptyDirectory $path] } { puts $entry } } proc statDir { dir } { cd $dir puts "" puts $dir if [catch { set l [exec svn status . | findstr /V Pics ] } ] { # above, I'm piping the output of svn status . through # findstr. Just as an example. puts "" } else { set l [string map { /} $l] set l [split $l "n"] foreach s $l { handleEntry $dir $s } } } while { 1 } { statDir C:/code/Client/WebClient statDir C:/code/Client/ServiceLayer statDir D:/src/TraceOfDeathApp statDir E:/src/AspServer puts "Press Return key to repeat." gets stdin }

2 thoughts on “Tclkit: A Tiny Full Featured Scripting Language

  1. Pingback: Unit Tests, Unicode and Special URI Characters | 99 Developer Tools

  2. Pingback: Personal Backup Scripting Example | 99 Developer Tools

Comments are closed.