This section describes several options you have for incorporating scripts you've written in to SpecTcl either at startup or while SpecTcl is running. In this section I'm going to assume that you have a file that contains the scripts you want to incorporate, and that you will be using the folder GUI. I am also going to assume you are starting from a SpecTcl that comes directly from the skeleton directories, rather than an application specific SpecTcl.
Let's start by looking at the various ways you can load script files into a runnig SpecTcl.
The methods I will describe all eventually devolve to executing the Tcl source command. You can, of course execute this command manually either at the TkCon window or by typing to the terminal window from which you started SpecTcl.
The form fo the source command is simply:
where filename is the name of the file you want interpreted. There are no restrictions on the form of filename, however by convention, Tcl script file end either in .tcl or .tk although the latter imply that the script contains or manipulates GUI elements.The TkCon component that SpecTcl starts as it initializes (well that's a partial lie, more about this later), includes a -> menu option that allows you to graphically select a Tcl script file and load it.
Similarly, the Folder GUI menu allows you to source Tcl files. The -> menu command sources the Tcl file, displays any errors and subsequently executes updates the Folder GUI and refreshes the object browser tree. While this is intended for restoring setup files saved by the GUI, you can actually use it to source arbitrary Tcl scripts. If your script creates, modifies or deletes objects the browser or Xamine display, you should consider using -> to source your scripts.
The Folder GUI -> is intended to allow you to source arbitrary Tcl/Tk scripts. No special processing follows the execution of script sourced via ->.
Sometimes, when you write scripts for SpecTcl you don't want to have to worry about manually executing them. For example, if you have defined procs that are used to extend the SpecTcl command language or if you have built your own application specific GUI or added GUI elements to existing windows, you want these to be available from the beginning of a SpecTcl run.
When SpecTcl starts, it runs two sets of initialization scripts before taking commands from stdin, and TkCon. SpecTcl assumes that for each of these scripts, there may be
System wide versions of the script.
User specific versions of the script (located in the user's home directory).
Project specific versions of the script (where a project is defined by the current working directory at the time SpecTcl starts).
SpecTcl looks for and runs each of these scripts, if they exist, in the order shown above. All scripts found are run.
The first script is called SpecTclInit.tcl. This is run very early in SpecTcl's startup and should not be used for much more than setting variables and defining procs. We have already run across this script in: SpecTcl Initialization where I described the variables that SpecTcl itself may use from this file. Theres' nothing to stop you from using this file to define additional commands (via proc) and variables used later by your application specific code. This script is run prior to the registration of the standard SpecTcl commands.
The second script is called SpecTclRC.tcl. It is run just after SpecTcl is completely initialized, but before control is returned to the Tcl interpreter. All SpecTcl commands are available as are all commands you may have defined in your C++ code assuming you followed the rules for defining them in Programming SpecTcl below.
This script is normally used to setup the GUI, and define other command procs. A sample of this script is included in the SpecTcl Skel directory and can be edited to add your own Tcl code, or to remove code you don't want.
SpecTclRC.tcl is run prior to Tcl's initialization of its stderr channel. This means that if your script has errors it will just stop running without any error messages. To deal with this restriction, I suggest the following development process:
When developing new functionality to be incorporated in SpecTclRC.tcl, develop it in a separate script file rather than simply editing it into SpecTclRC.tcl.
When developing these new script files, first debug them by manually sourcing them into SpecTcl. This allows you to see errors detected when the script is read.
Once the script is debugged, add the appropriate source command to the end of your SpecTclRC.tcl script.
Lastly, you can use input redirection to incorporate scripts into SpecTcl. This is especially useful if you allow the default SpecTclRC.tcl to start the TkCon console. Simply start up SpecTcl using the command:
SpecTcl < yourscriptfile
Where yourscriptfile is a script that adds your functionality to SpecTcl.