Navigation Logo 1.3  Direct Execution Navigation Logo

 

 

By direct execution, I mean execution of a script file as if it were any other program known to your operating system. Your script is still interpreted but you do not fire the interpreter up first and interact with it. Instead, you start the script by telling the operating system to execute it. The operating system then figures out that execution requires that an interpreter be fired up to do the actual work.

With direct execution, there is no command window. Standard input and output will be through the window where the script is executed, if any. Under Windows 95, Tcl will provide a new DOS window for this purpose, if necessary.

Unix

From the first, Unix has had an elegant mechanism for executing scripts directly. The mechanism requires two preparatory actions:

  1. The file containing the script must be declared executable with Unix's chmod command. On the Unix command line, enter
    chmod +x script
    
    where script names the file with your script.

  2. The file containing the script must begin with the two characters #!. On the same first line as these characters, the pathname of the appropriate interpreter is listed. This will be the tclsh?? interpreter for Tcl or the wish?? interpreter for Tk.

When you have taken these two preliminary steps for a file script you can execute script with a normal Unix command line, for example,

script ARG1 ARG2 ... ARGn
where ARG1, ARG2, ... ARGn represent arguments passed to the script.

Windows 95

Scripts can be executed directly in the way Windows 95 expects programs to execute – by clicking on them. To arrange for this you need to give your script files a distinctive extension, such as tcl or tk, and to tell Windows about your extension.

To tell Windows about your extension, do the following:

  1. From the Explorer click on "View," then "Options," then "File Types," and finally "New Type."

  2. Now you have to enter a description such as "Tcl Script" and an extension such as "tcl."

  3. At the bottom of this window is an "Action" subwindow. Under that is a "New" button. Click on it.

  4. Now you have another window that wants "Action" and "Application" infomation. Enter "open" as the action because "open" is the action Windows will use when you double-click on a file of the designated extension. You also need to enter an application for Windows to execute when it does the "open" action. For Tcl on my computer, I entered
    c:\progra~1\tcl\bin\tclsh80.exe "%1"
    

  5. Now back out of the data entry windows to the Explorer and double click a file with the designated extension as a test.

If the file type already exists, you can look for it with the scrollbar window that you see after clicking "File Types." Then click on "Edit" and change the windows I have described above as necessary.

When Tcl or Tk is executed this way, it is called "direct execution." Tcl brings up a DOS window that is used for standard input and output – this is not a command window. Tk brings up a root window but not a command window. With either Tcl or Tk, commands are taken from the script file you double click on. If that script begins with the first line shown above for Unix usage, it is OK. Tcl/Tk sees any line beginning with a # as a comment and ignores it.

Command Line Execution in Windows 95

If you execute Tcl as just described, you may find the DOS window disappears much too soon. This problem can be fixed with an extra input statement at the end of your scripts – if your script catches all possible errors.

Another fix is to execute your scripts from a batch file, tcl.bat, whose contents are something like:

echo off
c:\progra~1\tcl\bin\tclsh80.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

The advantage of the batch file is that it creates the DOS Window from which your script is executed – you will have good control over how long that window lasts. A disadvantage is that your scripts are limited to accepting eight or fewer arguments. That probably will not fret you.

If you drag tcl.bat from the Explorer to your desktop, Windows will create a shortcut to tcl.bat. After the shortcut is created, right click on the shortcut symbol, then on "Properties," and finally on the "Program" tab. Edit the "Cmd Line" field by adding a space and then a question mark to the end of the line. Enter the directory where you keep your Tcl scripts into the "Working" field. At the bottom of the dialog box, make sure there is no check mark in the "Close on exit" square. Exit the Properties window.

Now when you click on the shortcut you will get a window into which you can enter a command line for executing a Tcl script. This window pops up because you used a question mark as an argument in the "Cmd Line" field.

Remark

The arguments you enter into this window can be a bit confusing. In one sense, they are DOS arguments – it is DOS that will determine exactly what each argument is. This means you enter "Hellow Orld" and not {Hellow Orld}. In another sense, the contents of these arguments will be interpreted by Tcl, not DOS. This means you enter C:/Dat/Whatever rather than C:\Dat\Whatever.

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.