GetActiveWindow suddenly not always working

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

GetActiveWindow suddenly not always working

Post by Dick99999 » Sun Jul 27, 2014 10:33 am

The code below sometimes does not get the name of the active window. Sometimes, meaning if I start notepad, then run the code by hot key, all is fine.
When I try it a second time (after activating notepad) there is no window title, length is 0. This happened 'suddenly' probably from last week on. Notepad is an example, get this issue also with other programs.

Running windows 8.1 Upd 1 and MS 14.0.18
Any help greatly appreciated

Code: Select all

Let>WIN_USEHANDLE=0
Let>GAW_TYPE=0
GetActiveWindow>activeWin,WinX,WinY,winW,winH
Length>activeWin,activeWinLen
MDL>active=<%activeWin%> length=%activeWinLen%
if>activeWinLen=0
// second time on this code is executed, while notepad is active

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1389
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: GetActiveWindow suddenly not always working

Post by Dorian (MJT support) » Mon Jul 28, 2014 7:09 pm

I hope I'm not missing the point here... but...

I ran this once, with a blank Notepad doc selected, and it gave me :

active=<Untitled - Notepad> length=18

Running it a second time immediately gave me :

active=<> length=0

When MessageModal pops up, it takes the focus. It doesn't return the focus to Notepad after you click it away, so when the script is run again, there is no active window. Hence there's nothing to return.

Does that make sense?
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: GetActiveWindow suddenly not always working

Post by JRL » Mon Jul 28, 2014 8:07 pm

I suspect the window (Notepad for example) doesn't have focus the second time around. The following code uses the Window_NewActive Event handler to grab window information each time a different window becomes active. If you run the following, does the message ever fail to provide the title and length of any window selected? This should test the accuracy of GetActiveWindow>.

If you run the program, pressing the ESC key will stop it.

Code: Select all

OnEvent>WINDOW_NEWACTIVE,0,0,Process
OnEvent>key_down,VK27,0,Quit
Let>msg_xpos=0
Let>msg_ypos=0
Message>

Label>Loop
Wait>0.01
Goto>Loop

SRT>Quit
  IfWindowOpen>Macro Scheduler Message*
    CloseWindow>Macro Scheduler Message*
  EndIf
  Exit>0
END>Quit

SRT>Process
Let>WIN_USEHANDLE=0
Let>GAW_TYPE=0
GetActiveWindow>activeWin,WinX,WinY,winW,winH
Length>activeWin,activeWinLen
SetControlText>Macro Scheduler Message,TMemo,1,active=<%activeWin%> length=%activeWinLen%
END>Process

Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Re: GetActiveWindow suddenly not always working

Post by Dick99999 » Mon Jul 28, 2014 8:16 pm

Dorian Ellis wrote:I hope I'm not missing the point here... but...

I ran this once, with a blank Notepad doc selected, and it gave me :

active=<Untitled - Notepad> length=18

Running it a second time immediately gave me :

active=<> length=0

When MessageModal pops up, it takes the focus. It doesn't return the focus to Notepad after you click it away, so when the script is run again, there is no active window. Hence there's nothing to return.

Does that make sense?
That's right as you describe it. However,
- The first time I run the code and I click the MDL message away,
- Then I click on notepad (still open), so it gets focus again
- Then I run the code again (by hot key) and it gives : active=<> length=0
- If I close notepad, reopen it, then it's again 'first time'

Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Re: GetActiveWindow suddenly not always working

Post by Dick99999 » Mon Jul 28, 2014 8:33 pm

JRL wrote:I suspect the window (Notepad for example) doesn't have focus the second time around. The following code uses the Window_NewActive Event handler to grab window information each time a different window becomes active. If you run the following, does the message ever fail to provide the title and length of any window selected? This should test the accuracy of GetActiveWindow>.

If you run the program, pressing the ESC key will stop it.
Thanks. Almost the same result:
- run your code
- run note pad: active=<Untitled - Notepad> length=18
- click on an open Firefox
- click notepad (still open) again and result is: active=<> length=0

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: GetActiveWindow suddenly not always working

Post by JRL » Mon Jul 28, 2014 9:02 pm

