Getting Started with Autoscripts in Python

Autoscripts are scripts that run automatically when triggered by the creation of specific pieces of output from selected procedures and typically require a reference to the object that triggered the script. They may also require a reference to the associated output document and possibly the index of the output item in the output document. These values are obtained from the SpssScriptContext object, as shown in this example of an autoscript that transposes the rows and columns of a pivot table.

import SpssClient
SpssClient.StartClient()

SpssScriptContext = SpssClient.GetScriptContext()
SpssOutputItem = SpssScriptContext.GetOutputItem()
SpssPivotTable = SpssOutputItem.GetSpecificType()
SpssPivotMgr = SpssPivotTable.PivotManager()
SpssPivotMgr.TransposeRowsWithColumns()

SpssClient.StopClient()

Although not used in this example, the GetOutputDoc method of the SpssScriptContext object returns the associated output document, and the GetOutputItemIndex method returns the index (in the associated output document) of the output item that triggered the autoscript.

General information on autoscripts is available from Core System>Scripting Facility in the Help system.

Detecting When a Script is Run as an Autoscript

Using the GetScriptContext method, 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.

import SpssClient
SpssClient.StartClient()

SpssScriptContext = SpssClient.GetScriptContext()
if SpssScriptContext == None:
   print "I'm not an autoscript"
else:
   print "I'm an autoscript"

SpssClient.StopClient()