# Can I tell what scope a script is running from?

**URL:** <https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880>\
**Category:** Ignition\
**Tags:** vision, perspective, scripting\
**Created:** [March 10, 2023, 4:54am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880 "2023-03-10T04:54:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [March 10, 2023, 4:54am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/1 "2023-03-10T04:54:46Z")

</div>

From a script, can I tell what scope it's running from? e.g. Gateway, Perspective, Vision, Designer, etc.

I want to display an error popup, but the call is different for different scopes

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [March 10, 2023, 10:13am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/2 "2023-03-10T10:13:01Z")

</div>

> [@Best methods for checking script scope](https://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201/2):
>
> Try this: def getScope(): """ Returns: scope (int): 0=Gateway, 1=Client, 2=Designer """ try: if system.util.getSystemFlags() & system.util.CLIENT\_FLAG: location = 1 elif system.util.getSystemFlags() & system.util.DESIGNER\_FLAG: location = 2 except AttributeError: # system.util.getSystemFlags() does not exist in gateway scope location = 0 return location (Source: [Which scope is my code running in?](http://forum.inductiveautomation.com/t/which-scope-is-my-code-running-in/15540/3))

This is 2019 so whilst perspective was out then (April 2019 = 8.0.0) it doesn't mention that scope and may need tweaked for it

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [March 10, 2023, 10:24am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/3 "2023-03-10T10:24:23Z")

</div>

That's very useful! Hmm. I'll have to see if I can get it to return if it's in Perspective

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 10, 2023, 11:35am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/4 "2023-03-10T11:35:32Z")

</div>

Testing in perspective returns a gateway scope.

```python
from com.inductiveautomation.ignition.common.model import ApplicationScope
scope = ApplicationScope.getGlobalScope()

if scope == ApplicationScope.GATEWAY:
	...
if scope == ApplicationScope.CLIENT:
	...
if scope == ApplicationScope.DESIGNER:
	...
if scope == ApplicationScope.ALL:
	...
if scope == ApplicationScope.NONE:
	...

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 10, 2023, 2:55pm UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/5 "2023-03-10T14:55:37Z")

</div>

What events/context would trigger .ALL or .NONE?

---

<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:** [March 10, 2023, 3:27pm UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/6 "2023-03-10T15:27:50Z")

</div>

None. You'll only ever get one of the three C/D/G values as the current scope for a script. The other values are used in other areas, but will never be returned as the current scope.

---

<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:** [March 11, 2023, 11:36am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/7 "2023-03-11T11:36:32Z")

</div>

I recommend using `hasattr()` to probe the available functions in `system.*` to determine what is possible, preferably done at the top level of a script module with results placed into top-level booleans.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 13, 2023, 6:35pm UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/8 "2023-03-13T18:35:34Z")

</div>

This is what we do to determine scope in a script

```python
from com.inductiveautomation.ignition.common.model import ApplicationScope

def getScope():
	scope = ApplicationScope.getGlobalScope()
	if ApplicationScope.isClient(scope):
		return "c"

	if ApplicationScope.isDesigner(scope):
		return "d"

	if ApplicationScope.isGateway(scope):
		if "perspective" in dir(system):
			return "p"
		return "g"
	return "u"

```

---

<div class="post-metadata">

**Author:** ![Michael\_Lindon](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@Michael\_Lindon](https://forum.inductiveautomation.com/u/Michael_Lindon)\
**Post date:** [July 16, 2025, 10:45pm UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/9 "2025-07-16T22:45:55Z")

</div>

This post came up in a lot in my searches and had to make adjustments to it to differentiate between whether a script was being run on the Gateway vs a Perspective Session Scope, IE whether Perspective session properties exist. Here is a quick change to the above method that solved it for me.

```python
from com.inductiveautomation.ignition.common.model import ApplicationScope

class Scope(object):
	@staticmethod
	def get():
		scope = ApplicationScope.getGlobalScope()
		if ApplicationScope.isClient(scope):
			return "c" # vision client
	
		if ApplicationScope.isDesigner(scope):
			return "d" # designer
	
		if ApplicationScope.isGateway(scope):
			try:
				system.perspective.getProjectInfo()
				return "p"
			except:
				return "g"
		return "u" # unknown

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 17, 2025, 12:15am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/10 "2025-07-17T00:15:11Z")

</div>

You can use something like `hasattr(system, 'perspective')` instead of trying to call a function.  
I think that's what @pturmel suggested.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 17, 2025, 12:29am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/11 "2025-07-17T00:29:25Z")

</div>

Nah it doesn't work, `perspective` exists in global scope as well

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 17, 2025, 7:03am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/12 "2025-07-17T07:03:03Z")

</div>

Then the accepted solution is probably wrong too.  
I'd expect `'perspective' in dir(system)` to return the same thing as `hasattr()`

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 17, 2025, 7:05am UTC](https://forum.inductiveautomation.com/t/can-i-tell-what-scope-a-script-is-running-from/70880/13 "2025-07-17T07:05:01Z")

</div>

Correct. Oh wait, it was _my_ topic ahaha, my bad. Ok, i've re-solutioned it!
