How to switch to an external application

Just use os.remove(path) to delete the file

The .exe probably won't work in the switching script, so you will probably have to add a second variable. It would look something like this:

taskName = "DisplayClient.exe"
switchName = "DisplayClient"

The isAppRunning function would stay the same, but the second line of your scriptLines array would use switchName instead of taskName.

Putting it all together, the second half of the script would look like the revised code below:

Revised Code
taskName = "DisplayClient.exe"
switchName = "DisplayClient"
if isAppRunning(taskName):
	username = getpass.getuser()
	path = "C:/Users/" + username + "/.ignition/cache/tempFile.bat"
	if os.path.exists(path):
		os.remove(path)
	scriptLines = [
		"@echo WScript.CreateObject(\"WScript.Shell\").AppActivate(WScript.Arguments.Item(0))>%tmp%\switch.vbs",
		"%tmp%\switch.vbs \""+switchName+"\"",
		"exit"]
	with open(path, "w") as tf:
		for line in scriptLines:
			tf.write(line + "\n")
	system.util.execute([path])

This makes perfect sense:
image

1 Like