Skip to content

Instantly share code, notes, and snippets.

@codeartery
Last active July 21, 2024 05:29
Show Gist options
  • Save codeartery/0ea3a74e92d39ce520cffb05a8b99446 to your computer and use it in GitHub Desktop.
Save codeartery/0ea3a74e92d39ce520cffb05a8b99446 to your computer and use it in GitHub Desktop.
An AppActivate alternative that shows and activates a window using its process name, window title, or both.
Function ShowWindow( name, title )
REM@description
' An AppActivate alternative that shows and activates a window using its process name, window title, or both.
REM@author
' Jeremy England, http://codeartery.com/
REM@params
' name <string> The process name running the window. Use a comma delimited string to add more processes if you're unsure what process will be used. Leave blank "" if you don't know the process.
' title <regex> A regular expression string, to match the correct window title. Use "." if you don't know the title.
REM@returns
' ShowWindow <bool> Returns false if it doesn't find the window; true if it does.
REM@mini
' Function ShowWindow(n,t):With CreateObject("WScript.Shell"):ShowWindow=.Run "PowerShell -Ex Bypass -Com ""Add-Type -M '[DllImport(\""user32.dll\"")] public static extern bool ShowWindow(IntPtr h,int c);[DllImport(\""user32.dll\"")] public static extern int SetForegroundWindow(IntPtr h);' -Name ps -Names w;$d=(gps "&n&").Where({$_.MainWindowTitle -Match '"&t&"'},1).MainWindowHandle;[w.ps]::ShowWindow($d,4);[w.ps]::SetForegroundWindow($d)""",0,-1:End With:End Function
With CreateObject( "WScript.Shell")
ShowWindow = .Run( "PowerShell -ExecutionPolicy Bypass -Command ""Add-Type -MemberDefinition '" & _
"[DllImport(\""user32.dll\"")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);" & _
"[DllImport(\""user32.dll\"")] public static extern int SetForegroundWindow(IntPtr hwnd);' -Name PS -Namespace WindowsAPI;" & _
"$hWnd = (Get-Process " & name & ").Where({ $_.MainWindowTitle -Match '" & title & "' }, 1).MainWindowHandle;" & _
"[WindowsAPI.PS]::ShowWindow( $hWnd, 4 );[WindowsAPI.PS]::SetForegroundWindow( $hWnd );""", 0, True ) = 0
End With
End Function
REM@usage
' Put the full or mini class/sub/function in your script to use.
Function ShowWindow(n,t):With CreateObject("WScript.Shell"):ShowWindow=.Run "PowerShell -Ex Bypass -Com ""Add-Type -M '[DllImport(\""user32.dll\"")] public static extern bool ShowWindow(IntPtr h,int c);[DllImport(\""user32.dll\"")] public static extern int SetForegroundWindow(IntPtr h);' -Name ps -Names w;$d=(gps "&n&").Where({$_.MainWindowTitle -Match '"&t&"'},1).MainWindowHandle;[w.ps]::ShowWindow($d,4);[w.ps]::SetForegroundWindow($d)""",0,-1:End With:End Function
' activate windows with the process name and title
ShowWindow "chrome", "Nekomesha - Youtube"
ShowWindow "notepad", "Untitled - Notepad"
' don't know the title? use a period for the title
ShowWindow "iexplore", "."
' don't know the processes name? leave it blank
ShowWindow "", "Control Panel"
' not sure which process will be running? use commmas to add more processes to the search
ShowWindow "cscript, wscript", "Form Entry"
' the title changes depending on if minimzed or maximized? use a regular expression to get it
ShowWindow "", "(\d{1,3}% complete)|(Compressing...)"
' if your process name includes a space use single quotes around it.
ShowWindow "'my proccess'", "."
' the window is not being activated? use the return value to check if you typed the process name or title correctly
Dim isFound
isFound = ShowWindow( "notepad", "Untitled" )
If( isFound )Then
Call MsgBox( "Found it, here is your window!", vbInformation )
Else
Call MsgBox( "Sorry, I couldn't find that window.", vbCritical )
End If
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment