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()
- SpssClient.GetScriptContext returns an SpssScriptContext object that provides values for use by the autoscript.
- The GetOutputItem method of the SpssScriptContext object returns the output item that triggered the current autoscript--in this example, the pivot table whose rows and columns are to be transposed.
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()
- When a script is not run as an autoscript, the GetScriptContext method will return a value of None.
- 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.