The scriptContext Object

Detecting When a Script is Run as an Autoscript

Using the scriptContext object, you can detect when a script is being run as an autoscript. This allows you to code a script so that it functions in either context (autoscript or not). This trivial script illustrates the approach.

Sub Main
    If scriptContext Is Nothing Then
     MsgBox "I'm not an autoscript"
    Else
     MsgBox "I'm an autoscript"
    End If
End Sub
  • When a script is not run as an autoscript, the scriptContext object will have a value of Nothing.
  • Given the If-Else logic in this example, you would include your autoscript-specific code in the Else clause. Any code that is not to be run in the context of an autoscript would be included in the If clause. Of course you can also include code that is to be run in either context.

Getting Values Required by Autoscripts

The scriptContext object provides access to values required by an autoscript, such as the output item that triggered the current autoscript.

  • The scriptContext.GetOutputItem method returns the output item (an ISpssItem object) that triggered the current autoscript.
  • The scriptContext.GetOutputDoc method returns the output document (an ISpssOutputDoc object) associated with the current autoscript.
  • The scriptContext.GetOutputItemIndex method returns the index, in the associated output document, of the output item that triggered the current autoscript.

Note: The object returned by scriptContext.GetOutputItem is not activated. If your script requires an activated object, you'll need to activate it--for instance, with the ActivateTable method. When you're finished with any manipulations, call the Deactivate method.