viewpoint-particle

Author Topic: Is This Partial Window Name Possible? "ABC*XYZ"  (Read 273 times)

mcjathan

  • Newbie
  • *
  • Posts: 36
Is This Partial Window Name Possible? "ABC*XYZ"
« on: August 17, 2017, 11:17:27 am »
I'm needing to detect a partial window name where I know the beginning and ending of the window name, but not the middle.  Using standard wildcard string matching, here's what I'm after:

      "ABC*XYZ"

where the * is an unknown number of undetermined characters.

Is there anyway to do this with a partial window name?

td

  • Tech Support
  • *****
  • Posts: 2223
    • WinBatch
Re: Is This Partial Window Name Possible? "ABC*XYZ"
« Reply #1 on: August 17, 2017, 11:49:09 am »
From the Consolidated WIL Help file:

Exact Match
Those WIL functions which take a partial windowname as a parameter can be directed to accept only an exact match by ending the window name with a tilde (~).

A tilde (~) used as the last character of the window name indicates that the name must match the window title through to the end of the title. For example, WinShow("Note~") would only match a window whose title was "Note"; it would not match "Notepad".

Match Any Window
A tilde (~) used as the first character of the window name, will match any window containing the specified string anywhere in its title. For example, WinShow("~Notepad") will match a window title of "(Untitled) - Notepad" and a window title of "My Notepad Application", as well as a window title of "Notepad - (Untitled)".

Window Name Ends With…
A tilde (~) used as the first and last character of the window name, will match any window containing the specified string anywhere in its title, except at the beginning. For example, WinShow("~Notepad~") will match a window title of "Notepad" and a window title of "(Untitled) -Notepad", but will not match a window title of "Notepad - (Untitled)".

Note:  All functions that accept a partial window name also accept a Windows ID.

So the short answer is no.

However, you could implement your own find window by perhaps using the WinItemize function to obtain a fill list of window names and then using either the COM Automation "vbscript.regexp" object or dotNet"System.Text.RegularExpressions.Regex"class to search the list.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


mcjathan

  • Newbie
  • *
  • Posts: 36
Re: Is This Partial Window Name Possible? "ABC*XYZ"
« Reply #2 on: August 17, 2017, 12:19:07 pm »
Thanks, td.  I did look at the documentation before posting.

I can run with the WinItemize suggestion, thank you!

Untested, but I think this will do the job for me:
Code: [Select]
#DefineFunction WinExistWild(WildString)

   allwins = WinItemize()
   result = @FALSE
   n = ItemCount(allwins, @TAB)
   For i = 1 to n
      eachwin = ItemExtract(i, allwins, @TAB)
      If StrIndexWild(eachwin, WildString, 1) > 0 Then
         result = @TRUE
         Break
      Endif
   Next n

   Return result

#EndFunction

td

  • Tech Support
  • *****
  • Posts: 2223
    • WinBatch
Re: Is This Partial Window Name Possible? "ABC*XYZ"
« Reply #3 on: August 17, 2017, 01:39:14 pm »
A regular expression example that may or may not be faster when a lot of windows are open:

Code: Winbatch
Run("NotePad.exe","")

lWinNames  = WinItemize()
strWinName = ''
objRegEx           = CreateObject("vbscript.regexp")
objRegEx.Global    = @True
objRegEx.IgnoreCase = @True
objRegEx.Pattern   = "no[a-zA-Z_0-9]*ad"

colRegEx = objRegEx.Execute(lWinNames)
For i = 0 To colRegEx.Count - 1
   strWinName = colRegEx.Item(i).value ;.SubMatches(0)
   break
Next

Message("Found Window Names", strWinName)
if strWinName != '' then WinClose('~':strWinName)
exit
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


td

  • Tech Support
  • *****
  • Posts: 2223
    • WinBatch
Re: Is This Partial Window Name Possible? "ABC*XYZ"
« Reply #4 on: August 18, 2017, 01:29:33 pm »
More fun with regular expressions:
Code: Winbatch
Run("NotePad.exe","")

strWinName = ''
ObjectClrOption("useany", "System")
objRegEx = objectClrNew("System.Text.RegularExpressions.Regex")

; Patter matches from begining of an item list item up to but not including the next @tab
; In this case it matches the no...ad of Notepad.
strPattern   = '(([A-Za-z0-9\- ]*)no([A-Za-z0-9\- ]+)ad([A-Za-z0-9\- ]*))'
lWinNames  = WinItemize()

; (System.Text.RegularExpressions.RegexOptions:1 means ignore case.)
objMatches = objRegEx.Matches(lWinNames , strPattern, System.Text.RegularExpressions.RegexOptions:1)
x = 0
foreach objMatch in objMatches
   strWinName :=  objMatch.Groups.Item(1).Value
   if strWinName != '' then WinClose(strWinName)
   break ; Assume one match but can be easily modifited to handle multiple matches.
next

Message("Found Window Names", strWinName)
exit
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates