Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
activating windows with dynamic name
#1
I need to activate windows which have dynamic names.  

The names vary second by second and are not static the xyz and abc middle parts keeps changing but the start and end of the window name is static

eg.

window 1 name="test xyz blue"
window 2 name="test abc black"

so if I want to activate window 1 I need some sort of wildcard match like

act(win("test WILDCARD blue"))

I cannot use act(win("test")) as this may activate window 2 and I prefer not to use

int a=win("test xyz blue")
act a

Can I activate a window using a string name with a wildcard in the middle - if so how please?
#2
example using notepad window that i change name to test abc black. Can change the middle part to any 3 letters lowercase and will activate it


Code:
Copy      Help
int w1=win("test [a-z]{3} black" "Notepad" "Notepad" 0x200) 
act w1

so if i had 2  notepad windows named "test xyz blue" and "test abc black" and the middle part of each changed would be like this  to activate them

 uses regular expression to find the windows
Code:
Copy      Help
int w1=win("test [a-z]{3} black" "Notepad" "Notepad" 0x200) 
act w1
1
int w2=win("test [a-z]{3} blue" "Notepad" "Notepad" 0x200) 
act w2
 the [a-z]{3}  means any 3 letters a-z lowercase
#3
Brilliant thank you.

What if the length of the middle part xyz can vary so that the number of characters is not known in advance please?

eg could be 3 characters or 5

Is some sort of broad wildcard matching possible?
#4
this is what ya should need
matches any letter upper or lower case or combination of both 3 to 5 characters long
Code:
Copy      Help
,int w1=win("test [a-zA-Z]{3,5} black" "Notepad" "Notepad" 0x200) 
,act w1

here is a quick demo of it in action

Function RandomNotepadName
Code:
Copy      Help
run "$system$\notepad.exe" "" "" "" 0x800 win("" "Notepad") w
rep 10
,str mp
,mp.RandomString(3 5 "a-zA-Z")
,_s.format("test %s black" mp)
,_s.setwintext(w)
,0.3
,int w1=win("test [a-zA-Z]{3,5} black" "Notepad" "Notepad" 0x200) 
,act w1
,1
,act _hwndqm
,1
#5
Very impressed thank you.

Is it possible to match unknown length wild cards - should I just put the max length of the wildcard in  e.g. 99 ?

Is it possible to include characters other than letters in the wildcard e.g. "-","%","+" etc and multiple wildcards at different points in the window name

eg "test xyz blue abc"


Very grateful thank you in advance
#6
1. Is it possible to match unknown length wild cards?  
yes as long as the maxlength is higher than the unknown length 

in the regex pattern [a-zA-Z]{3,5} matches any letters 3 to 5 characters long the {3,5} the 3 is the min number to match the 5 is the max. can change either number. The [] means characters in set. So could do

[a-zA-Z0-9]{3,50} which would match any group of letters and or letters and numbers 3 to 50 characters long


2. Is it possible to include characters other than letters in the wildcard e.g. "-","%","+" etc and multiple wildcards at different points in the window name?
yes
regular expressions are a very powerful tool


Code:
Copy      Help
int w1=win("test .{3,50} black .{3,50}" "Notepad" "Notepad" 0x200)

will match name  test followed by any character 3-50 in length followed by black followed by  any character 3-50 in length

another quick example for a more complicated search with random characters in the middle and the end

Function RandomNotepadName2
Code:
Copy      Help
int w
run "$system$\notepad.exe" "" "" "" 0x800 win("" "Notepad") w
rep 10
,str mp ep
,mp.RandomString(3 50)
,ep.RandomString(3 50)
,_s.format("test %s black %s" mp ep)
,_s.setwintext(w)
,0.3
,int w1=win("test .{3,50} black .{3,50}" "Notepad" "Notepad" 0x200) 
,act w1
,1
,act _hwndqm
,1
#7
Very helpful thank you. I will study your reply and regular expressions as they are new to me despite years with QM.
#8
The combination of QM and ability to do some basic RegEx is EXTREMELY powerful for any Windows API type tasks.
I basically learned by using the RegEx wizard built into the QM interface: Under 'More Tools' in the top floating QM menu (three triangles) is 'Reg Expr Menu'. It is truly a menu/wizard in that I use it all the time to generate the regex around my specific text to capture, so don't have to memorize the strange regex constructions i.e. 'followed by' or 'preceeded by' ....very powerful.
There is also a help menu there and example code snippets. There are a million sites online that do general intro's and can walk you through the RegEx 'logic' until you get it (some advanced stuff still escapes me).
Also, if you just google the exact issue you are having, there is usually someone on StackOverflow or some other forum that has anwered that same exact problem...so you don't have to bother Gintaras all the time :-)
(though he has answered many RegEx questions in the various forum posts - including my own).
So yes, definitely, definitely something that can take your QM Jedi skills up a belt-level (sorry for mixed metaphor).
It also becomes super useful in other applications - e.g. complex search, find, filter missions in Excel an other apps that seem impossible for others, end up being a 1 or 2 line loop.
S
#9
Maybe this can be of help:
https://www.youtube.com/watch?v=JyGvpOK6GfI
Look below in the description section for time-stamps chapters to quickly navigate to the desired topic(s).

And maybe:
https://www.youtube.com/watch?v=HM3WkEYG2Bo
(Tip 2 , Tip 9 and Tip 11 , see also below in description for time stamp links)


Forum Jump:


Users browsing this thread: 1 Guest(s)