Ok. I think I beat this one to death. I was unable to get the isAppRunning to work with my app. It does work with other apps like notepad, chrome etc just not with my app. Not sure, why but I gave up, at least using the VBScript approach. I used Justin's caching method and tweaked it do the same in PowerShell rather than VBScript. PowerShell seems to work for me on both Win10 & WinServer2019. It also works when using the Task name with or without the .EXE. It also works using the application description (the name that shows up when you ALT+TAB to that window) .
The only caveat using PowerShell is to set PS Script Authorization. Both LocalMachine & CurrentUser should work:
PowerShell.exe Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine
I ended up turning the code into 2 functions. Function #1 isAppRunning scans the TaskList and returns a True or False if the required application is running & exists in the task list. Function #2 AppActivate will perform the AppActivate or switch focus to the required application.
If I have more time, I would like to add code when the isAppRunning returns a False, to manually run the application.
All the credit goes to justinedwards.jle as I used his original source code above and guidance to complete this task - Thanks.
The GoTo Button runs the code as follows:
taskName = "DisplayClient.exe"
switchName = "Tea_Room" # < DisplayClient or DisplayClient.exe > also works
if shared.Functions.isAppRunning(taskName,0):
shared.Functions.AppActivate(switchName)
The 2 functions are as follows:
import os
import getpass
from java.lang import Runtime
from java.io import InputStreamReader
from java.io import BufferedReader
def isAppRunning(taskName,printTaskList=0):
# Rev.2022-12-11
x = 0
testLines = ""
TaskExistsCount = 0
processList = BufferedReader(InputStreamReader(Runtime.getRuntime().exec("tasklist.exe").getInputStream()))
while testLines != None:
if printTaskList == 1:
print testLines
if taskName in testLines:
TaskExistsCount = TaskExistsCount + 1
if printTaskList == 1:
print \r\r\r\r
print 'Task #: %d (Task Name: %s)' %(TaskExistsCount,testLines)
print \r\r\r\r
testLines = processList.readLine()
x = x+1
if printTaskList == 1:
print \r\r\r\r
print 'Task Count: ' + str(x)
processList.close()
if TaskExistsCount > 0:
if printTaskList == 1:
print 'Task Exists: Yes (Occurence: %d)' %(TaskExistsCount)
return True
if TaskExistsCount == 0:
if printTaskList == 1:
print 'Task Exists: No'
return False
processList.close()
def AppActivate(taskName):
# Rev.2022-12-11
# taskName can be:
# Window Name/Description
# Program Executable (without the EXE)
# Program Executable (with the EXE)
#
# If PS error < UnauthorizedAccess > is displayed (Probably a non-admin account)
# Run PowerShell as Admin and execute:
# PowerShell.exe Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine
username = getpass.getuser()
ingTmpPath = "C:/Users/" + username + "/.ignition/cache/tempFile.bat"
winTmpPath = "C:/Users/" + username + "/AppData/Local/Temp/switch.ps1"
if os.path.exists(ingTmpPath): os.remove(ingTmpPath)
if os.path.exists(winTmpPath): os.remove(winTmpPath)
if not os.path.exists(ingTmpPath):
scriptLines = [
"@echo $wshell = New-Object -ComObject wscript.shell>%tmp%\switch.ps1",
"@echo #$wshell.Run('exe goes here')>>%tmp%\switch.ps1",
"@echo $wshell.AppActivate('"+taskName+"')>>%tmp%\switch.ps1",
"@echo #$wshell.Sleep('500')>>%tmp%\switch.ps1",
"@echo #$wshell.SendKeys('sendKeys goes here')>>%tmp%\switch.ps1",
"PowerShell.exe -command %tmp%\switch.ps1",
"exit"]
with open(ingTmpPath, "w") as tf:
for line in scriptLines:
tf.write(line + "\n")
system.util.execute([ingTmpPath])