Advice on Windows/Dialog Handling

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
carfan
Newbie
Posts: 9
Joined: Tue Jul 17, 2007 11:07 am

Advice on Windows/Dialog Handling

Post by carfan » Sat Aug 18, 2007 1:04 am

Can anyone advise how I can best handle the following situation?

I am in a window application (APP) main screen and I trigger a particular function.
Another windows (providing that function) pops out.
Now, if there is any exception(s), one or more error dialogs (with OK button) may appear.
These dialogs contains different error text which I need to clear (by clicking Ok).
I want to be able to recognise the error texts so that I can decide whether
to proceed or to terminate back to the main screen.
If there is no error dialog, I want to proceed to the next stage of automation through the subfunctions provided in the function window.

Appreciate some guidance on this. Spend quite a fair bit of time without success.

Thanks in advance.

Snickers
Macro Veteran
Posts: 150
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Post by Snickers » Sat Aug 18, 2007 2:58 am

Do the error messages pop up in the same locations every time?

Are all error message window_titles the same?

How many different type of error messages do you expect may appear?

carfan
Newbie
Posts: 9
Joined: Tue Jul 17, 2007 11:07 am

Post by carfan » Sat Aug 18, 2007 4:14 am

Snickers wrote:Do the error messages pop up in the same locations every time?

Are all error message window_titles the same?

How many different type of error messages do you expect may appear?
1. same location

2. yes, same dialog title but different message in the dialog.

3. at most 3 different types that may appear.

Thanks.

Snickers
Macro Veteran
Posts: 150
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Post by Snickers » Sat Aug 18, 2007 4:31 am

When an error message comes up, try the following code to see if it retrieves any text from the window:

Code: Select all

GetWindowText>window_title,text
MessageModal>%text%
If that code retrieves text from the window, the following code can be used to determine which error message popped up:

Code: Select all

GetWindowText>window_title,text
if>text="the EXACT text you see on error message 1"
  GoSub>whereever1
endif

if>text="the EXACT text you see on error message 2"
  GoSub>whereever2
endif

if>text="the EXACT text you see on error message 3"
  GoSub>whereever3
endif
GetWindowText>window_title,text
replace "window_title" with the caption of the error message. For example: the caption on a notepad window is Untitled - Notepad. You can replace that "window_title" with "notepad*" and it will detect any window with the word notepad in the caption

GetWindowText>window_title,text
text is the name of the variable to which the text in the window will be stored.

If this works, I may have a suggestion on detecting when the error message pops up. GetActiveWindow> GetWindowHandle> GetPixelColor> all of these are the methods I primarily use.

If this doesn't work, my only other suggestion is much sloppier than this. My code isn't usually very clean but it gets the job done. The Macro Scheduler pros will be on in the morning, and they can certainly help you more than I can.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Aug 18, 2007 7:05 am

If you cannot predict *when* these dialogs appear and therefore need to clear them whenever they appear, use OnEvent to create an event handler. Use a WINDOW_OPEN event handler. See OnEvent in the help and/or search this forum for OnEvent.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

carfan
Newbie
Posts: 9
Joined: Tue Jul 17, 2007 11:07 am

Post by carfan » Sat Aug 18, 2007 3:07 pm

Thanks for the advice so far.
To be more precise, the app is connect to a serial device.
There can be a number of exceptions after clicking "Fetch Status" in the app Main.
(Case 1) In normal case, a "Status" window will appear and give the status report.

(Case 2) However, if the serial device is not connected properly, the "Status" window and a modal dialog will appear. In this case, I want to know the error and handle it accordingly. E.g. stop the script

(Case 3) Even when the device connection is ok, sometimes there may be a reading error on some sections reported by a device (other content segements are ok). In this case, the "Status" window and another modal dialog (a warning) with different dialog text will appear which I have to clear. However, in this case, I will not stop the script but to continue to the automation.

For both Case 2 and 3, I use GetActiveWindow and able to get the window title "Status". However, the dialog window cannot be detected.

By the way, how do I change the caption of a window?

Thanks.

carfan
Newbie
Posts: 9
Joined: Tue Jul 17, 2007 11:07 am

Post by carfan » Sun Aug 19, 2007 7:09 am

mtettmar wrote:If you cannot predict *when* these dialogs appear and therefore need to clear them whenever they appear, use OnEvent to create an event handler. Use a WINDOW_OPEN event handler. See OnEvent in the help and/or search this forum for OnEvent.
Thanks for advice. I tried out OnEvent with WINDOW_OPEN.
OnEvent>WINDOW_OPEN,Status*,1,DoOpenWindow

1st observation:
The Status Window is detected but not its error dialog window.

2nd observation:
Onevent also returns Macro Scheduler app window. Why?

One further question, how can I "turn off" the onevent once it is being initiated?

Thanks again.

carfan
Newbie
Posts: 9
Joined: Tue Jul 17, 2007 11:07 am

