Jump to content
Macro Express Forums

Getting a handle on handles


Recommended Posts

Problem: Distinguishing between windows with the same title.

Solution: Using windows handles.

Problem: I can’t figure out how to make them work!

 

The issue has come up a few times where windows with the same title have caused a problem. I remember reading in the release notes that one could now grab windows handles and save them in the new handles variable type. It sounded like the perfect solution but I must be thick because I can’t seem to figure out how to use them.

 

The Variable Set Handle help topic says

This command is a reliable way to uniquely identify windows by their titles. Even if the window title later changes, this command still identifies the window - as long as the window has not closed. In the operating system each window receives a handle for unique identification purposes. Use this variable later in other Window commands such as Resize, Reposition, etc.
Sounds like one would use the handle variable in place of the window title but that doesn’t work. In Windows Activate I get “Undefined variable or of wrong type” and in Window Reposition it claims it can’t find the window.

 

This sounds like the solution to all our problems but I’m to thick to figure out how they work. Can someone explain to me what I’m doing wrong?

Link to comment
Share on other sites

Problem: Distinguishing between windows with the same title.

Solution: Using windows handles.

Problem: I can't figure out how to make them work!

 

The issue has come up a few times where windows with the same title have caused a problem. I remember reading in the release notes that one could now grab windows handles and save them in the new handles variable type. It sounded like the perfect solution but I must be thick because I can't seem to figure out how to use them.

 

The Variable Set Handle help topic saysSounds like one would use the handle variable in place of the window title but that doesn't work. In Windows Activate I get "Undefined variable or of wrong type" and in Window Reposition it claims it can't find the window.

 

This sounds like the solution to all our problems but I'm to thick to figure out how they work. Can someone explain to me what I'm doing wrong?

 

Trying a simple test I got the same error as you.

 

I don't have any identical window titles to hand, so I was using 'Partial Match'. Maybe that's the probem in my case.

 

BTW, running it crashed ME Pro. Couldn't close with running man or <Pause+Scroll Lock> hotkey, had to terminate. Here's the script and code:

 

Variable Set Handle %Handle TextPad% from all windows with the title of "Latest - Textpad"

Text Box Display:

 

 

<VARIABLE SET HANDLE Option="\x00" Info="Latest - Textpad" Partial="TRUE" Wildcards="FALSE" Destination="%Handle TextPad%"/>

