[Top] [Prev] [Next] [Bottom]

2

2 Getting Started

This chapter explains how to run Tcl and Tk on different operating system platforms: UNIX, Windows, and Macintosh. Tcl commands: source and info.
This chapter explains how to run Tcl scripts on different computer systems. While you can write Tcl scripts that are portable among UNIX, Windows, and the Macintosh, the details about getting started are different on each system.

The main Tcl/Tk program is wish. Wish stands for windowing shell, and with it you can create graphical applications that run on all these platforms. The name of the program is a little different on UNIX, Windows, and Macintosh systems. On UNIX it is just wish. On Windows you will find wish.exe, and on the Macintosh the application name is Wish. A version number may also be part of the name, such as wish4.1, wish42.exe, or Wish 8.0. The differences among versions are introduced on page xl, and described in more detail in Part VII of the book. This book will just use wish to refer to all of these possibilities.

Tk adds Tcl commands that are used to create graphical user interfaces, and it is described in Part III. You can run Tcl without Tk if you do not need a graphical interface, such as with the CGI script in Chapter 3. In this case the program is tclsh, tclsh.exe or Tclsh.

When you run wish it displays an empty window and prompts for a Tcl command with a % prompt. You can enter Tcl commands interactively and experiment with the examples in this book. On Windows and Macintosh, a console window is used to prompt for Tcl commands. On UNIX, your terminal window is used. As described later, you can also set up stand-alone Tcl/Tk scripts that are self contained applications.

The source Command

You can enter Tcl commands interactively at the % prompt. It is a good idea to try out the examples in this book as you read along. For longer examples you can find them on the CD-ROM and edit the scripts in your favorite editor. Save your examples to a file and then execute them with the Tcl source command:

source filename
The source command reads Tcl commands from a file and evaluates them just as if you had typed them interactively.

Chapter 3 develops a sample application. To get started, just open an editor on a file named cgi1.tcl. Each time you update this file you can save it, reload it into Tcl with the source command, and test it again. Development goes quickly because you do not wait for things to compile!

UNIX Tcl Scripts

On UNIX you can create a stand-alone Tcl or Tcl/Tk script much like an sh or csh script. The trick is in the first line of the file that contains your script. If the first line of a file begins with #!pathname, then UNIX uses pathname as the interpreter for the rest of the script. The "Hello, World!" program from Chapter 1 is repeated in Example 2-1 with the special starting line:

A stand-alone Tcl script on UNIX.
#!/usr/local/bin/tclsh
puts stdout {Hello, World!}
Similarly, the Tk hello world program from Chapter 18 is shown in Example 2-2:

A stand-alone Tk script on UNIX.
#!/usr/local/bin/wish
button .hello -text Hello -command {puts "Hello, World!"}
pack .hello -padx 10 -pady 10
The actual pathname for tclsh and wish may be different on your system. If you get the pathname for the interpreter wrong, you get a confusing "command not found" error. You can find out the complete pathname of the Tcl interpreter with the info nameofexecutable command. This is what I get on my system:

info nameofexecutable
=> /proj/tcl/install/5.x-sparc/bin/tclsh8.0

Watch out for long pathnames.

On most UNIX systems this special first line is limited to 32-characters, including the #!. If the pathname is too long you may end up with /bin/sh trying to interpret your script, giving you syntax errors. You might try using a symbolic link from a short name to the true, long name of the interpreter. However, watch out for systems like Solaris in which the script interpreter cannot be a symbolic link. Fortunately Solaris doesn't impose a 32 character limit on the pathname, so you can just use a long pathname.

The next example shows a trick that works around the pathname length limitation in all cases. The trick comes from a posting to comp.lang.tcl by Kevin Kenny. It takes advantage of a difference between comments in Tcl and the Bourne shell. Tcl comments are described on page 15. In the example, the Bourne shell command that runs the Tcl interpreter is hidden in a comment as far as Tcl is concerned, but it is visible to /bin/sh:

Using /bin/sh to run a Tcl script.
#!/bin/sh
# The backslash makes the next line a comment in Tcl \
exec /some/very/long/path/to/wish "$0" ${1+"$@"}
#	... Tcl script goes here ...
You do not even have to know the complete pathname of tclsh or wish to use this trick. You can just do this:

#!/bin/sh

# Run wish from the users PATH \
exec wish -f "$0" ${1+"$@"}

The drawback of an incomplete pathname is that many sites have different versions of wish and tclsh that correspond to different versions of Tcl and Tk. In addition, some users may not have these programs in their PATH.

