# Calling Project Script to Edit Session Props

**URL:** <https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [April 10, 2024, 10:06pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668 "2024-04-10T22:06:59Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 10, 2024, 10:06pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/1 "2024-04-10T22:06:59Z")

</div>

Good afternoon everyone.

I'm on Ignition 8.1.32 developing in Perspective. I have a script that currently runs on a button to change the language from English to Spanish:

```auto
def runAction(self, event):
	if self.session.props.locale == "en-US":
		self.session.props.locale = "es"
	else:
		self.session.props.locale = "en-US"

```

This script works just fine for a button that is visible on the main login page of the application. However, I want to add this to our nav menu after the user logs in. To do this, I have written this in a project library script:

```auto
def en_to_es():
	if self.session.props.locale == "en-US":
		self.session.props.locale = "es"
	else:
		self.session.props.locale = "en-US"

```

I want to call this project library script as part of another script that runs when the right component is clicked on, but when I test the script in the script console as "translate.en\_to\_es()", I get the following error:

Traceback (most recent call last):  
File "", line 1, in   
File "module:translate", line 2, in en\_to\_es  
NameError: global name 'self' is not defined

When I make a test script with a print function and call it in the same way, it works fine.

Am I correct that is has to do with the way it's calling the locale prop? It's not on a button so self.session probably can't reach it. But what is the correct way to modify that prop from the project library?

---

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 10, 2024, 10:15pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/2 "2024-04-10T22:15:18Z")

</div>

Could I use a custom method on the component that is running the script? That is not something I've done before and I only just thought of it.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 10, 2024, 10:18pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/3 "2024-04-10T22:18:00Z")

</div>

`self` refers to the container or component in Perspective. The gateway doesn't know what you're referring to and also doesn't know what (who's browser) session you're referring to (and if you got it to work it would affect everyone's session).

A simpler solution is to use a Radio Group or a dropdown.

 ![ES - EN](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/a/fa516d690e9c7db404a8c8b56032efe8e108f8a6.png)

You can use this as many times as required in your project and there is no script required.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 10, 2024, 10:22pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/4 "2024-04-10T22:22:48Z")

</div>

The other option is to:

- Create a small coordinate view with the button and its working code.
- Embed that view wherever you need it.

---

<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:** [April 10, 2024, 10:38pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/5 "2024-04-10T22:38:40Z")

</div>

> [@Joshua\_Myers](#):
>
> when I test the script in the script console

Worth calling out as well - the script console is running your code in the local designer scope, _not_ on the gateway, as scripts in Perspective will be (even the Perspective session in your designer, for consistency).

And the project library is just that - a library, that you can call _into_ from anywhere, but it doesn't have any inherent scope attached; project library code may or may not work when called from the gateway or the designer, depending on what it's actually doing.

---

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 10, 2024, 10:45pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/6 "2024-04-10T22:45:19Z")

</div>

So the reason I was trying to use a callable script is because I’m using this menu from the Exchange:

> **[Ignition Exchange](https://inductiveautomation.com/exchange/2463/overview)**
>
> Discover, share, and download community made Ignition resources in collaboration with your organization or the Ignition community.

When you dig into it down to the bottom, there’s a script that runs on click on the container. That script is just a navigate script based on a parameter that you set at the top level of the three views.

I want to keep the existing navigation function, so I created a new BOOL parameter that passes through to the script, so I can toggle it if I want it to perform another function on click. I was going to try to call the specific script with another parameter that passes the name of the translate script into the ‘on click’ script.

The idea being that for each element of the flex repeater on the top level, I can dig into and set the parameters to perform the standard navigate function or to perform some other function when it’s clicked.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 10, 2024, 11:00pm UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/7 "2024-04-10T23:00:46Z")

</div>

> [@Joshua\_Myers](#):
>
> `def en_to_es():`

Change this to `def en_to_es(self):` and then include `self` from the point where you call it.

Project library scripts have no scope of their own, and generally have no context (not quite true). If a variable is passed **into** an event, and you want it within something you call, pass it inward.

---

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 11, 2024, 2:40am UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/8 "2024-04-11T02:40:21Z")

</div>

Mr Turmel does it again. Thank you sir!

---

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 11, 2024, 2:41am UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/9 "2024-04-11T02:41:11Z")

</div>

That's good to know. I'm completely knew to both Python and Javascript and really anything that's going on in Perspective so this is all good info for me. Thank you!

---

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 11, 2024, 2:46am UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/10 "2024-04-11T02:46:23Z")

</div>

That is a great solution and I may use that in the future, but for this project specifically, I'm trying to get it to work within an existing script that navigates to a page. It's a flex view within a view that's called in a flex repeater, and I added parameters to toggle between the navigate function and any other script that I want to run 'on click.'

---

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 11, 2024, 2:57am UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/11 "2024-04-11T02:57:07Z")

</div>

So now the script will work when I call it directly, but if I try to pass it through using a parameter to push the script in, it no longer works.

```auto
def runAction(self, event):
	if not self.view.params.button:
		system.perspective.navigate(page = self.view.params.target)
	else:
		self.view.params.script
		#translate.en_to_es(self)

```

The part that is commented out is what works while self.view.params.script does not pass it through.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/3/33e3070dc0ea7a83b7adc05b45424cb82d5143a5.png)

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [April 11, 2024, 4:23am UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/12 "2024-04-11T04:23:21Z")

</div>

Are you trying to dynamically assign what script to run on the button press event by trying to specify the script you want to run as a parameter? If so, that will not work. What are you trying to accomplish/simplify with this?

If you are trying to be able to dynamically tell the button what to do, a better way would be use a `typeId` property/param and come up with some set values for it. Then, in a top level script, you compare the passed value of `typeId` with known values and if it matches the value, run the specific script you decided to associate with that value.

Something along the lines of:

```auto
def onButtonTemplateClick(session, typeId):
	""" """

	if typeId == "Translate_ES_US":
		translate.en_to_es(session)

	elif typId == "Nav_Main":
		navigate.goto_main_Screen(session)

	else:
		print "Unknown typeId '%s'" % typeId

	return

```

---

<div class="post-metadata">

**Author:** ![Joshua\_Myers](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Joshua\_Myers](https://forum.inductiveautomation.com/u/Joshua_Myers)\
**Post date:** [April 11, 2024, 11:46am UTC](https://forum.inductiveautomation.com/t/calling-project-script-to-edit-session-props/88668/13 "2024-04-11T11:46:36Z")

</div>

This works perfectly, thank you!
