Vision Scripting: IndexOf and LastIndexOf / Substring

Trying to write a script that breaks apart a string. I realize I can't use the expression functions, but I can't seem to figure out how to find the index of a colon in a string, and then take a substring up to that index. I've found some previous posts about how to do substrings, but they are all using literal values, and I need to substitute a variable using something equivalent to IndexOf and LastIndexOf.

If you show an example of the string and what you want in the end, it would help give a solution. I would assume you want to use the python split() fuction.

Python String | split() - GeeksforGeeks


What I am trying to do is pass some information into the Alarm Lists from my UDT configurations, and then use that to create a hyperlink to object the corresponding instances faceplate of the UDT associated with the alarm. In my case, I want to do this on a double click.

My issue was that alarmEvent.displayPath is not a string, but some type of object that prints as a string, so the .index() or .rindex() methods were failing. To get around this, I had to explicitly cast alarmEvent.display path to string first.

def onDoubleClicked(self, alarmEvent):
	#Break apart the alarm DisplayPath property into components that can be used to open a faceplate
	DisplayPath = str(alarmEvent.displayPath)
	FaceplatePath = DisplayPath[0:DisplayPath.index(":")]
	PLC = DisplayPath[DisplayPath.index(":")+1:DisplayPath.rindex(":")]
	Instance = DisplayPath[DisplayPath.rindex(":")+1:]

	#Open the faceplate associated with this alarm
	system.nav.openWindowInstance(FaceplatePath, {'PLC' : PLC, 'Instance' : Instance})

type(alarmEvent.displayPath) would show you that it's a StringPath, which you can (as you noted) convert to a string, or, depending on what you want to do, you could operate on its parts directly, via, which might make what you're trying to do easier. It's a StringPath instead of a plain string for ease of parsing and comparison throughout the rest of the system:


I actually got smarter about this and just passed the FaceplatePath, PLC, and Instance name into Associated Data fields from UDT parameters, and then was able very easily get them from in the Alarm List scripts. This also freed up the Display Path column to be used for more informational text about the alarm. All this python string handling has become obsolete for me, but it's always good to learn something new!