If you have Tk version 3.6 or earlier, its version of wish requires a -f argument to make it read the contents of a file. The -f switch is ignored in Tk 4.0 and higher versions. The -f, if required, is also counted in the 32-character limit on #! lines.

#!/usr/local/bin/wish -f

Windows 95 Start Menu

You can add your Tcl/Tk programs to the Windows start menu. The command is the complete name of the wish.exe program and the name of the script. The trick is that the name of wish.exe has a space in it in the default configuration, so you must use quotes. Your start command will look something like this:

"c:\Program Files\TCL76\wish.exe" c:\your\script.tcl
This starts c:\your\script.tcl as a stand-alone Tcl/Tk program.

The Macintosh and ResEdit

If you want to create a self-contained Tcl/Tk application on Macintosh you must copy the Wish program and add a Macintosh resource named tclshrc that has the start-up Tcl code. The Tcl code can be a single source command that reads your script file. Here are step by step instructions to create the resource using ResEdit:

source "Hard Disk:Tcl/Tk 4.1:Applications:MyScript.tcl"
source -rcrc resource
If you don't want to edit resources, you can just use the Wish Source menu to select a script to run.

The console Command

The Windows and Macintosh platforms have a built-in console that is used to enter Tcl commands interactively. You can control this console with the console command. The console is visible by default. Hide the console like this:

console hide
Display the console like this:

console show
The console is implemented by a second Tcl interpreter. You can evaluate Tcl commands in that interpreter with:

console eval command

Command-Line Arguments

If you run a script from the command line, for example from a UNIX shell, you can pass the script command-line arguments. You can also specify these arguments in the shortcut command in Windows. For example, under UNIX you could type this at a shell:

% myscript.tcl arg1 arg2 arg3
In Windows, you can have a shortcut that runs wish on your script and also passes additional arguments:

"c:\Program Files\TCL76\wish.exe" c:\your\script.tcl arg1
The Tcl shells pass the command-line arguments to the script as the value of the argv variable. The number of command-line arguments is given by the argc variable. The name of the program, or script, is not part of argv nor is it counted by argc. Instead, it is put into the argv0 variable. Table 2-2 lists all the predefined variables in the Tcl shells. argv is a list, so you can use the lindex command described in Chapter 5 to extract items from it:

set arg1 [lindex $argv 0]
The following script prints its arguments (foreach is described on page 67):

The EchoArgs script.
# Tcl script to echo command line arguments
puts "Program: $argv0"
puts "Number of arguments: $argc"
set i 0
foreach arg $argv {
	puts "Arg $i: $arg"
	incr i
}

Command-Line Options to Wish

Some command-line options are interpreted by wish, and they do not appear in the argv variable. The general form of the wish command line is:

wish ?options? ?script? ?arg1 arg2?
If no script is specified, then wish just enters an interactive command loop. Table 2-1 lists the options that wish supports:
Wish command line options.
-colormap new Use a new private colormap. See page 456.
-display display Use the specified X display. UNIX only.
-geometry geometry The size and position of the window. See page 488.
-name name Specify the Tk application name. See page 478.
-sync Run X synchronously. UNIX only.
-use id Use the window specified by id for the main window. See page 496.
-visual visual Specify the visual for the main window. See page 456.
-- Terminate options to wish.

Predefined Variables
Variables defined by tclsh and wish.
argc The number of command-line arguments
argv A list of the command-line arguments
argv0 The name of the script being executed. If being used interactively, argv0 is the name of the shell program.
embed_args The list of arguments in the <EMBED> tag. Tcl applets only. See page 607.
env An array of the environment variables. See page 108.
tcl_interactive True (one) if the tclsh is prompting for commands.
tcl_library The script library directory.
tcl_patchLevel Modified version number, e.g., 8.0b1
tcl_platform Array containing operating system information. See page 152.
tcl_pkgPath List of directories to search for packages.
tcl_prompt1 If defined, this is a command that outputs the prompt.
tcl_prompt2 If defined, this is a command that outputs the prompt if the current command is not yet complete.
tcl_version Version number.
auto_path The search path for script library directories. See page 136.
auto_index A map from command name to a Tcl command that defines it.
auto_noload If set, the library facility is disabled.
auto_noexec If set, the auto execute facility is disabled.
geometry (wish only). The value of the -geometry argument.



[Top] [Prev] [Next] [Bottom]

welch@acm.org
Copyright © 1997, Brent Welch. All rights reserved.
This will be published by Prentice Hall as the 2nd Edition of
Practical Programming in Tcl and Tk