Executing Scripts within other Scripts


There are some situations where you will want to execute a script from within another script. This could be to combine multiple scripts into one single action call (from a button, link, or image inside a Text Area), or it could be to enable the execution of various scripts based off some logic checks inside a master script.


The following script will execute another script (name ‘secondScript’ that already exists and is stored inside the same Spotfire Analysis file):

from System.Collections.Generic import Dictionary
from Spotfire.Dxp.Application.Scripting import ScriptDefinition
import clr
scriptDef = clr.Reference[ScriptDefinition]()
#The ExecuteScript method takes in the script code as a string parameter, so we must look the script up and output its code using the ScriptCode property
Document.ScriptManager.TryGetScript("secondScript", scriptDef)
params = Dictionary[str, object]()
Document.ScriptManager.ExecuteScript(scriptDef.ScriptCode, params)

The code above assumes that the script, secondScript, has no parameters being passed into it. Let’s assume that we have another script, called thirdScript, which takes in one string parameter, named “phrase”. We will need to update our code to pass in that parameter, as shown below:

from System.Collections.Generic import Dictionary
from Spotfire.Dxp.Application.Scripting import ScriptDefinition
import clr
scriptDef = clr.Reference[ScriptDefinition]()
Document.ScriptManager.TryGetScript("thirdScript", scriptDef)
paramDict = {"phrase":"Hey there"}
params = Dictionary[str, object](paramDict)
Document.ScriptManager.ExecuteScript(scriptDef.ScriptCode, params)