Programatically search through templates?

yes. This may require some explanation. Instead of using an event object i create a Jframe and grab the projects highest most frame reference and get it that way… I work in console a lot too and this helps. This is a singleton class which makes it nice. Load it at startup and dont worry about getting it again.Heavily unsupported.

from system.util import CLIENT_FLAG, DESIGNER_FLAG
from javax.swing import JFrame
from java.awt.event import WindowEvent

try:
	from com.inductiveautomation.ignition.designer import IgnitionDesigner
except ImportError:
	pass

from com.inductiveautomation.factorypmi.application.runtime import ClientPanel


class ActiveContext(object):
	"""
	Singleton for a stable ignition context reference. 
	NOTE: Mainly for code running where there is no event 
	object to derive context access. i.e. 
	scripts launched automatically or script-console..
	# Has been tested for Designer, 
	# Script Console, Client [Full Screen, Published].
	# We call this as a startup script.
	# Tested on Ignition Version 7.9.3
	# Certian API dependencies may need to be resolved 
	# on versions before or after (7.9.3).
	"""

	def __new__(cls):
		"""
		Hook into the class constructor.
		Adds an class attribute reference to the
		instance.
		"""
		if not hasattr(cls, 'instance'):
			cls.instance = super(ActiveContext, cls).__new__(cls)
			# we only need to establish the context reference 1 time.
			cls.instance.run()
			
		return cls.instance
   
	def run(self):
		"""Get context."""
		print '*************************** Acquiring Project Context ***************************'
		self.valid = True
		self.context = self.resolveContext()

	def isValid(self):
		"""Is Context Valid."""
		return self.valid
	
	def setValid(self):
		"""Set Valid State."""
		self.valid = False if self.valid else True
	
	def resolveDesignerContext(self):
		"""Return designer context."""
		try:
			return IgnitionDesigner.getFrame().getContext()
		except AttributeError:
			pass
		self.isValid()
		
	def resolveClientContext(self):
		"""
		Attempt to resolve client context.
		By creating a frame it prevents the need to
		traverse the object heirarchy. i.e. parent.parent....
		"""
		# acquire project name.
		projName = system.util.getProjectName()
		# Frame title.
		frame_name = 'ThrowAway'
		# Create disposable top level frame object.
		frm = JFrame(frame_name)
		# Adjust close behavior.
		frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
		# get the windows.
		windows = frm.getWindows()
		# cleanup frame.
		frm.dispose()
		# iterate windows, filter on the title containing the 
		# project name.
		for window in windows:
			try:
				if projName in window.getTitle():
					pane =  window.getContentPane()
					# Compare the content pane instance 
					# to the ClientPanel object.
					if isinstance(pane, ClientPanel):
						if hasattr(pane, 'getClientContext'):
							return pane.getClientContext()
			except AttributeError:
				pass
		self.isValid()	
		
	def resolveContext(self):
		"""Attempt to resolve the Context."""
		current_flag = system.util.getSystemFlags()
		ctx = self.resolveDesignerContext
		# If False assumes the client context.
		if current_flag not in [DESIGNER_FLAG, 3]:
			ctx = self.resolveClientContext
		return ctx()		

	def __call__(self):
		"""Calls to the Instance return the Context."""
		return self.getContext()
	
	def getContext(self):
		"""Return Context."""
		return self.context



ctx = ActiveContext()

def initActiveContext():
	"""Called from startup script"""
	return ctx
2 Likes