Running Scripts

Tcl/Tk scripts can be run in several ways.

  • From the File menu, using the Run > Tcl Script option
  • From the Standard toolbar, using the Run Tcl Script button
  • From a button on the Utility menu
  • From a command file using *evaltclscript()
  • Starting HyperWorks Desktop using the –tcl <filename> option
  • Starting HyperWorks Desktop Batch using hmbatch –tcl <filename>
To run a Tcl/Tk script from the command window, use the source command along with the script filename.
source test.tcl
Note that the source command comes in two variants. The first is the Tcl command source, using a lower-case "s". This command requires the full path. If no path is specified, HyperWorks Desktop will look in the current working directory, which can be determined using the pwd Tcl command. The second is the HWT command Source, using an upper-case "s". This command causes HyperWorks Desktop to search through its pre-set script directories, defined with the TCL_INCLUDE environment variable, to look for the script. Thus, to search for the script called test.tcl and run the first found instance of it:
::hwt::Source test.tcl
To run test.tcl from a specific location:
source C:/My_scripts/test.tcl
Also note that Tcl/Tk requires the use of the forward slash / for the pathname separator character, as the backward slash \ is a special reserved character. As an example:
source C:/My_scripts/test.tcl
instead of
source C:\My_scripts\test.tcl
It is also necessary to use quotes " " or curly braces { } to encapsulate pathnames containing spaces:
source "C:/Documents and Settings/Paul/My Documents/test.tcl"
or
source {C:/Documents and Settings/Paul/My Documents/test.tcl}
A button can be created on the User page of the Utility menu to run a Tcl/Tk script. This is done by modifying or creating the userpage.mac file. An example is:
*createbutton(5, "My Script",0,0,10,BLUE,"",EvalTclScript,
"C:/My_scripts/my_script.tcl")

*beginmacro("EvalTclScript")
*evaltclscript($1,0)
*endmacro()
A Tcl/Tk script may also be run from within a command file by using the *evaltclscript command. When the command file is run, the script is executed. This is useful when running a Tcl/Tk script upon launching HyperWorks Desktop or in batch mode. As an example, to run the script my_script.tcl, in the command file you would use:
*evaltclscript("C:/My_scripts/my_script.tcl",0)

A command file may also be run by invoking HyperWorks Desktop using the –tcl<filename> option. This option launches a HyperWorks Desktop session and executes the script specified by <filename>. If *quit appears in the script HyperWorks Desktop will exit, otherwise HyperWorks Desktop will remain open.

The final option is to invoke HyperWorks Desktop in batch mode using hmbatch –tcl<filename>. HyperWorks Desktop batch mode does not launch a graphical display so any commands in the script that require a display are ignored or will generate an error. Once the script is complete, HyperWorks Desktop will exit, regardless of the existence of *quit in the script.