WinTitleSearch.ahk - Super Simple Solution to Case Sensitive Window Title Search. Return PID or title from any input sea

Post your working scripts, libraries and tools for AHK v1.1 and older
sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

WinTitleSearch.ahk - Super Simple Solution to Case Sensitive Window Title Search. Return PID or title from any input sea

Post by sashaatx » 07 Feb 2023, 03:14

WinTitleSearch.ahk - Removes the need to care about case sensitive window titles, return PID or Title with formatting.

https://github.com/samfisherirl/WinTitleSearch.ahk-Non-Case-Sensitive-Search-of-Window-Titles

WinTitleSearch("application", "title") WinTitleSearch("application", "1")
WinTitleSearch("application", "PID") WinTitleSearch("application", "2")

same same

I bet my next paycheck that this has already been solved for but, nonetheless, here ya go.

Code: Select all

#Include WinTitleSearch.ahk

;in this example, "steam" is the input search and "Steam" is the application name, not returned by AHK without proper casing. 

msgbox % WinTitleSearch("steam", "title")
;Returns the Title of the window with proper formatting

msgbox % WinTitleSearch("steam", "PID")
;Returns the PID of the window with the title "Steam"

msgbox % WinTitleSearch("steam", "1")
;Returns the Title of the window  with proper formatting

msgbox % WinTitleSearch("steam", "2")
;Returns the PID of the window with the title "Steam"



Example Script:

Code: Select all

#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%

;simply:

#Include WinTitleSearch.ahk

msgbox % WinTitleSearch("steam", 1)
;Returns the PID of the window with the title "steam"

PID := WinTitleSearch("steam", "PID")

msgbox % WinExist(PID)
; WinTitleSearch.ahk - Super Simple Solution to Case Sensitive Window Title Search. Return PID or title from any input search matching a window title. 

WinTitleSearch.ahk Code:

Code: Select all

SetTitleMatchMode, 2
DetectHiddenWindows, on

;msgbox % WinTitleSearch("steam", "title")
;Returns the Title of the window with the title "Steam"

;msgbox % WinTitleSearch("steam", "PID")
;Returns the PID of the window with the title "Steam"

;msgbox % WinTitleSearch("steam", "1")
;Returns the Title of the window with the title "Steam"

;msgbox % WinTitleSearch("steam", "2")
;Returns the PID of the window with the title "Steam"

WinTitleSearch(input, config){
	list_of_windows := WinTit.list_windows()
	windows_lowercase := WinTit.convert_to_lowercase(list_of_windows)
  ; convert all active windows to lowercase
	StringLower, input_lowercase, input
  ; convert input to lowercase
	keyvalue := WinTit.search(input_lowercase, windows_lowercase)
  ; return keyvalue within array matching lowercase will match key of list of windows
	PID := WinExist(list_of_windows[keyvalue])
  if (config := "title") or (config := "1")
  {
    return list_of_windows[keyvalue]
  }
  if (config := "PID") or (config := "2")
  {
    return PID
  }
}


class WinTit
{
	list_windows(){
		window_list := []
		windows_lowercase := []
		WinGet windows, List
		Loop %windows%
		{
			id := windows%A_Index%
			WinGetTitle wt, ahk_id %id%
			window_list[A_Index] := wt
			;r .= wt . "`n"
		}
		return window_list
		;MsgBox %r%
	}
	
	convert_to_lowercase(windows_list){
		windows_lowercase := []
		for k, v in windows_list
			{
				StringLower, val, v
				windows_lowercase[k] := trim(val)
			}
		return windows_lowercase
	}
	
	
	search(input_lower, windows_lowercase){
		for k, v in windows_lowercase
		{
			if InStr(v, input_lower)
			{
				return k
			}
		}
		return 0
	}
	
	
}



https://github.com/samfisherirl/WinTitleSearch.ahk-Non-Case-Sensitive-Search-of-Window-Titles

I bet my next paycheck that this has already been solved for but, nonetheless, here ya go.
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

swub
Posts: 19
Joined: 25 Feb 2019, 09:16

Re: WinTitleSearch.ahk - Super Simple Solution to Case Sensitive Window Title Search. Return PID or title from any input

Post by swub » 08 Feb 2023, 10:56

sashaatx Not sure when this will be helpful but it might be. Your example code does not work for me. Perhaps it needs to be pointed to a specific window? :shock:

gya
Posts: 25
Joined: 04 Nov 2021, 01:22

Re: WinTitleSearch.ahk - Super Simple Solution to Case Sensitive Window Title Search. Return PID or title from any input

Post by gya » 08 Feb 2023, 13:32

@swub

Code: Select all

#NoEnv
#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%
SetTitleMatchMode, 2
tt := "- Bloc-notes"  ; for french !
PID = % WinExist(WinTitleSearch(tt, 2))  : <------
If PID <> 0x0
  WinActivate, %tt%
  else{
    Run, notepad.exe
  }
WinWaitActive, ahk_exe notepad.exe
WinTitleSearch(input, config){
	list_of_windows := WinTit.list_windows()
	windows_lowercase := WinTit.convert_to_lowercase(list_of_windows)
  ; convert all active windows to lowercase
	StringLower, input_lowercase, input
  ; convert input to lowercase
	keyvalue := WinTit.search(input_lowercase, windows_lowercase)
  ; return keyvalue within array matching lowercase will match key of list of windows
	PID := WinExist(list_of_windows[keyvalue])
  if (config := "title") or (config := "1")
  {
    return list_of_windows[keyvalue]
  }
  if (config := "PID") or (config := "2")
  {
    return PID
  }
}
class WinTit
{
	list_windows(){
		window_list := []
		windows_lowercase := []
		WinGet windows, List
		Loop %windows%
		{
			id := windows%A_Index%
			WinGetTitle wt, ahk_id %id%
			window_list[A_Index] := wt
			;r .= wt . "`n"
		}
		return window_list
		;MsgBox %r%
	}	
	convert_to_lowercase(windows_list){
		windows_lowercase := []
		for k, v in windows_list
			{
				StringLower, val, v
				windows_lowercase[k] := trim(val)
			}
		return windows_lowercase
	}	
	search(input_lower, windows_lowercase){
		for k, v in windows_lowercase
		{
			if InStr(v, input_lower)
			{
				return k
			}
		}
		return 0
	}	
}
[Mod edit: [code][/code] tags added.]

Post Reply

Return to “Scripts and Functions (v1)”