OK I admit it, I'm getting confused over how to specify the script to run during the Startup event on an object.
I have a View that has:
a script attached to the root object called "testMethod" (with no parameters)
A Startup event on the root object that attempts to call testMethod
In the startup event I try to invoke it like
runScript("self.testMethod",0)
But I am getting a jython exception of "NameError"
I don't think that I am doing anything wrong, or misspelling anything.
My complete scripts are:
def runAction(self):
"""
Method that will run whenever the selected event fires.
Arguments:
self: A reference to the component that is invoking this function.
"""
#import java.lang.Exception
try:
myEvent = "onStartUp"
myMethod = "self.testMethod"
runScript(myMethod,0)
except:
print "### except: " + myEvent + " Failed to run " + myMethod
print "### except: " + str(sys.exc_info()[0])
and
def testMethod(self):
"""
Custom method that can be invoked directly on this component.
Arguments:
self: A reference to the component that is invoking this function.
"""
print "### testMethod()"
and the wrapper log entry is simply
INFO | jvm 1 | 2022/05/13 15:37:06 | ### except: onStartUp Failed to run self.testMethod
INFO | jvm 1 | 2022/05/13 15:37:06 | ### except: <type 'exceptions.NameError'>
Based on this I need to resolve the server and path of a tag from its pathname in order to invoke system.opc.writeValue . As this should be constant over the life of a session I should only need to look the info up once and locally cache it. I was trying to do this in the object where the info was needed. I was choosing to do the work in a separate function than the startup event in order to better organize my code.
And what do you mean by?
can I invoke testMethod() directly from the Startup event?
If you really want to make things dynamic, then you would use getattr on self:
def runAction(self):
"""
Method that will run whenever the selected event fires.
Arguments:
self: A reference to the component that is invoking this function.
"""
#import java.lang.Exception
try:
myEvent = "onStartUp"
myMethod = "self.testMethod"
getattr(self, myMethod)() #second parens to invoke
except:
print "### except: " + myEvent + " Failed to run " + myMethod
print "### except: " + str(sys.exc_info()[0])
That part definitely makes sense - you may also want to consider moving your logic into project level shared scripts so that you can re-use these scripts across multiple views/components more easily.
And I have event EventA on Object2 that uses a runScript to call a function ScriptA on Object2, from within ScriptA, can I now invoke ScriptB via something like:
self.parent.ScriptB()
??
I've already partitioned my objects across a handful of mutually exclusive functionality, so while your suggestion is something to keep in mind, it's going to be more complicated to come up with a simple library that encompasses all the functional that I want.