Post by carfan » Mon Aug 20, 2007 6:10 am

Ok, I have explored "View System Windows" and able to find the error dialog "Error" of class "#32770 as caption. It has a static that says "Unable to connect to device".

Back to the issue.

Code roughly as follows:

// Set focus on the application
Setfocus>App
// Click Fetch Status Button
MouseMove>10,10
LCLICK

// Wait for Status Window to appear
WaitWindowChanged>10

// Status Window appears.
// However if there is any error, a dialog window will appear as well.
// This is the part where I need to handle any error dialog window that might appear
// Using OnEvent churns up lots of unwanted windows whose windows title is not what I stated.
// e.g. OnEvent>WINDOW_OPEN,Status,1,DoOpenWindow

// What that works is as follows.
// It clears the error dialog if any and is able to proceed with the automation.
// However, I do not know what error it was. Further, if any error dialog appears again.
// My automation will go haywire.
Wait>10
PRESS ENTER

// procedure to get the status
Gosub>GetStatus

Thanks.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Aug 20, 2007 6:56 am

// What that works is as follows.
// It clears the error dialog if any and is able to proceed with the automation.
// However, I do not know what error it was. Further, if any error dialog appears again.
// My automation will go haywire.
Why would it go haywire? Your DoOpenWindow subroutine should clear it and then the script can continue as normal. The subroutine will fire whenever such a matching dialog again pops up. So it works every time, not just the first time.

System dialogs can be captured by simply pressing CTRL-C on them. This copies the dialog message to the clipboard. This may work with your dialog. You could then have your DoOpenWindow subroutine do:

Press CTRL
Send>c
Release CTRL
Wait>0.5
GetClipBoard>dlgText

And then it can store the text or look for a word in it and make decisions based on what the error was. Or whatever you want to do.

If that doesn't work you can try using GetWindowText. Some of the dialog text may be extractable with that.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

carfan
Newbie
Posts: 9
Joined: Tue Jul 17, 2007 11:07 am

Post by carfan » Mon Aug 20, 2007 8:59 am

mtettmar wrote:
Why would it go haywire? Your DoOpenWindow subroutine should clear it and then the script can continue as normal. The subroutine will fire whenever such a matching dialog again pops up. So it works every time, not just the first time.

System dialogs can be captured by simply pressing CTRL-C on them. This copies the dialog message to the clipboard. This may work with your dialog. You could then have your DoOpenWindow subroutine do:

Press CTRL
Send>c
Release CTRL
Wait>0.5
GetClipBoard>dlgText

And then it can store the text or look for a word in it and make decisions based on what the error was. Or whatever you want to do.

If that doesn't work you can try using GetWindowText. Some of the dialog text may be extractable with that.
Thanks! I still have a few questions to strengthen my understanding. Hope you can enlighten...

Qn 1:
// "Error" is dialog's title
OnEvent>WINDOW_OPEN,Error,2,DoOpenWindow
apparently did not work well in my testing. I am looking specifically for "Error" but all sorts of other windows were captured. Why is that so?

Qn 2:
What is the lifespan of OnEvent? Is it as long as the script runs?
Is there any way to suspend and resume during the lifespan of the script run?

Qn 3:
When the OnEvent executes a Subroutine, does the main script flow continue or does it suspend itself until the subroutine execution finishes?

Anyway when I use the following, it works better than WINDOW_OPEN
OnEvent>WINDOW_NEWACTIVE,0,0,DoNewWindow

SRT>DoNewWindow
GetActiveWindow>title,x,y
If>{%title%="Error"}
Press CTRL
Send>c
Release CTRL
GetClipBoard>dlgTxt
MessageModal>Error Dialog Caught with Text: %CRLF% =%dlgTxt%=
PutClipBoard>
Endif
// ignore all those non error dialogs
END>

Apologise for being inexperience. Thanks for your patience and guidance.

carfan
Newbie
Posts: 9
Joined: Tue Jul 17, 2007 11:07 am

Post by carfan » Mon Aug 27, 2007 5:32 am

Can someone help me out on the my 2nd last post? Thanks.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Aug 27, 2007 7:12 am

Qn 1:
// "Error" is dialog's title
OnEvent>WINDOW_OPEN,Error,2,DoOpenWindow
apparently did not work well in my testing. I am looking specifically for "Error" but all sorts of other windows were captured. Why is that so?
This command does not "capture" windows. What it does is jump to the subroutine if a window titled "Error" is open. If the subroutine runs then there MUST be a window titled "Error" open.
Qn 2:
What is the lifespan of OnEvent? Is it as long as the script runs?
Is there any way to suspend and resume during the lifespan of the script run?
OnEvent is active for the duration of the script. To suspend/resume it you can set a variable and have the subroutine check the value of that variable.
Qn 3:
When the OnEvent executes a Subroutine, does the main script flow continue or does it suspend itself until the subroutine execution finishes?
The main script flow continues after the subroutine is executed.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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