Check if external app is running

Is there a way (on Windows) to check if an external exe is running? I’ve used FindWindow API in .Net but not sure how to use it in Xojo.

Dim hwnd As IntPtr = FindWindow(Nothing, “Notepad”)
If hwnd = IntPtr.Zero Then
Shell(“Notepad.exe”)
Else
SetForegroundWindow(hwnd)
SetFocus(hwnd)
End If

Here’s what I use. Pass in the name of the application and get a Boolean back.

Var tempString as String
Var tempShell as New Shell
tempShell.TimeOut = 30000
tempString = "tasklist /FI ""IMAGENAME eq " + appName + ""
tempShell.Execute (tempString)
Return tempShell.Result.IndexOf (appName) > -1

It worked great, thanks Dale!

what is the equivalent for mac??

var z as new Shell
var x as Integer
var a as String=“Safari”

z.ExecuteMode=Shell.ExecuteModes.Synchronous
z.Execute("pgrep "+a)
x=z.ExitCode

if x=0 then
MessageBox(a+" is running")
else
MessageBox(a+" is not running")
end if

just try this mbut using safari instead Safari and go no result. once change to Safari, got the following bunch of numbers?? what does it mean?? also don’t see the “is running” or “is not running”

1123
1828
1831
1843
5000
5360
5615
5626
5643

when you type pgrep app-name in terminal - you will get list of processes for Safari… When you use z.Execute(“pgrep Safari”) then in z.ExitCode you will get zero or one (when no Safari process found)

so those number of line of process is how many tabs i have open ??

not exactly, try:

ps aux | grep Safari

Public Function getPID(theApplication as String) as String
  
  'Return PID via shell
  
    if theApplication = "" then Return ""
    
    dim theShell as new Shell 
    theShell.Execute "ps -alxwc"
    if theShell.ExitCode <> 0 then Return ""
    
    dim theResult as String = theShell.Result
    dim NoOfLines as Integer = theResult.CountFields(EndOfLine.UNIX)
    for currentLine as Integer = 1 to NoOfLines
      dim theLine as String = theResult.NthField(EndOfLine.UNIX, currentLine)
      if theLine.IndexOf(theApplication) > -1 then
        Return mid(theLine, 7, 5)
      end if
    Next
    
End Function