<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil Tahoma;}{\\f1\\fnil\\fcharset0 Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 %Handle TextPad[1]%\r\n\\par %Handle TextPad[\\f1 2\\f0 ]%\r\n\\par %Handle TextPad[\\f1 3\\f0 ]%\r\n\\par %Handle TextPad[\\f1 4\\f0 ]%\r\n\\par %Handle TextPad[\\f1 5\\f0 ]%\r\n\\par %Handle TextPad[\\f1 6\\f0 ]%\r\n\\par %Handle TextPad[\\f1 7\\f0 ]%\r\n\\par %Handle TextPad[\\f1 8\\f0 ]%\r\n\\par %Handle TextPad[\\f1 9\\f0 ]%\r\n\\par %Handle TextPad[1\\f1 0\\f0 ]%\r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

 

It would be good to see a few examples in Help.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

Trying a simple test I got the same error as you.

 

I don't have any identical window titles to hand, so I was using 'Partial Match'. Maybe that's the probem in my case.

 

BTW, running it crashed ME Pro. Couldn't close with running man or <Pause+Scroll Lock> hotkey, had to terminate.

The following code opens 2 copies of Notepad, saves all window titles containing "Notepad" to a handle variable %hwin% (an array variable of 5 elements), then repositions the first window (%hwin[1]%). It works perfectly for me.

Launch Program and Activate Window: Program "notepad.exe", Parameters "", Window "notepad"
Launch Program and Activate Window: Program "notepad.exe", Parameters "", Window "notepad"
Variable Set Handle %hwin% from all windows with the title of "notepad"
Window Reposition: %hwin[1]% Move 10%, 10% relative to the current location

<LAUNCH PROGRAM AND ACTIVATE WINDOW Title="notepad" Exact_Match="FALSE" Wildcards="FALSE" Path="notepad.exe" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12"/>
<LAUNCH PROGRAM AND ACTIVATE WINDOW Title="notepad" Exact_Match="FALSE" Wildcards="FALSE" Path="notepad.exe" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12"/>
<VARIABLE SET HANDLE Option="\x00" Info="notepad" Partial="TRUE" Wildcards="FALSE" Destination="%hwin%"/>
<WINDOW REPOSITION Option="\x01" Title="%hwin[1]%" Partial="TRUE" Wildcards="FALSE" Method="\x0B" Left="10" Top="10" Monitor="0" _IGNORE="0x0006"/>

Link to comment
Share on other sites

This doesn't work 100% for me..................................

 

I modified it a little bit: I have it position %hwin[1]% in the top left corner, then position %hwin[2]% in the top right. I worked the first time I ran it, but not any time since. It launches the notepads fine, but doesn't reposition them...

 

Well, it repositions one of them: %hwin[2]% But %hwin[1]% just sits where it was when it opened.

 

<LAUNCH PROGRAM AND ACTIVATE WINDOW Title="notepad" Exact_Match="FALSE" Wildcards="FALSE" Path="notepad.exe" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12"/>
<LAUNCH PROGRAM AND ACTIVATE WINDOW Title="notepad" Exact_Match="FALSE" Wildcards="FALSE" Path="notepad.exe" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12"/>
<VARIABLE SET HANDLE Option="\x00" Info="notepad" Partial="TRUE" Wildcards="FALSE" Destination="%hwin%"/>
<WINDOW REPOSITION Option="\x01" Title="%hwin[1]%" Partial="TRUE" Wildcards="FALSE" Method="\x02" Left="10" Top="10" Monitor="0" _IGNORE="0x0006"/>
<WINDOW REPOSITION Option="\x01" Title="%hwin[2]%" Partial="TRUE" Wildcards="FALSE" Method="\x04" Left="10" Top="10" Monitor="0" _IGNORE="0x0006"/>

Link to comment
Share on other sites

This works but with a tweak it doesn't..................................

You sample worked right off the bat. I then removed the first two lines and left the windows up and typed in each 1 and 2 and noticed something a little concerning. If I changed the window order manually it would move the other window. I'm not sure if this is a bad thing yet though as each time we would need to do some extra steps to determine which window was which. EG get the date of each email or something and save those values in a parallel array.

 

Now I changed your macro like this:

 Variable Set Handle %Handle% from a window with the title, "Untitled - Notepad"
Window Reposition: %Handle% Move 10%, 10% relative to the current location

where %Handle% is not an array and it does nothing. Also tried Window Activate and got noting. Doesn’t even generate an error. Is this a bug or am I missing something?

Link to comment
Share on other sites

I think I found one bug with the array................................................................

Variable Set Handle %hwin% from all windows with the title of "Untitled - Notepad"
Window Activate: %hwin[1]%

I have two “Untitled – Notepad” windows open and this one does nothing. I changed it to index 2 and it opened the first window. Huh? Then on a lark I changed it to 3 and it opened the second window! I checked the variable window and sure enough even though there are only 2 notepad windows open it propagated 3 values. But the value of the first looks different. 2 and 3 are 600544 and 992002 but the first one has one less digit 65666. I don’t know if that means anything. Maybe to Chris.

 

I was thinking the Window With Specific Title and Enumerate Windows With a Specific Title might be using the same mechanism. IE if it’s just one you just stick the topmost one in a non-array variable. Sure enough the variable window shows 65666 so that appears to be the case. Looking more like a bug.

 

I tried turning off Aero as well but there was no difference.

Link to comment
Share on other sites

But now it works. WTF?...............................................

So I was going to do some tests with controls and decided to use Calculator but started with some baseline tests. Guess what. Now with two open it only propagates two elements of the array. And the single option works. I went back to Notepad and again had three and the single option wouldn’t work. What does it all mean?

Link to comment
Share on other sites

Splitting the thread to talk about controls..........................................

So let’s assume for the moment that the handles can be used in most window manipulation commands and that my early frustration was user error or a bug. Now how do I use it with controls? I thought maybe I could use the Variable Modify Control to change the window title but it does not let me enter a handle variable. I also tried the entering a handle in the Top-Level Window Caption box but again no joy. So let’s say I want to calculate something in Calculator 1 and something else in Calculator 2 using controls how would I do that?

Link to comment
Share on other sites

I amended Paul's macro so that I could more clearly see what was happening to the Notepad windows. And I also added another Window Activate to test what would happen if reference was made to a third window/handle that didn't exist.

 

Launch Program and Activate Window: Program "notepad.exe", Parameters "", Window "notepad"

Launch Program and Activate Window: Program "notepad.exe", Parameters "", Window "notepad"

Variable Set Handle %hwin% from all windows with the title of "notepad"

Window Activate: %hwin[1]%

Text Type (Simulate Keystrokes): This is Notepad window # 1

Delay: 100 milliseconds

Window Reposition: %hwin[1]% Move to the top-left corner

Window Activate: %hwin[2]%

Text Type (Simulate Keystrokes): This is Notepad window # 2

Delay: 100 milliseconds

Window Reposition: %hwin[2]% Move to the top-right corner

Window Activate: %hwin[3]%

<LAUNCH PROGRAM AND ACTIVATE WINDOW Title="notepad" Exact_Match="FALSE" Wildcards="FALSE" Path="notepad.exe" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12"/>
<LAUNCH PROGRAM AND ACTIVATE WINDOW Title="notepad" Exact_Match="FALSE" Wildcards="FALSE" Path="notepad.exe" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12"/>
<VARIABLE SET HANDLE Option="\x00" Info="notepad" Partial="TRUE" Wildcards="FALSE" Destination="%hwin%"/>
<WINDOW ACTIVATE Title="%hwin[1]%" Exact_Match="FALSE" Wildcards="FALSE" _IGNORE="0x0006"/>
<TEXT TYPE Action="0" Text="This is Notepad window # 1"/>
<DELAY Flags="\x02" Time="100"/>
<WINDOW REPOSITION Option="\x01" Title="%hwin[1]%" Partial="TRUE" Wildcards="FALSE" Method="\x02" Left="10" Top="10" Monitor="0" _IGNORE="0x0006"/>
<WINDOW ACTIVATE Title="%hwin[2]%" Exact_Match="FALSE" Wildcards="FALSE" _IGNORE="0x0006"/>
<TEXT TYPE Action="0" Text="This is Notepad window # 2"/>
<DELAY Flags="\x02" Time="100"/>
<WINDOW REPOSITION Option="\x01" Title="%hwin[2]%" Partial="TRUE" Wildcards="FALSE" Method="\x04" Left="10" Top="10" Monitor="0" _IGNORE="0x0006"/>
<WINDOW ACTIVATE Title="%hwin[3]%" Exact_Match="FALSE" Wildcards="FALSE" _PROMPT="0x0006"/>

 

TestHandles_1.mex

 

The two windows opened OK. But the final activate was set to Prompt for Action if the window wasn't found, yet that was ignored and the macro finished without protest.

 

I haven'y yet checked out Cory's later tests. I'm hoping maybe ISS will contribute with the definitive explanations about each variation. Rather than our testing all combinations by extensive trial/error.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

I amended Paul's macro so that I could more clearly see what was happening to the Notepad windows. And I also added another Window Activate to test what would happen if reference was made to a third window/handle that didn't exist.

The two windows opened OK. But the final activate was set to Prompt for Action if the window wasn't found, yet that was ignored and the macro finished without protest.

I'm not sure if attempting to activate a window using a handle that doesn't exist [any longer] is really an error. You can always use the "If Window "%hwin[3]%" is running" command. I'm pretty sure that addressing non-existent windows like this in VB never produced an error either.

Link to comment
Share on other sites

But the window does exist...........................................

Look, if I open Notepad and it gets buried somehow I use the Activate command to bring it back to being, well, the active window. Why would you think it doesn't exist?

Link to comment
Share on other sites

But the window does exist...........................................

Look, if I open Notepad and it gets buried somehow I use the Activate command to bring it back to being, well, the active window. Why would you think it doesn't exist?

 

No, in this case the third Notepad window doesn't exist. As I said in the intro to my revised macro, "I also added another Window Activate to test what would happen if reference was made to a third window/handle that didn't exist."

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

Sorry all I think I responded incorrectly...........................................................

I was thinking Paul was talking about my original problem and now that I re-read I see he’s quoting your post. I want to know why a simple Windows Activate doesn’t work with a non-array variable. IE set Window Handle to specific window title.

Link to comment
Share on other sites

I was thinking Paul was talking about my original problem and now that I re-read I see he's quoting your post. I want to know why a simple Windows Activate doesn't work with a non-array variable. IE set Window Handle to specific window title.

When you set a handle, at least from all windows with a common title, then clearly there may be more than one such window. So MEP expects to populate an array, even if there's only a single element. I imagine MEP treats array and scalar variables differently. So it expects to populate an array, and you provide a scalar. I guess that, metaphorically speaking, MEP shrugs its shoulders and simply ignores you!

Link to comment
Share on other sites

I beg to differ, non-array do work.

........................................................

Don’t forget I’ve already established that the array is being populated for some windows with N+1 elements. EG 2 Notepads result in 3 elements being populated whereas 2 Calculator results in 2. It must be a bug but I’m thinking it’s affecting the Window with a specific title command as well.

 

Also I do believe it’s design intent is such that one does not use an array command. If you will notice when selecting a variable using the Variables button the Window with a specific title shows both array and non array variables. But if one chooses Enumerate windows with a specific title it filters out non-array variables. This suggests to me we should be able to use non-array variables when only one value is going to be returned. Additionally the syntax checker has no problem with the non-array variable. But most importantly if I use a non-array var with Window with a specific title and use Calculator instead of Notepad it works!

 

I’m going to report these as bugs later. I must be cursed. It seems every time I try to use a new MEP feature it fails in practical application.

Link to comment
Share on other sites

I beg to differ, non-array do work.

But earlier you said

I want to know why a simple Windows Activate doesn’t work with a non-array variable. IE set Window Handle to specific window title.

So now I'm confused! Does it work, or doesn't it work (or does it depend on which direction you're facing at the time?).

 

I couldn't get my earlier example to work with a scalar variable.

Link to comment
Share on other sites

I think I might have confused you

.....................................................

In the beginning it was not working. That is before I found the bug and the difference between Notepad and Calculator. Now the script below works but only with Calculator.

<VARIABLE SET HANDLE Option="\x01" Info="Calculator" Partial="FALSE" Wildcards="FALSE" Destination="%Handle%"/>

<WINDOW ACTIVATE Title="%Handle%" Exact_Match="FALSE" Wildcards="FALSE" _IGNORE="0x0006"/>

Your earlier example =was using the Enumerate option and you are correct it needs an array var.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...