# Correct invocation of runScript?

**URL:** <https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561>\
**Category:** General Discussion\
**Tags:** ignition81, scripting, perspective\
**Created:** [May 13, 2022, 9:38pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561 "2022-05-13T21:38:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [May 13, 2022, 9:38pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/1 "2022-05-13T21:38:39Z")

</div>

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:

```auto
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

```auto
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

```auto
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'>

```

What am I doing wrong?!?!?

---

<div class="post-metadata">

**Author:** ![andrews](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@andrews](https://forum.inductiveautomation.com/u/andrews)\
**Post date:** [May 13, 2022, 10:04pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/2 "2022-05-13T22:04:26Z")

</div>

What kind of object are you doing this on?

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [May 13, 2022, 10:05pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/3 "2022-05-13T22:05:16Z")

</div>

It is on the co-ordinate root container of a perspective View

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 13, 2022, 10:06pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/4 "2022-05-13T22:06:20Z")

</div>

> [@peter](#):
>
> A Startup event on the root object that attempts to call testMethod

Is there a reason you're doing this via an expression and runscript, vs just invoking your custom method directly?

~~I'm not sure the expression context automatically brings `self` into scope; maybe try `this`?~~ Nevermind, looks like `self` _should_ be correct.

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [May 13, 2022, 10:11pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/5 "2022-05-13T22:11:14Z")

</div>

Based on [this](https://forum.inductiveautomation.com/t/how-best-to-optimize-for-real-time-jog-buttons/59518/14) 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?

> [@](#):
>
> Is there a reason you’re doing this via an expression and runscript, vs just invoking your custom method directly?

can I invoke testMethod() directly from the Startup event?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 13, 2022, 10:19pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/6 "2022-05-13T22:19:00Z")

</div>

Oh, okay, nevermind. We're _both_ confused 🙂

`runScript` is an _expression_ function - it's designed as a bridge/escape hatch from the limited capabilities of expressions to the power of Jython:  
[https://docs.inductiveautomation.com/display/DOC81/runScript](https://docs.inductiveautomation.com/display/DOC81/runScript)

If you have a script _action_ already (`def runAction` above) - you can just call your custom method directly:

```auto
	try:
		self.testMethod()
	except:
		print "### except: " + "onStartup" + " Failed to run " + "testMethod" 
		print "### except: " + str(sys.exc_info()[0])

```

If you _really_ want to make things dynamic, then you would use `getattr` on `self`:

```auto
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])

```

> [@peter](#):
>
> I was choosing to do the work in a separate function than the startup event in order to better organize my code.

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.

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [May 13, 2022, 10:41pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/7 "2022-05-13T22:41:13Z")

</div>

OK .. that worked!!!

I now better understand the use of runScript!

But that leads me to a question.

If I have a hierarchy like

```auto
View
   +--root--+
         | |--ScriptB
         |
         +--Object1
         |  
         +--Object2----+
                       |--EventA
                       |--ScriptA

```

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()

??

> [@](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 13, 2022, 10:46pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/8 "2022-05-13T22:46:38Z")

</div>

> [@peter](#):
>
> self.parent.ScriptB()

I _think_ this should work, yes.

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [May 13, 2022, 11:08pm UTC](https://forum.inductiveautomation.com/t/correct-invocation-of-runscript/59561/9 "2022-05-13T23:08:21Z")

</div>

I just tested it and it _did_ work!
