This chapter explains how to run Tcl and Tk on different operating system platforms: UNIX, Windows, and Macintosh. Tcl commands discussed are: source
, console
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 Macintosh, the details about getting started are different for each system. If you are looking for a current version of Tcl/Tk, use the CD-ROM or check the URLs listed in the Preface on page liv.
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 each of the 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 wish8.0, wish83.exe, or Wish 8.4. The differences among versions are introduced on page lii, and described in more detail in Part VII of the book. This book will use wish to refer to all of these possibilities.
Tk adds Tcl commands that are used to create graphical user interfaces, and Tk 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 discussed 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 standalone Tcl/Tk scripts that are self-contained applications.
It is a good idea to try out the examples in this book as you read along. The highlighted examples from the book are on the CD-ROM in the exsource
folder. You can edit these scripts in your favorite editor. Save your examples to a file and then execute them with the Tcl source
command:
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!
On UNIX you can create a standalone 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:
The Tk hello world program from Chapter 23 is shown in Example 2-2:
Example 2-2. A standalone Tk script on UNIX
#!/usr/local/bin/wish button .hello -text Hello -command {puts "Hello, World!"} pack .hello -padx 10 -pady 10
The actual pathnames for tclsh and wish may be different on your system. If you type the pathname for the interpreter wrong, you receive 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 appears on my system:
info nameofexecutable
=> /home/welch/install/linux-ix86/bin/tclsh8.4
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 older versions of 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 16. In the example, the exec
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
. The exec
command (in /bin/sh
) replaces the current program, so that is all that the Bourne shell processes; Tcl interprets the rest of the script.
Example 2-3. 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 the following:
#!/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.
You can hide more than one Bourne shell command in a script with this trick. For example, you might need to set environment variables:
#!/bin/sh # export LD_LIBRARY_PATH=/usr/local/lib # exec /usr/local/bin/tclsh "$0" ${1+"$@"}
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 FilesTcl84wish84.exe" "c:My Filesscript.tcl"
This starts c:My Filesscript.tcl
as a standalone Tcl/Tk program.
If you want to create a self-contained Tcl/Tk application on Macintosh OS 8 or 9, 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:
First, make a copy of Wish and open the copy in ResEdit.
Pull down the Resource
menu and select Create New Resource
operation to make a new TEXT
resource.
ResEdit opens a window and you can type in text. Type in a source
command that names your script:
source "Hard Disk:Tcl/Tk 8.3:Applications:MyScript.tcl"
Set the name of the resource to be tclshrc
. You do this through the Get Resource Info
dialog under the Resources
menu in ResEdit.
This sequence of commands is captured in an application called Drag n Drop Tclets, which comes with the Macintosh Tcl distribution. If you drag a Tcl script onto this icon, it will create a copy of Wish and create the tclshrc
text resource that has a source
command that will load that script.
If you have a Macintosh development environment, you can build a version of Wish that has additional resources built right in. You add the resources to the applicationInit.r
file. If a resource contains Tcl code, you use it like this:
If you don't want to edit resources, you can just use the Wish Source
menu to select a script to run.
Mac OS X can run the same Tcl/Tk as Macintosh system 8 or 9. However, the preferred version for Mac OS X is Tcl/Tk Aqua, which uses the native windowing system known as Aqua. There are some differences in the application structure due to the new application framework used when building this variant. Wish checks the Resources/Scripts
directory in its application bundle for a file called AppMain.tcl
, if found it is used as the startup script and the Scripts
folder is added to the auto_path
. This is similar in spirit to the tclshrc
resource described above. Daniel Steffen deserves a great deal of credit for the Tcl/Tk Aqua port and his continued support of the Macintosh platform. He has put together a great distribution that includes many popular extensions, which you can find on the CD-ROM. You can find out more about Tcl/Tk on Macintosh through these URLs:
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
There is an alternate version of this console called TkCon. It is included on the CD-ROM, and you can find current versions on the Internet. TkCon was created by Jeff Hobbs and has lots of nice features. You can use TkCon on Unix systems, too. Some of its features were added to console
in 8.4.
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 can 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 FilesTcl84wish.exe" c:yourscript.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, which is described on page 63, to extract items from it:
set arg1 [lindex $argv 0]
The following script prints its arguments (foreach is described on page 79):
Example 2-4. 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 }
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:
Table 2-1. Wish command line options
Use a new private colormap. See page 624. | |
Use the specified X | |
The size and position of the window. See page 658. | |
Specify the Tk application name. See page 648. | |
Run X synchronously. UNIX only. | |
Use the window specified by | |
Specify the visual for the main window. See page 624. | |
| Terminate options to wish. |
Table 2-2. Variables defined by tclsh and wish
The number of command-line arguments. | |
| A list of the command-line arguments. |
| The name of the script being executed. If being used interactively, |
The list of arguments in the | |
An array of the environment variables. See page 124. | |
True (one) if the tclsh is prompting for commands. | |
| The script library directory. |
Modified version number, e.g., 8.0b1. | |
Array containing operating system information. See page 192. | |
If defined, this is a command that outputs the prompt. | |
| If defined, this is a command that outputs the prompt if the current command is not yet complete. |
Version number. | |
| The search path for script library directories. See page 172. |
A map from command name to a Tcl command that defines it. | |
| If set, the library facility is disabled. |
| If set, the auto execute facility is disabled. |
| (wish only). The value of the |
18.117.196.217