# Perspective Button to run script then close window

**URL:** <https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287>\
**Category:** Ignition\
**Tags:** perspective, ignition81\
**Created:** [April 23, 2024, 2:02pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287 "2024-04-23T14:02:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 23, 2024, 2:02pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/1 "2024-04-23T14:02:16Z")

</div>

I am trying to create a button on a pop up window that would change a tag value and then close the window. The script and pop up close seem to work fine when I have them as the only action but the script won't write to the tags when both of the actions are on the button. I found a few posts that said this was a bug in Ignition and the actions actually run at the same time and not in a row, causing it to close before it writes. Has anyone found a work around to this problem?

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

---

<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 23, 2024, 2:09pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/2 "2024-04-23T14:09:11Z")

</div>

1. Don't use the Popup action to close the popup. Script it instead _after_ your tag writes. See [system.perspective.closePopup | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-perspective/system-perspective-closePopup). That way you guarantee the execution order.
2. `system.tag.writeBlocking' accepts lists of tags and values. You can then write to them all at once. It's faster and less gateway load.

> **[system.tag.writeBlocking | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-writeBlocking)**
>
> Writes values to tags at the given paths.

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 23, 2024, 2:23pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/3 "2024-04-23T14:23:45Z")

</div>

Thanks! I was not aware of the "system.perspective.closePopup" script. I am still struggling a little though. I have this code on a value change event script for the tag:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if currentValue.value == False:
		system.perspective.closePopup(12345)

```

and this is the code to open the pop up

```python
	if currentValue.value == 7:
		system.perspective.openPopup(12345, 'Page/program7Popup')

```

The page seems to still not want to close.

---

<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 23, 2024, 2:34pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/4 "2024-04-23T14:34:45Z")

</div>

This started off as a question on a button script.  
Now it has changed into a script on a tag event!

1. Tags don't know anything about sessions and what views / popups you have open. They don't even know if any client has the project open.
2. In your code you are treating currentValue.value as boolean in one instance and integer in another. There seems to be some confusion in someone's thinking.
3. `if currentValue.value == False:` can be written as `if not currentValue.value:`

You need to trigger popup opening and closing from changes **in your application** , not in tags which are on the gateway. You _could_ bind a component or custom property in your application to a gateway tag and trigger an _onChange_ event on the component / property - but not on the tag itself.

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 23, 2024, 3:20pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/5 "2024-04-23T15:20:43Z")

</div>

I changed it up and have the 'Popup active' tag bound to a cutsom property and I have a change script on that custom property that looks like this:

```python
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	#Current_Program_Number writes turns activePopup true when prgrm = 7, Buttons turn it flase when click
	if currentValue.value:
		system.perspective.openPopup(12345, 'Page/program7Popup')
	elif not currentValue.value:
		system.perspective.closePopup(12345)

```

but now the pop up won't even pop up

---

<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 23, 2024, 3:24pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/6 "2024-04-23T15:24:58Z")

</div>

1. Have you confirmed that your 'cutsom' property is actually changing? Add a label to your view and bind `props.text` to the custom property to monitor it.
2. What values does it give?
3. Why are you using a tag to (now indirectly) control a popup? Have you considered what happens when multiple clients are open?

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 23, 2024, 3:28pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/7 "2024-04-23T15:28:56Z")

</div>

1. Yes the property is flipping correctly with the tag

2. It is a boolean, giving either true or false:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/7/2744a0d732069c2ef462c67cb11dd9388bf18550.png)

3. Each client will be unique based off of its IP address. I have a query that looks up the current IP address and assigns it based on a table, then each tag path I have set up indirrectly so it will know each machines tags  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/f/ffee584310d089d8b94defe091318934258e088d.png)

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

Is my problem that I do not have a unique pop up ID, for a specific session?

---

<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 23, 2024, 3:34pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/8 "2024-04-23T15:34:47Z")

</div>

What component is the custom property assigned to?

I still don't understand why you're using a tag to control the activePopup. Do you pass this value to the machine somehow? If not then activePopup should be a `session.custom` property.

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 23, 2024, 3:39pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/9 "2024-04-23T15:39:20Z")

</div>

It is assigned to the root of the pop up.  
The tag is only a memory tag that I am writing to when another tag is a value that I want the pop up to happen on.

---

<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 23, 2024, 3:43pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/10 "2024-04-23T15:43:54Z")

</div>

> [@Ryan\_Craig](#):
>
> It is assigned to the root of the pop up.

But if the popup isn't open it will never read the custom property. You need to add the custom prop and event to the calling view.

> The tag is only a memory tag that I am writing to when another tag is a value that I want the pop up to happen on.

You can still do that with a session variable.

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [April 23, 2024, 3:51pm UTC](https://forum.inductiveautomation.com/t/perspective-button-to-run-script-then-close-window/89287/11 "2024-04-23T15:51:11Z")

</div>

I switched the pop up script to the root of the main page and it seems to work now, thanks!
