Using Tcl Scripts in More than One Callback Function

Sometimes, the same Tcl procedures need to be used in several callback functions. For not having to maintain identical code at different locations, you can place it in a file which is sourced when the Content Manager is started. For this, proceed as follows:

  1. Place a file containing the procedure into a directory whose content is automatically sourced when the Content Manager is started, for example

    instance/myInstance/script/cm/serverCmds/callbackCommand.tcl

  2. All the arguments passed to the callback function or functions should also be passed to the procedure. Furthermore, the procedure is required to return the values that are relevant to the calling functions. The following code is a sample completion check:

    proc callbackCommand {contentId} {
      set messages {"Message"}
      set result 0
      return "$messages $result"
    }
  3. Finally, the procedure needs to be registered in the safe interpreter:

    safeInterp alias callbackCommand callbackCommand
  4. Restart the Content Manager for the script file to be sourced and the procedure to be made available:

    instance/myInstance/bin/rc.npsd restart CM
  5. You can now call the procedure in the functions concerned and make use of the return value in the desired way:

    CM> objClass withName document get completionCheck
    set returnValue [callbackCommand $contentId]
    set messages [lindex $returnValue 0]
    set result [lindex $returnValue 1]
    

If a procedure is called from within functions that expect different return values, make the procedure return a list containing all the required return values. In the calling functions extract the required value from the list using lindex.
If the functions calling the procedure need to pass different arguments to the procedure, use an additional argument that identifies the function from which the procedure is called. Then make the calculations in the procedure dependent on the value of this additional argument.