Ok... Just installed 14.0.17 (I don't have 14.0.18) on my Win 8.1 computer and had no problems using your script or my script. Currently applying the latest updates for Win 8.1 to see if that makes any difference.

Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Re: GetActiveWindow suddenly not always working

Post by Dick99999 » Tue Jul 29, 2014 11:15 am

Thanks for trying. I tested your script on windows 7. Same result, does not display a process name after the first time. (That first time is OK.)

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: GetActiveWindow suddenly not always working

Post by JRL » Tue Jul 29, 2014 3:06 pm

Thanks. I've now got all of my computers updated. :D

Tried this on my XP , Win 7 and Win 8.1 computer. No problems on any of them.

Here are the steps I use.

1) copy the script from this thread
2) save the script and assign it a hotkey
3) minimize the macro scheduler main window.
4) open a couple of applications (notepad and firefox)
5) arrange the windows so I can click on them
6) press the hotkey combination to run the script

Doesn't matter what I click I always get a name and a length. I did get a blank and zero once when I closed notepad. Focus defaulted back to the message box and apparently the script doesn't see its own message box? Makes me wonder if something like that is happening to you.

Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Re: GetActiveWindow suddenly not always working

Post by Dick99999 » Tue Jul 29, 2014 4:48 pm

Indeed step 1 to 6 is what I also did. Thanks again for testing. Glad I could help to make your computers up to date :-) . I tried some MS older version, all same result on win 8.1/Surface pro 2.

However,....... I changed the script a little and now it always displays the window name (except the MS window as you also experienced.) The change is a short delay and a clear message. So I will test which of the 2 is causing that.

Code: Select all

//Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
//If using IGNORESPACES quote strings in {" ... "}
//Let>IGNORESPACES=1
OnEvent>WINDOW_NEWACTIVE,0,0,Process
OnEvent>key_down,VK27,0,Quit
Let>msg_xpos=0
Let>msg_ypos=0
Message>
Label>Loop
Wait>0.01
Goto>Loop
SRT>Quit
  IfWindowOpen>Macro Scheduler Message*
    CloseWindow>Macro Scheduler Message*
  EndIf
  Exit>0
END>Quit
SRT>Process
SetControlText>Macro Scheduler Message,TMemo,1,Clear,m switchingn aftern 1 sec
Let>WIN_USEHANDLE=0
Let>GAW_TYPE=0
Let>WIN_USEHANDLE=0
Let>GAW_TYPE=0
wait>0.3
GetActiveWindow>activeWin,WinX,WinY,winW,winH
GetWindowList>winList
Length>activeWin,activeWinLen
SetControlText>Macro Scheduler Message,TMemo,1,active=<%activeWin%> length=%activeWinLen% List: %winlist%
END>Process


Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Re: GetActiveWindow suddenly not always working

Post by Dick99999 » Wed Aug 06, 2014 12:42 pm

My experience these last days is that the issue is intermittent. For example, if no window is open, the program manager should be the active window, at least that is what I get most times. Sometimes, however, I don't get Program Manager, and as described before, I get a empty string as the active window name.

Since MacroScheduler programs also give me an empty window name when being the active window, one of the possibles for this issue is that macro scheduler itself interferes somehow, sometimes.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: GetActiveWindow suddenly not always working

Post by JRL » Wed Aug 06, 2014 1:51 pm

My guess is that the empty string window name is always a Macro Scheduler window and you could just assign that value to "empty string". You could test that theory by doing a few simple things.

1) Edit the script you're testing with by replacing any message windows with a write to a text file. So any line in your script that starts with Message gets replaced with WriteLn>. Write the same info you put in the message. This will assure you don't have Macro Scheduler message windows stealing focus.

2) Close Macro Scheduler. At the Macro Scheduler main menu select "File" then "Exit". Check Task Manager to be sure there are no instances of msched.exe running. This way you know that no Macro Scheduler windows will become active.

3) Open Windows Explorer and browse to the location of your script and run it by double clicking on it.

If you take these steps, the only time you should see any Macro Scheduler windows would be if the script errors for some reason and that should be obvious. Of course this method will be more difficult to "watch". There will not be any immediate visual telling you which window is currently active. You'll need to leave the script running for a while and occasionally go look at the text file to see if there are any "empty string" lines.

Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Re: GetActiveWindow suddenly not always working

Post by Dick99999 » Thu Aug 07, 2014 8:37 am

This morning another hit. And I think I know what's causing this. When I start a macro by a hot key, MS in the system tray shows a message like 'running name macro'. As long as that is active the empty window name may turn up. Have not tested it thoroughly though.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts