Can I tell what scope a script is running from?

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

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

2 Likes

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

Testing in perspective returns a gateway scope.

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:
	...
4 Likes

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

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.

3 Likes

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.

2 Likes

This is what we do to determine scope in a script

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"
7 Likes

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.

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
3 Likes

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

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

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

2 Likes

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

2 Likes