Erwin had posted this in another post, so I will add my response here as well. As Diego noted the service object is not always available so it may not be a good idea to rely on it. Here is an example of inlining another script in Python.
from com.ibm.tivoli.maximo.script import ScriptCache
from com.ibm.tivoli.maximo.script import JSR223ScriptDriver
# Note the use of "snake_case" for variable names as is the PEP8 naming convention standard for Python
def get_auto_script(auto_script_name):
# Use the ScriptCache to get the script source so you are not making a database call, which is otherwise expensive
scriptInfo = ScriptCache.getInstance().getScriptInfo(auto_script_name)
# Make sure that the script is Jython / Python. Note that we are comparing the engine name because both Jython and Python use the same engine.
# Also note that getScriptLanguge() is the correct method name and Language is misspelled in the Maximo API
if JSR223ScriptDriver.getSupportedScriptEngineInfo(scriptInfo.getScriptLanguge()).getEngineName() == "jython":
return scriptInfo.getScriptSource()
exec(get_auto_script("EXAMPLE_SCRIPT_NAME"))
print(str(example_function_name(1)))
You can also do this is JavaScript / Nashorn like the following:
var ScriptCache = Java.type("com.ibm.tivoli.maximo.script.ScriptCache");
var JSR223ScriptDriver = Java.type("com.ibm.tivoli.maximo.script.JSR223ScriptDriver");
function getAutoScript(autoScriptName){
// Use the ScriptCache to get the script source so you are not making a database call, which is otherwise expensive
scriptInfo = ScriptCache.getInstance().getScriptInfo(auto_script_name)
// Make sure that the script is Javascript / Nashorn. Note that we are comparing the engine name because all the Javascript language names use the same engine.
// Also note that getScriptLanguge() is the correct method name and Language is misspelled in the Maximo API
if (JSR223ScriptDriver.getSupportedScriptEngineInfo(scriptInfo.getScriptLanguge()).getEngineName() == "Oracle Nashorn"){
return scriptInfo.getScriptSource();
}
}
var scriptName = "EXAMPLE_SCRIPT_NAME"
load({ script: ScriptCache.getInstance().getScriptInfo(scriptName).getScriptSource(), name: scriptName});
Java.type("java.lang.System").out.println(example_function_name(1));
I wrote a post about this a while back you that you might also want to check out.
https://www.sharptree.io/blog/2021/2021-11-29-js-invoke-library/
Regards,
Jason
------------------------------
Jason VenHuizen
Sharptree
https://sharptree.iohttps://opqo.io------------------------------
Original Message:
Sent: Sun March 30, 2025 12:02 PM
From: Diego Visentin
Subject: Define a library of automationscripts that can be used in other automationscripts
Hi @Erwin Hebing
I have been using this approach for a long time because I find it more functional than service.invokeScript() (which is "the" supported one and generic with respect to the scripting language, so it would also work with Groovy, for example).
Warning: the service object is not always available as you might expect. I will write a post about this as soon as possible.
------------------------------
Diego Visentin
Original Message:
Sent: Fri March 28, 2025 03:36 AM
From: Erwin Hebing
Subject: Define a library of automationscripts that can be used in other automationscripts
Why?
We found the other methods complex so we came up with another method to define jython functions that can be used in another jython automationscript
What did we come up with?
An automationscript with a function that retrieves an automationscript and returns the script source
After retrieving the script source we 'exec(ute)' the script source so the function becomes available for use
What do we need?
(1) An automationscript (LIB_FUNCTION_001) containing a function that we want to use in another automationscript:
Lets say you want to perform some simple calculations in a function called iIncrease that has one parameter iIncreaseMe
if iIncreaseMe == 1 you want to add 1
if iInCreaseMe == 2 you want to add 2
otherwise you want to add 10
Writing a jython function for this would look something like:
def iIncrease(iIncreaseMe):
print "Function:iIncrease(" + str(iIncreaseMe) + ") - Start"
if (iIncreaseMe == 1):
iReturnValue = iIncreaseMe + 1
elif (iIncreaseMe == 2):
iReturnValue = iIncreaseMe + 2
else:
iReturnValue = iIncreaseMe + 10
print "Function:iIncrease(" + str(iIncreaseMe) + ") - End - Returning:[" + str(iReturnValue) + "]"
return iReturnValue
(2) Another automationscript (USE_LIB_FUNCTION) that retrieves the script source of LIB_FUNCTION_001 and then 'exec(ute)' the script source so the function becomes available for use
We have therefore defined following automationscript called USE_LIB_FUNCTION:
----- Script START ----------
from com.ibm.tivoli.maximo.script import ScriptCache
def strGetAutoscriptSource(strScriptName):
strReturnValue = ""
# Use the ScriptCache to get the script source so you are not making a database call, which is otherwise expensive
scriptInfo = ScriptCache.getInstance().getScriptInfo(strScriptName)
# Make sure that the script is Jython / Python. Note that we are comparing the engine name because both Jython and Python use the same engine.
# Also note that getScriptLanguge() is the correct method name and Language is misspelled in the Maximo API
if JSR223ScriptDriver.getSupportedScriptEngineInfo(scriptInfo.getScriptLanguge()).getEngineName() == "jython":
strReturnValue = scriptInfo.getScriptSource()
return scriptInfo.getScriptSource()
print "============================================(1)"
print "Read autoscript LIB_FUNCTION_001"
strFunction = strGetAutoscriptSource("LIB_FUNCTION_001")
print "============================================(2)"
print "strFunction:"
print strFunction
print "============================================(3)"
print "Performing Exec - Read Function into memory"
exec(strFunction)
print "============================================(4)"
print "Use Read Function - by executing iValue = iIncrease(1)"
iValue = iIncrease(1)
print "iValue:"
print str(iValue)
print "============================================(5)"
print "Use Read Function - by executing iValue = iIncrease(2)"
iValue = iIncrease(2)
print "iValue:"
print str(iValue)
print "============================================(6)"
print "Use Read Function - by executing iValue = iIncrease(10)"
iValue = iIncrease(10)
print "iValue:"
print str(iValue)
print "============================================(7)"
----- Script END ----------
Result: When we execute the script by pressing (the re-introdued) run script button we see the following output:
----- Output START ----------
============================================(1)
Read autoscript LIB_FUNCTION_001
============================================(2)
strFunction:
def iIncrease(iIncreaseMe):
print "Function:iIncrease(" + str(iIncreaseMe) + ") - Start"
if (iIncreaseMe == 1):
iReturnValue = iIncreaseMe + 1
elif (iIncreaseMe == 2):
iReturnValue = iIncreaseMe + 2
else:
iReturnValue = iIncreaseMe + 10
print "Function:iIncrease(" + str(iIncreaseMe) + ") - End - Returning:[" + str(iReturnValue) + "]"
return iReturnValue
============================================(3)
Performing Exec - Read Function into memory
============================================(4)
Use Read Function - by executing iValue = iIncrease(1)
Function:iIncrease(1) - Start
Function:iIncrease(1) - End - Returning:[2]
iValue:
2
============================================(5)
Use Read Function - by executing iValue = iIncrease(2)
Function:iIncrease(2) - Start
Function:iIncrease(2) - End - Returning:[4]
iValue:
4
============================================(6)
Use Read Function - by executing iValue = iIncrease(10)
Function:iIncrease(10) - Start
Function:iIncrease(10) - End - Returning:[20]
iValue:
20
============================================(7)
----- Output END ----------
Let us know what you think...!
------------------------------
Erwin Hebing
------------------------------